Advertisement
bocajbee

llmore2.c

May 23rd, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cs50.h>
  4.  
  5. typedef struct node  // custom struct for each node in the linked list
  6. {
  7.     int number;  // int of the node
  8.     struct node *next;  // pointer to next node
  9. }
  10. node;
  11.  
  12. node* create_node(int);  // function prototypes
  13. void add_node(node *list, node *n);
  14. void print_list(node *list);
  15. node* delete_node(node *list);
  16. node* insert_node(node *list);
  17. void free_memory(node *list);
  18.  
  19. int main(void)
  20. {
  21.     int input = get_int("Please enter how many nodes you would like to create in the linked list? \n");
  22.     node *list = NULL;  // list pointer variable, anticipating to be pointed at something of "node" datatype, initalize it to NULL so its not pointing to anything
  23.  
  24.     if (input != 0)
  25.     {
  26.         node *n = malloc(sizeof(node));  // malloc() "node 0" in memory + point our *n pointer to it
  27.         if (n != NULL)  // saftey check to ensure n is not pointing to a NULL value in memory to avoid a seg fault
  28.         {
  29.             n->number = 0;  // set the number field of the node our "*n" is currently pointing to: 2
  30.             n->next = NULL;   // set the next field of the node our "*n" is currently pointing to: NULL
  31.         }
  32.         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)
  33.  
  34.         for (int i = 1; i < input; i++) // start counting i at 1, since we statically declared the first node as "0"
  35.         {
  36.             n = create_node(i);  // use create_node to create a new node + also take the pointer to a node this function returns & assign it back to n
  37.             add_node(list, n);  // connect the "nth" node created() above, to the existing linked list
  38.         }
  39.  
  40.         print_list(list);
  41.         list = delete_node(list);
  42.         print_list(list);
  43.         list = insert_node(list);
  44.         print_list(list);
  45.         free_memory(list);
  46.         return 0;
  47.     }
  48. }
  49.  
  50. 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
  51. {
  52.     node *n = malloc(sizeof(node)); // a new n pointer that will malloc "node i" in memory + point our "*n" pointer to this node
  53.     if (n != NULL)  // saftey check to ensure n is not pointing to a NULL value in our memory
  54.     {
  55.         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)
  56.         n->next = NULL;  // set the next field of the node our "*n" is currently pointing to: NULL
  57.     }
  58.     return n;
  59. }
  60.  
  61. 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)
  62. {
  63.     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
  64.     while (tmp->next != NULL) // every time when looping through the linked list, if the current node tmp is  sitting on isn't NULL
  65.     {
  66.         tmp = tmp->next; // then migrate the temp pointer to the next node in the linked list
  67.     }
  68.     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.
  69. }
  70.  
  71. void print_list(node *list) // takes in paramater for the list pointer (that is anticipating to point to a node)
  72. {
  73.     printf("Numbers in linked list:\n");
  74.     node *tmp = list; // make a temp pointer that will point to the head of the linked list
  75.  
  76.     if (list == NULL)  // if the list pointer variable pointing to the head of thelinked list is NULL
  77.     {
  78.         printf("The linked list is empty\n");
  79.     }
  80.     else
  81.     {
  82.         while (tmp != NULL)  // keep performing this while loop until temp is not longer pointing to a node after in the linked list
  83.         {
  84.             printf("%d\n", tmp->number);  // print out the current nodes.number where tmp is pointing to
  85.             tmp = tmp->next;  // then after this, update temp to point to the next node in the linked list to perform this action again
  86.         }
  87.     }
  88.     printf("\n");
  89. }
  90.  
  91. node* delete_node(node *list)
  92. {
  93.     int input = get_int("Please enter the number of a node you would like to delete from the linked list?\n");
  94.     node *tmp_before = list;  // make a temp pointer that will point to the head of the linked list
  95.  
  96.     if (list == NULL)  // if the list pointer variable pointing to the head of the linked list is NULL
  97.     {
  98.         printf("Invalid linked list");
  99.     }
  100.     else
  101.     {
  102.         bool bool_var = true;
  103.         while (bool_var == true)
  104.         {
  105.             if (tmp_before == NULL)
  106.             {
  107.                 printf("Your linked list does not have any nodes to delete, goodbye:\n");
  108.                 bool_var = false;
  109.             }
  110.  
  111.             if (input == 0)  // handle the deleation of the first node in the linked list
  112.             {
  113.                 if (tmp_before->next != NULL)
  114.                 {
  115.                     list = tmp_before->next;  // set the list pointer to point to where tmp_before->next is pointing to
  116.                     free(tmp_before);  // free the node tmp_before is currently sitting on
  117.                     bool_var = false;
  118.                     break;
  119.                 }
  120.                 else // tmp_before->next == NULL)
  121.                 {
  122.                     free(tmp_before); // just free the node tmp_before is currently sitting on if we know there are no nodes after we will end up orphaning by mistake
  123.                     bool_var = false;
  124.                     break;
  125.                 }
  126.             }
  127.  
  128.             for (int i = 0; i < input - 1; i++)  // keep looping until we hit the value of the users input -1
  129.             {
  130.                 if (tmp_before->next == NULL) // if the nth node we are trying to delete doesn't exist
  131.                 {
  132.                     printf("The node you are trying to delete doesn't exist in the linked list, goodbye.\n");
  133.                     bool_var = false;
  134.                 }
  135.                 tmp_before = tmp_before->next;  // set tmp_before on thisiteration of this loop to == tmp_before->next of the previous loop.
  136.             }
  137.  
  138.             node *tmp = tmp_before->next;  // create a second tempoary pointer variable to sit on the "nth" node
  139.  
  140.             if (tmp->next != NULL)  // if the node after temp actually points to another node afterwards
  141.             {
  142.                 tmp_before->next = tmp->next;  // point the pointer 1. from "temp_before.next" to 2. the node after temp, where "temp.next" is pointing to
  143.                 free(tmp);  // free the node after it is safe to do so without orphaning the rest of the linked list
  144.                 bool_var = false;
  145.                 break;
  146.             }
  147.             else  // https://www.tutorialspoint.com/c_standard_library/c_function_free.htm
  148.             {
  149.                 tmp_before->next = NULL;  // set the last node of the linked ist to NULL before we free it(). if we dont do this, after we free() it tmp_before->next will point to the "0" free() leaves in this block of memory tmp used to occupy
  150.                 free(tmp);
  151.                 bool_var = false;
  152.                 break;
  153.             }
  154.         }
  155.     }
  156.     return list;
  157.     printf("\n");
  158. }
  159.  
  160. node* insert_node(node *list)
  161. {
  162.     int input = get_int("Please enter the place of a node# you would like to insert a node into after? \n");
  163.     node *tmp_before = list;
  164.  
  165.     node *yeet = malloc(sizeof(node)); // a new n pointer that will malloc "node i" in memory + point our "*n" pointer to this node
  166.     if (yeet != NULL)  // saftey check to ensure n is not pointing to a NULL value in our memory
  167.     {
  168.         yeet->number = 420;
  169.         yeet->next = NULL;
  170.     }
  171.  
  172.     if (list == NULL)
  173.     {
  174.         printf("Invalid linked list to insert into");
  175.     }
  176.     else
  177.     {
  178.         bool bool_var = true;
  179.         while (bool_var == true)
  180.         {
  181.             if (tmp_before == NULL)
  182.             {
  183.                 printf("Your linked list does not have any nodes to insert into, goodbye");
  184.                 bool_var = false;
  185.                 return;
  186.             }
  187.  
  188.             for (int i = 0; i < input; i++)
  189.             {
  190.                 if (tmp_before->next == NULL)
  191.                 {
  192.                     printf("The node you are trying to delete doesn't exist in the linked list, goodbye.\n");
  193.                     bool_var = false;
  194.                     return;
  195.                 }
  196.                 tmp_before = tmp_before->next;  // set tmp_before on thisiteration of this loop to == tmp_before->next of the previous loop.
  197.             }
  198.  
  199.             node *tmp = tmp_before->next;  // create a second tempoary pointer variable to sit on the "nth" node
  200.  
  201.             if (tmp != NULL)  // if the node after temp actually points to another node afterwards
  202.             {
  203.                 yeet->next = tmp;
  204.                 tmp_before->next = yeet;
  205.                 bool_var = false;
  206.                 break;
  207.             }
  208.             else
  209.             {
  210.                 tmp_before->next = yeet;
  211.                 bool_var = false;
  212.                 break;
  213.             }
  214.         }
  215.     }
  216.     return list;
  217.     printf("\n");
  218. }
  219.  
  220. void free_memory(node *list)
  221. {
  222.     node* tmp = list;
  223.  
  224.     while (list != NULL)  // check if list is currently NULL, if so, the list is empty and we just return
  225.     {
  226.         tmp = list;  // save list into a tmp variable
  227.         list = list->next;  // save list into a tmp variable
  228.         free(tmp);  // safely free the node where the (tmp) variable is currently sitting at 1 nod ebehind the new head now, repeat step 1
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement