Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void append(struct Node** head_ref, int new_data)
- {
- /* 1. allocate node */
- struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
- struct Node *last = *head_ref; /* used in step 5*/
- /* 2. put in the data */
- new_node->data = new_data;
- /* 3. This new node is going to be the last node, so make next
- of it as NULL*/
- new_node->next = NULL;
- /* 4. If the Linked List is empty, then make the new node as head */
- if (*head_ref == NULL)
- {
- *head_ref = new_node;
- return;
- }
- /* 5. Else traverse till the last node */
- while (last->next != NULL)
- last = last->next;
- /* 6. Change the next of last node */
- last->next = new_node;
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement