4.2Stack with a Linked List
Push: adding an element
push puts a new element on top of the stack. In a stack built on a linked list the top is the head of the list, so push is the same as inserting at the head. Only the names differ: top instead of head, and the function takes a struct Stack.
- In which order the new node is linked into the stack
- Why a linked-list stack never gets full, and when push can still fail
- How push is written in C, Java and Python, and why it is O(1)
1Idea
First a new node is allocated and its value is written. Then the new node's next field points at the old top. Finally top is moved to the new node. The rest of the stack is not touched.
The order matters. If you change top first, you lose the address of the old top and every older element in the stack becomes unreachable.
In the array version, push first checks whether the stack is full. There is no such limit here. In C, push fails only when malloc returns NULL, that is, when memory runs out, and then it returns false. In Java and Python the language throws its own error if memory runs out.
You do not need a special case for pushing onto an empty stack. If top is NULL, the new node's next becomes NULL and you get a correct one-node stack.
2Step by step
The drawing pushes 30 onto a stack holding 20 and 10. The top is on the left, at the head of the list. At each step the matching line of code lights up.
- Start. top points to 20. 10 is at the bottom.
- Memory is allocated for the new node. In C, if
mallocdid not return NULL, 30 is written into it. The node is not linked to the stack yet. - The value of
topis written into the new node's next field. The new node now points to the old top (20). topis moved to the new node. 30 is now on top.- Result: from top to bottom 30, 20, 10. In C the function returns
true.
struct Node { int data; struct Node* next;}; struct Stack { struct Node* top;}; bool push(struct Stack* s, int value) { struct Node* node = malloc(sizeof(struct Node)); if (node == NULL) return false; // out of memory node->data = value; node->next = s->top; s->top = node; return true;}class Node { int data; Node next; Node(int data) { this.data = data; }} class LinkedStack { Node top = null; void push(int value) { Node node = new Node(value); node.next = top; top = node; // the new top }}class Node: def __init__(self, data): self.data = data self.next = None class LinkedStack: def __init__(self): self.top = None def push(self, value): node = Node(value) node.next = self.top self.top = node # the new topThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Independent of the number of elements: one allocation and two assignments.
Space is allocated only for the new node.
Push on an array stack is O(1) too, but nothing can be added once the array is full. A linked list has no such limit.
4Common mistake
The most common mistake is mixing up the order of the two assignments:
s->top = node;node->next = s->top; // the node points to itselfAfter the first line s->top already points to the new node. The second line links the new node to itself and the address of the old top is lost. First node->next, then s->top.
5Try it
When does push return false in C?
The answer is B. A linked-list stack has no fixed capacity. If no memory can be allocated for the new node, the push cannot happen; that is the only way it fails.
Show the answer
The answer is B. A linked-list stack has no fixed capacity. If no memory can be allocated for the new node, the push cannot happen; that is the only way it fails.
If you push 5 onto an empty stack (top = NULL), what does the new node's next field hold?
The answer is B. The line node->next = s->top copies the value of top, which is NULL. In a one-node stack the top is also the bottom and its next is NULL.
Show the answer
The answer is B. The line node->next = s->top copies the value of top, which is NULL. In a one-node stack the top is also the bottom and its next is NULL.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.