Advertisement
bocajbee

linkedlist_dynamic.c

May 18th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cs50.h>
  4.  
  5. typedef struct node  // define our own custom struct for each node in the linked list
  6. {
  7.     int number;
  8.     struct node *next;
  9. }
  10. node;
  11.  
  12. node* create_node(int);  // create node handles the malloc line, checking if it's not null and returns the completed node
  13. void add_node(node *list, node *n);  // And add node loops through the list until it reaches the end and adds the new node to that point
  14. void print_list(node *list);
  15. void free_memory(node *list);
  16.  
  17. int main(void)
  18. {
  19.     int input = get_int("Please enter how many nodes you would like to create in the linked list? \n");
  20.     node *list = NULL;  // create the list pointer variable, that will anticipate to be pointed to something of a "node" datatype, initalize it to NULL asits not pointing to anything
  21.  
  22.     node *n = malloc(sizeof(node));  // malloc() "node 2" in our memory + point out *n pointer to this node
  23.     if (n != NULL)  // saftey check to ensure n is not pointing to a NULL value in our memory
  24.     {
  25.         n->number = 0;  // set the number field of the node our "*n" is currently pointing to: 2
  26.         n->next = NULL;   // set the next field of the node our "*n" is currently pointing to: NULL
  27.     }
  28.     list = n; // point our "*list" pointer to the same node our "*n" pointer is pointing to (at the start of this program list will be pointing to the head of the linked list)
  29.  
  30.     for (int i = 1; i < input; i++) // start counting i at 1 since we declared the first node as "0"
  31.     {
  32.         n = create_node(i);  // use creat_node to create a new node + also take the pointer to a node this function returns + assign it back to n
  33.         add_node(list, n);
  34.     }
  35.  
  36.     print_list(list);
  37.     free_memory(list);
  38.     return 0;
  39. }
  40.  
  41. node* create_node(int i)  // our function to generate a node with the number we got from "i' in main()" "node 0", then "node 1" , then "node 2" etc
  42. {
  43.     node *n = malloc(sizeof(node)); // a new n pointer that will malloc "node i" in memory + point our "*n" pointer to this node
  44.     if (n != NULL)  // saftey check to ensure n is not pointing to a NULL value in our memory
  45.     {
  46.         n->number = i;  // set the number field of the node our "*n_2" is currently pointing to: whatever "i" is (this draws a line in our linked list)
  47.         n->next = NULL;  // set the next field of the node our "*n" is currently pointing to: NULL
  48.     }
  49.     return n;
  50. }
  51.  
  52. void add_node(node *list, node *n)  // takes in paramaters for the 1. "list" pointer (that is anticipating to point to a node) and the "n" pointer (to point to the "n" node itself)
  53. {
  54.     node *tmp = list; // create a temp pointer, that will anticipate to be poiinted to something of a "node" datatype, and point it to where *tmp is pointing to, drawing a link
  55.     while (tmp->next != NULL) // every time when looping through the linked list, if the current node tmp is  sitting on isn't NULL
  56.     {
  57.         tmp = tmp->next; // then migrate the temp pointer to the next node in the linked list
  58.     }
  59.     tmp->next = n; // then, set tmp->next field in this current node to = n, to point to where *n is pointing to, drawing another link between the temp node and node n after it.
  60. }
  61.  
  62. void print_list(node *list) // takes in paramater for the list pointer (that is anticipating to point to a node)
  63. {
  64.     printf("Numbers in linked list:\n");
  65.     node *tmp = list; // make a temp pointer that will point to the head of the linked list
  66.  
  67.     if (list == NULL)  // if the list pointer variable pointing to the head of thelinked list is NULL
  68.     {
  69.         printf("The linked list is empty\n");
  70.     }
  71.     else
  72.     {
  73.         while (tmp != NULL)  // keep performing this while loop until temp is not longer pointing to a node after in the linked list
  74.         {
  75.             printf("%d\n", tmp->number);  // print out the current nodes.number where tmp is pointing to
  76.             tmp = tmp->next;  // then after this, update temp to point to the next node in the linked list to perform this action again
  77.         }
  78.     }
  79. }
  80.  
  81. void free_memory(node *list)
  82. {
  83.     node* tmp = list;
  84.  
  85.     while (list != NULL)
  86.     {
  87.         tmp = list;
  88.         list = list->next;
  89.         free(tmp);
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement