Advertisement
_Lunar

test_list.c

Apr 19th, 2025
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 KB | Source Code | 0 0
  1. #include "list.h"
  2. #include <stdio.h>
  3.  
  4. // Using the same node structure I defined in list.h
  5. struct my_node {
  6.     struct my_node *next;
  7.     int value;
  8. };
  9.  
  10. int main() {
  11.     LIST(my_list);
  12.  
  13.     // Creating Nodes on the stack
  14.     struct my_node item1, item2, item3;
  15.  
  16.     // Adding Nodes To "my_list"
  17.     list_add(my_list, &item1);
  18.     list_add(my_list, &item2);
  19.     list_push(my_list, &item3);
  20.  
  21.     // Pairing Up Node Memory Addresses with integer values
  22.     list_item_set_value(&item1, 42);
  23.     list_item_set_value(&item2, 100);
  24.     list_item_set_value(&item3, 69);
  25.  
  26.     // Using list_remove()
  27.     list_remove(my_list, &item2);
  28.  
  29.     // Using list_tail()
  30.     printf("list_tail()\n");
  31.     struct my_node *tail_node = (struct my_node *)list_tail(my_list);
  32.     if (tail_node != NULL) {
  33.       printf("Tail Node: %d\n", tail_node->value);
  34.     } else {
  35.         printf("Tail Node: List was empty\n");
  36.     }
  37.  
  38.     // Using list_length()
  39.     printf("List Length: %d\n", list_length(my_list));
  40.  
  41.     // Using list_insert()
  42.     struct my_node new_node;
  43.     list_insert(my_list, &item3, &new_node);
  44.     list_item_set_value(&new_node, 5);
  45.  
  46.     // Using list_length() the second time
  47.     printf("List Length: %d\n", list_length(my_list));
  48.  
  49.     // Using list_contains()
  50.     printf("Contains new_node? : %d\n", list_contains(my_list, &new_node));
  51.  
  52.     // Using list_chop()
  53.     printf("list_chop()\n");
  54.     for (int x = 0; x < 3; x++) {
  55.       struct my_node *chopped_node = (struct my_node *)list_chop(my_list);
  56.       if (chopped_node != NULL) {
  57.           printf("Chopped Node: %d\n", chopped_node->value);
  58.       } else {
  59.           printf("Chopped Node: List was empty\n");
  60.       }
  61.     }
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement