4.2Stack with a Linked List
Creating a linked-list stack
An array stack works with a fixed capacity and cannot take new elements once it is full. A stack built on a linked list allocates a separate node for each element, so it grows for as long as memory lasts. In this lesson you set up the structure and write the empty stack and the emptiness check.
- Which fields a linked-list stack has and how an empty stack is represented
- Why the top of the stack is the head of the list, not the end
- How
createandisEmptyare written in C, Java and Python
1Idea
The structure has a single field: top. It is a pointer to the first node of the list; it is exactly the head from the linked-list lessons, only renamed. When the stack is empty, top is NULL. There is no items array and no capacity as in the array version.
Why is the top the head of the list? In a stack all the work happens at one end: push adds at that end and pop removes from it. At the head both take O(1): inserting at the head and deleting from the head touch only the first node.
If we put the top at the end of the list, adding would be quick with a tail pointer, but removing would not. To delete the last node you need the node before it, and in a singly linked list the only way to find it is to traverse from the start. That is why deleting from the end is O(n).
A linked-list stack has no isFull. The stack can only fail to grow when memory runs out, and push finds that out by checking the result of malloc.
2Step by step
The drawing first shows an empty stack, then a stack after 10, 20 and 30 were added in that order. At each step the matching line of code lights up.
createwrites NULL intotop. There are no nodes yet; this is the empty stack.isEmptychecks whethertopis NULL. Right now it is, so the result is true.- The state after three push calls. 30, added last, is at the head of the list and
toppoints to it. - 30 is on top, and 10, added first, is at the bottom, the end of the list. The next push and pop will work only at the end where 30 is.
isEmptynow returns false, becausetoppoints to a node.
struct Node { int data; struct Node* next;}; struct Stack { struct Node* top; // the top node}; void create(struct Stack* s) { s->top = NULL; // empty stack} bool isEmpty(struct Stack* s) { return s->top == NULL;}class Node { int data; Node next; Node(int data) { this.data = data; }} class LinkedStack { Node top = null; // the top node, null when empty boolean isEmpty() { return top == null; }}class Node: def __init__(self, data): self.data = data self.next = None class LinkedStack: def __init__(self): self.top = None # the top node, None when empty def is_empty(self): return self.top is NoneThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Each function does a single assignment or comparison.
An empty stack takes only the space of the top pointer.
Compare: an array stack reserves room for its full capacity up front. A linked-list stack allocates room node by node as elements arrive.
4Common mistake
A common mistake in C is using the stack without giving top a starting value:
struct Stack s;if (isEmpty(&s)) { ... } // s.top is undefinedThe fields of a local variable do not become NULL on their own; they hold some random address. isEmpty gives a wrong answer and the first pop tries to read from that address. Always call create(&s) before using the stack.
5Try it
If the top of a linked-list stack were at the end of the list, which operation would be O(n)?
The answer is B. In a singly linked list you cannot step back from a node. To remove the last node, the node before it is found by walking from the start. With the top at the head this problem does not exist.
Show the answer
The answer is B. In a singly linked list you cannot step back from a node. To remove the last node, the node before it is found by walking from the start. With the top at the head this problem does not exist.
How do you tell that a linked-list stack is empty?
The answer is B. In the linked-list version top is a pointer, not an index. When there is no node to point to, it is NULL. -1 marks an empty stack in the array version.
Show the answer
The answer is B. In the linked-list version top is a pointer, not an index. When there is no node to point to, it is NULL. -1 marks an empty stack in the array version.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.