seafox311

Untitled

Nov 11th, 2025
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.22 KB | None | 0 0
  1. /*
  2.  * You will write a function list_add() to append an integer to the end
  3.  * of a linked list. You will also write a function called list_find() that
  4.  * will return the list node containing the integer value or NULL if the
  5.  * value is not in the list.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. struct lnode {
  12.     int value;
  13.     struct lnode *next;
  14. };
  15.  
  16. struct list {
  17.     struct lnode *head;
  18.     struct lnode *tail;
  19. };
  20.  
  21.  
  22. /* Append the value to the end of the linked list */
  23. void list_add(lst, value)
  24.     struct list *lst;
  25.     int value;
  26. {
  27.     /*1. Allocate the new node in the heap. */
  28.     struct lnode* newNode;
  29.     newNode = malloc(sizeof(struct lnode));
  30.     /*2. Set its value to value argument */
  31.     newNode->value = value;
  32.     /*3. Make the next pointer point to NULL, it's at the end of our list */
  33.     newNode->next = NULL;
  34.     /* Check if it's the first node in list */
  35.     if (lst->head == NULL) {
  36.         lst->head = newNode;
  37.         lst->tail = newNode;
  38.     /* If it's not, add it to the end of the list */
  39.     } else {
  40.         lst->tail->next = newNode;
  41.         lst->tail = newNode;
  42.     }
  43. }
  44. /* Search for the user-provided value in the given user-provided linked list */
  45. struct lnode* list_find(lst, value)
  46.     struct list *lst;
  47.     int value;
  48. {
  49.     /* Create a pointer to a current node */
  50.     struct lnode *cur;
  51.  
  52.     /* Iterate through the list searching for user-provided value */
  53.     for (cur = lst->head; cur != NULL; cur = cur->next) {
  54.         /* If we find the value, return the lnode */
  55.         if (cur->value == value) {
  56.             return cur;
  57.         }
  58.     }
  59.     return NULL;
  60. }
  61.  
  62. void list_remove(lst, value)
  63.     struct list *lst;
  64.     int value;
  65. {
  66.     /* Remove the value from the linked list. */
  67.  
  68.     /* Search the linked list until you find the value */
  69.     /* Need to keep track of the previous node so we can update the links */
  70.     struct lnode* cur;
  71.     struct lnode* prev;
  72.  
  73.     /* Initialize prev */
  74.     prev = NULL;
  75.  
  76.     for (cur = lst->head; cur != NULL; cur = cur->next) {
  77.         /* Value found */
  78.         if (cur->value == value) {
  79.             /* Value to be removed is at front of list */
  80.             if (prev == NULL) {
  81.                 lst->head = cur->next;
  82.             } else {
  83.                 prev->next = cur->next;
  84.             }
  85.             free(cur);
  86.             /* deleted the value, end the loop */
  87.             break;
  88.         } else {
  89.         /* Value not found */
  90.         prev = cur;
  91.         }
  92.     }
  93. }
  94.  
  95. void list_dump(lst)
  96.     struct list *lst;
  97. {
  98.     struct lnode *cur;
  99.     printf("\nDump:\n");
  100.     for(cur=lst->head; cur != NULL; cur=cur->next) {
  101.         printf(" %d\n", cur->value);
  102.     }
  103. };
  104.  
  105. int main()
  106. {
  107.     void list_add();
  108.     void list_dump();
  109.     void list_remove();
  110.     struct lnode * list_find();
  111.  
  112.     struct list mylist;
  113.     struct lnode * mynode;
  114.  
  115.     mylist.head = NULL;
  116.     mylist.tail = NULL;
  117.  
  118.     list_add(&mylist, 10);
  119.     list_add(&mylist, 20);
  120.     list_add(&mylist, 30);
  121.     list_dump(&mylist);
  122.    
  123.     list_remove(&mylist, 42);
  124.  
  125.     list_remove(&mylist, 10);
  126.     list_dump(&mylist);
  127.  
  128.     list_remove(&mylist, 30);
  129.     list_dump(&mylist);
  130.  
  131.     list_add(&mylist, 40);
  132.     list_dump(&mylist);
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment