└── lists.c /lists.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct { 5 | void *next; 6 | int data; 7 | } Node; 8 | 9 | Node *head = NULL; 10 | 11 | // add a node to the list 12 | Node *addNode(int data) 13 | { 14 | Node *new = NULL; 15 | // two cases: 16 | 17 | // if the list is empty. 18 | if (head == NULL) 19 | { 20 | new = malloc(sizeof(Node)); 21 | if (new == NULL) 22 | return NULL; 23 | 24 | new->data = data; 25 | head = new; 26 | new->next = NULL; 27 | } else { 28 | new = malloc(sizeof(Node)); 29 | if (new == NULL) 30 | return NULL; 31 | 32 | new->data = data; 33 | new->next = head; 34 | head = new; 35 | } 36 | 37 | return new; 38 | } 39 | 40 | // remove a node from the list 41 | int removeNode(int data) 42 | { 43 | Node *current = head; 44 | Node *prev = head; 45 | while (current != NULL) 46 | { 47 | if (current->data == data) 48 | { 49 | // if current node is the list head 50 | if (current == head) 51 | { 52 | head = current->next; 53 | } else { 54 | prev->next = current->next; 55 | free(current); 56 | current = NULL; 57 | } 58 | 59 | return 1; 60 | } 61 | prev = current; 62 | current = current->next; 63 | } 64 | 65 | return 0; 66 | } 67 | 68 | // insert a node into a position in the list 69 | Node *insertNode(int data, int position) 70 | { 71 | Node *current = head; 72 | while (current != NULL && position != 0) 73 | { 74 | position--; 75 | } 76 | 77 | if (position != 0) 78 | { 79 | printf("Requested position too far into list\n"); 80 | return NULL; 81 | } 82 | 83 | Node *new = malloc(sizeof(Node)); 84 | if (new == NULL) 85 | return NULL; 86 | 87 | new->data = data; 88 | new->next = current->next; 89 | current->next = new; 90 | 91 | return new; 92 | } 93 | 94 | // print operation 95 | void printList() 96 | { 97 | Node *current = head; 98 | while (current != NULL) 99 | { 100 | printf("%d->", current->data); 101 | current = current->next; 102 | } 103 | 104 | printf("\n"); 105 | return; 106 | } 107 | 108 | void printMenu() 109 | { 110 | printf("You have the following options:\n"); 111 | printf("\t1. Add a node to the list.\n"); 112 | printf("\t2. Remove a node from the list.\n"); 113 | printf("\t3. Insert a node to the list.\n"); 114 | printf("\t4. Print your list\n"); 115 | printf("\t5. Quit.\n"); 116 | return; 117 | } 118 | 119 | 120 | int main(int argc, char **argv) 121 | { 122 | int option = -1; 123 | int arg1 = 0; 124 | int arg2 = 0; 125 | while (option != 5) 126 | { 127 | printMenu(); 128 | int num_received = scanf("%d", &option); 129 | if (num_received == 1 && option > 0 && option <= 5) 130 | { 131 | switch(option) 132 | { 133 | case 1: 134 | // add operation 135 | printf("What data should I insert?:\n"); 136 | scanf("%d", &arg1); 137 | Node *new = addNode(arg1); 138 | break; 139 | case 2: 140 | // remove operation 141 | printf("What data should I remove?:\n"); 142 | scanf("%d", &arg1); 143 | int success = removeNode(arg1); 144 | if (!success) 145 | printf("Element not found\n"); 146 | 147 | break; 148 | case 3: 149 | // insert operation 150 | // remove operation 151 | printf("What data should I insert?:\n"); 152 | scanf("%d", &arg1); 153 | printf("What position?:\n"); 154 | scanf("%d", &arg2); 155 | new = insertNode(arg1, arg2); 156 | if (new == NULL) 157 | printf("Failed to insert into list\n"); 158 | break; 159 | case 4: 160 | // print the list 161 | printList(); 162 | break; 163 | case 5: 164 | break; 165 | } 166 | } 167 | } 168 | 169 | 170 | return 0; 171 | } --------------------------------------------------------------------------------