Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. void append(struct Node** head_ref, int new_data)
  2. {
  3. /* 1. allocate node */
  4. struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
  5.  
  6. struct Node *last = *head_ref; /* used in step 5*/
  7.  
  8. /* 2. put in the data */
  9. new_node->data = new_data;
  10.  
  11. /* 3. This new node is going to be the last node, so make next
  12. of it as NULL*/
  13. new_node->next = NULL;
  14.  
  15. /* 4. If the Linked List is empty, then make the new node as head */
  16. if (*head_ref == NULL)
  17. {
  18. *head_ref = new_node;
  19. return;
  20. }
  21.  
  22. /* 5. Else traverse till the last node */
  23. while (last->next != NULL)
  24. last = last->next;
  25.  
  26. /* 6. Change the next of last node */
  27. last->next = new_node;
  28. return;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement