The effect of addLast(h,x) --- inserting item x at the end of the linked list h:
|
|
|
|
Let's implement the addLast(h, x) method:
struct Node *addLast(struct Node *h, int x) { struct Node *newNode = malloc( sizeof(struct Node) ); newNode->item = x; if ( h == NULL ) { return newNode; // Return the new firstnode } struct Node *current = h; while ( current->next != NULL ) // Find last list element { current = current->next; } current->next = newNode; // Link new node after last node return h; // Return the new firstnode } |
Start the search at the first node in the linked list:
struct Node *addLast(struct Node *h, int x) { struct Node *newNode = malloc( sizeof(struct Node) ); newNode->item = x; if ( h == NULL ) { return newNode; // Return the new firstnode } struct Node *current = h; // h = first node in list while ( current->next != NULL ) // Find last list element { current = current->next; } current->next = newNode; // Link new node after last node return h; // Return the new firstnode } |
Find the last element in the linked list: the next variable in this element is NULL
struct Node *addLast(struct Node *h, int x)
{
struct Node *newNode = malloc( sizeof(struct Node) );
newNode->item = x;
if ( h == NULL )
{
return newNode; // Return the new firstnode
}
struct Node *current = h; // h = first node in list
while ( current->next != NULL ) // Exit loop when last list elem found
{
current = current->next;
}
current->next = newNode; // Link new node after last node
return h; // Return the new firstnode
}
|
Make a new Node containing the item = x and next = NULL (because it will be the last element)
struct Node *addLast(struct Node *h, int x) { struct Node *newNode = malloc( sizeof(struct Node) ); newNode->item = x; newNode->next = NULL; if ( h == NULL ) { return newNode; // Return the new firstnode } struct Node *current = h; // h = first node in list while ( current->next != NULL ) // Exit loop when last list elem found { current = current->next; } current->next = newNode; // Link new node after last node return h; // Return the new firstnode } |
Link the new Node to the last element in the list:
struct Node *addLast(struct Node *h, int x) { struct Node *newNode = malloc( sizeof(struct Node) ); newNode->item = x; newNode->next = NULL; if ( h == NULL ) { return newNode; // Return the new firstnode } struct Node *current = h; // h = first node in list while ( current->next != NULL ) // Exit loop when last list elem found { current = current->next; } current->next = newNode; // Link new node after last node return h; // Return the new firstnode } |
Return the new first element:
struct Node *addLast(struct Node *h, int x) { struct Node *newNode = malloc( sizeof(struct Node) ); newNode->item = x; newNode->next = NULL; if ( h == NULL ) { return newNode; // Return the new firstnode } struct Node *current = h; // h = first node in list while ( current->next != NULL ) // Exit loop when last list elem found { current = current->next; } current->next = newNode; // Link new node after last node return h; // Return the new first node } |
Check for edge case(s): an empty list
struct Node *addLast(struct Node *h, int x) { struct Node *newNode = malloc( sizeof(struct Node) ); newNode->item = x; newNode->next = NULL; if ( h == NULL ) { return newNode; // Return the new firstnode } struct Node *current = h; // Edge case: h = NULL while ( current->next != NULL ) // null->next will cause a crash !! { current = current->next; } current->next = newNode; // Link new node after last node return h; // Return the new first node } |
We must handle the edge case separately:
struct Node *addLast(struct Node *h, int x) { struct Node *newNode = malloc( sizeof(struct Node) ); newNode->item = x; newNode->next = NULL; if ( h == NULL ) { return newNode; // Return the new first node } struct Node *current = h; // h != NULL while ( current->next != NULL ) { current = current->next; } current->next = newNode; // Link new node after last node return h; // Return the new first node } |
struct Node { int item; struct Node *next; }; int main() { struct Node *head = NULL; int k; printList(head); for (k = 1; k < 10; k++ ) { head = addLast(head, k); printList(head); } } |
DEMO: demo/C/Linked-list/addLast.c