Advertisement
Guest User

list_delete_contains.c

a guest
May 24th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None | 0 0
  1. // delete the first node from a linked list containing a value argument
  2. // 25/5/2018
  3. // Lyall Beveridge & Khye Ean Lowe
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <assert.h>
  8.  
  9. struct node {
  10.     struct node *next;
  11.     int          data;
  12. };
  13.  
  14. struct node *delete_contains(int value, struct node *head);
  15. struct node *strings_to_list(int len, char *strings[]);
  16. void print_list(struct node *head);
  17.  
  18. // DO NOT CHANGE THIS MAIN FUNCTION
  19.  
  20. int main(int argc, char *argv[]) {
  21.     if (argc < 2) {
  22.         fprintf(stderr, "Usage: %s value list-elements\n", argv[0]);
  23.         return 1;
  24.     }
  25.     int value = atoi(argv[1]);
  26.     // create linked list from command line arguments
  27.     struct node *head = strings_to_list(argc - 2, argv + 2);
  28.  
  29.     struct node *new_head = delete_contains(value, head);
  30.     print_list(new_head);
  31.  
  32.     return 0;
  33. }
  34.  
  35.  
  36. // Delete the first node in the list containing the value `value`.
  37. // The deleted node is freed.
  38. // If no node contains `value`, the list is not changed.
  39. // The head of the list is returned.
  40.  
  41. struct node *delete_contains(int value, struct node *head) {
  42.     if (head == NULL) {
  43.         return head;
  44.     } else if (head->data == value) {
  45.         struct node *after = head->next;
  46.         free(head);
  47.         if (after == NULL) {
  48.             return NULL;
  49.         } else {
  50.             return after;
  51.         }
  52.     }
  53.  
  54.     struct node *current = head;
  55.     struct node *previous = head;
  56.     // break loop if the next value holds the correct data
  57.     while (current->next != NULL && current->data != value) {
  58.         previous = current;
  59.         current = current->next;
  60.     }
  61.    
  62.     // if loop was broken because there was the data in the next
  63.     if (current->data == value) {
  64.            
  65.          // if the one after next is null, the current pointer will be the last
  66.          if (current->next == NULL) {
  67.              previous->next = NULL;
  68.          } else {
  69.              previous->next = current->next;
  70.          }
  71.          // in all cases the last value is not needed
  72.          free(current);
  73.     }
  74.        
  75.        
  76.     return head;
  77.  
  78. }
  79.  
  80.  
  81. // DO NOT CHANGE THIS FUNCTION
  82. // create linked list from array of strings
  83. struct node *strings_to_list(int len, char *strings[]) {
  84.     struct node *head = NULL;
  85.     for (int i = len - 1; i >= 0; i = i - 1) {
  86.         struct node *n = malloc(sizeof (struct node));
  87.         assert(n != NULL);
  88.         n->next = head;
  89.         n->data = atoi(strings[i]);
  90.         head = n;
  91.     }
  92.     return head;
  93. }
  94.  
  95. // DO NOT CHANGE THIS FUNCTION
  96. // print linked list
  97. void print_list(struct node *head) {
  98.     printf("[");
  99.  
  100.     for (struct node *n = head; n != NULL; n = n->next) {
  101.         // If you're getting an error here,
  102.         // you have returned an invalid list
  103.          printf("%d", n->data);
  104.          if (n->next != NULL) {
  105.             printf(", ");
  106.         }
  107.     }
  108.     printf("]\n");
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement