algoritmauzmanı
Sign in
TREN
4.2Stack with a Linked List
Lesson 4.2.2

Push: adding an element

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.

What you will learn
  • 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
5 minTime O(1)Memory O(1)C · Java · Python

1Idea

First a new is allocated and its value is written. Then the new node's field points at the old top. Finally 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 returns , 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.

NULL30?0x2c1020?0x1a9010?0x1a40topnode
  1. Start. points to 20. 10 is at the bottom.
  2. Memory is allocated for the new node. In C, if malloc did not return NULL, 30 is written into it. The node is not linked to the stack yet.
  3. The value of top is written into the new node's field. The new node now points to the old top (20).
  4. top is moved to the new node. 30 is now on top.
  5. 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 top

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(1)

Independent of the number of elements: one allocation and two assignments.

Memory
O(1)

Space is allocated only for the new node.

Push on an array stack is 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 itself

After 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?

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?

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.