learning-cc4e

Solution to https://cc4e.com/. If you copy these you're not right in the head.
git clone https://kaka.farm/~git/learning-cc4e
Log | Files | Refs

solution.c (1362B)


      1 void list_add(lst, value)
      2     struct list *lst;
      3     int value;
      4 {
      5   struct lnode *new_node = (
      6     (struct lnode *)
      7     malloc(sizeof(struct lnode))
      8   );
      9   new_node->value = value;
     10   new_node->next = NULL;
     11 
     12   if (lst->head == NULL) {
     13     lst->head = new_node;
     14   }
     15 
     16   if (lst->tail == NULL) {
     17     lst->tail = new_node;
     18   } else {
     19     lst->tail->next = new_node;
     20     lst->tail = new_node;
     21   }
     22 }
     23 
     24 void list_remove(lst, value)
     25     struct list *lst;
     26     int value;
     27 {
     28   struct lnode *previous_lnode = NULL, *current_lnode = NULL;
     29 
     30   current_lnode = lst->head;
     31   while (current_lnode != NULL) {
     32     if (current_lnode->value == value) {
     33       if (lst->head == lst->tail) {
     34         free(current_lnode);
     35         current_lnode = NULL;
     36         lst->head = NULL;
     37         lst->tail = NULL;
     38       } else if (lst->head == current_lnode) {
     39         lst->head = current_lnode->next;
     40         free(current_lnode);
     41         current_lnode = lst->head;
     42       } else if (lst->tail == current_lnode) {
     43         lst->tail = previous_lnode;
     44         previous_lnode->next = NULL;
     45         free(current_lnode);
     46         current_lnode = NULL;
     47       } else {
     48         previous_lnode->next = current_lnode->next;
     49         free(current_lnode);
     50         current_lnode = previous_lnode->next;
     51       }
     52     } else {
     53       previous_lnode = current_lnode;
     54       current_lnode = current_lnode->next;
     55     }
     56   }
     57 }