Open In App

Stack - Linked List Implementation

Last Updated : 13 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. It can be implemented using a linked list, where each element of the stack is represented as a node. The head of the linked list acts as the top of the stack.

Declaration of Stack using Linked List

A stack can be implemented using a linked list where we maintain:

  • A Node structure/class that contains:
    data → to store the element.
    next → pointer/reference to the next node in the stack.
  • A pointer/reference top that always points to the current top node of the stack.
    Initially, top = null to represent an empty stack.
C++
// Node structure
class Node {
public:
    int data;
    Node* next;
    
    Node(int x) {
        data = x;
        next = NULL;
    }
};

// Stack class
class myStack {
    
    // pointer to top node
    Node* top;  
    
public:
    myStack() {
        
        // initially stack is empty
        top = NULL;   
    }
};
Java
/* Node structure */
class Node {
    public int data;
    public Node next;

    public Node(int x) {
        data = x;
        next = null;
    }
}

/* Stack class */
class MyStack {

    // pointer to top node
    private Node top;

    public MyStack() {
        // initially stack is empty
        top = null;
    }
}
Python
# Node structure
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None


# Stack class
class myStack:
    def __init__(self):
        
        # initially stack is empty
        self.top = None
C#
/* Node structure */
public class Node {
    public int data { get; set; }
    public Node next { get; set; }

    public Node(int x) {
        data = x;
        next = null;
    }
}

/* Stack class */
public class myStack {
    
    // pointer to top node
    private Node top;

    public myStack() {
        
        // initially stack is empty
        top = null;
    }
}
JavaScript
/* Node structure */
class Node {
    constructor(x) {
        this.data = x;
        this.next = null;
    }
}

/* Stack class */
class myStack {
    
    // pointer to top node
    top = null;

    constructor() {
        // initially stack is empty
    }
}

Operations on Stack using Linked List

Push Operation

Adds an item to the stack. Unlike array implementation, there is no fixed capacity in linked list. Overflow occurs only when memory is exhausted.

  •  A new node is created with the given value.
  • The new node’s next pointer is set to the current top.
  • The top pointer is updated to point to this new node.
Push-elements-in-the-stack-1


C++
void push(int x) {
    Node* temp = new Node(x);
    temp->next = top;
    top = temp;
}
Java
void push(int x) {
    Node temp = new Node(x);
    temp.next = top;
    top = temp;
}
Python
def push(self, x):
    temp = Node(x)
    temp.next = self.top
    self.top = temp
C#
void push(int x) {
    Node temp = new Node(x);
    temp.next = top;
    top = temp;
}
JavaScript
function push(x) {
    let temp = new Node(x);
    temp.next = top;
    top = temp;
}

Time Complexity: O(1)
Auxiliary Space: O(1)

Pop Operation

Removes the top item from the stack. If the stack is empty, it is said to be an Underflow condition.

  • Before deleting, we check if the stack is empty (top == NULL).
  • If the stack is empty, underflow occurs and deletion is not possible.
  • Otherwise, we store the current top node in a temporary pointer.
  • Move the top pointer to the next node.
  • Delete the temporary node to free memory.
Pop-elements-from-the-stack


C++
int pop() {

    if (top == NULL) {
        cout << "Stack Underflow" << endl;
        return -1;
    }

    Node* temp = top;
    top = top->next;
    int val = temp->data;

    delete temp;
    return val;
}
Java
public int pop() {
  
    if (top == null) {
        System.out.println("Stack Underflow");
        return -1;
    }

    Node temp = top;
    top = top.next;
    int val = temp.data;

    temp = null; 
    return val;
}
Python
def pop(self):
   
    if self.top is None:
        print("Stack Underflow")
        return -1

    temp = self.top
    self.top = self.top.next
    val = temp.data

    del temp
    return val
C#
int pop() {
    
    if (top == null) {
        Console.WriteLine("Stack Underflow");
        return -1;
    }

    Node temp = top;
    top = top.next;
    int val = temp.data;

    temp = null;
    return val;
}
JavaScript
function pop() {
    
    if (this.top === null) {
        console.log('Stack Underflow');
        return -1;
    }

    let temp = this.top;
    this.top = this.top.next;
    let val = temp.data;

    temp = null;
    return val;
}

Time Complexity: O(1)
Auxiliary Space: O(1)

Peek (or Top) Operation

Returns the value of the top item without removing it from the stack.

  • If the stack is empty (top == NULL), then no element exists.
  • Otherwise, simply return the data of the node pointed by top.
C++
int peek() {

    if (top == NULL) {
        cout << "Stack is Empty" << endl;
        return -1;
    }

    return top->data;
}
Java
int peek() {
   
    if (top == null) {
        System.out.println("Stack is Empty");
        return -1;
    }
   
    return top.data;
}
Python
def peek(self):
 
    if self.top is None:
        print("Stack is Empty")
        return -1
 
    return self.top.data
C#
int peek() {
   
    if (top == null) {
        Console.WriteLine("Stack is Empty");
        return -1;
    }
   
    return top.data;
}
JavaScript
function peek() {
  
    if (top === null) {
        console.log('Stack is Empty');
        return -1;
    }
  
    return top.data;
}

Time Complexity: O(1)
Auxiliary Space: O(1)

isEmpty Operation

Checks whether the stack has no elements.

  • If the top pointer is NULL, it means the stack is empty and the function returns true.
  • Otherwise, it returns false.
C++
bool isEmpty() {
    return top == NULL;
}
Java
boolean isEmpty() {
    return top == null;
}
Python
def isEmpty(self):
    return self.top is None
C#
bool isEmpty() {
    return top == null;
}
JavaScript
function isEmpty() {
    return top === null;
}

Time Complexity: O(1)
Auxiliary Space: O(1)

Stack Implementation using Linked List

C++
#include <iostream>
using namespace std;

// Node structure
class Node {
public:
    int data;
    Node* next;
    
    Node(int x) {
        data = x;
        next = NULL;
    }
};

// Stack implementation using linked list
class myStack {
    Node* top;
    
    // To Store current size of stack
    int count;
    
public:
    myStack() {
        
        // initially stack is empty
        top = NULL;
        count = 0;
    }

    // push operation
    void push(int x) {
        Node* temp = new Node(x);
        temp->next = top;
        top = temp;
        
        count++;
    }

    // pop operation
    int pop() {
        if (top == NULL) {
            cout << "Stack Underflow" << endl;
            return -1;
        }
        Node* temp = top;
        top = top->next;
        int val = temp->data;
        
        count--;
        delete temp;
        return val;
    }

    // peek operation
    int peek() {
        if (top == NULL) {
            cout << "Stack is Empty" << endl;
            return -1;
        }
        return top->data;
    }

    // check if stack is empty
    bool isEmpty() {
        return top == NULL;
    }

    // size of stack
    int size() {
        return count;
    }
};

int main() {
    myStack st;

    // pushing elements
    st.push(1);
    st.push(2);
    st.push(3);
    st.push(4);

    // popping one element
    cout << "Popped: " << st.pop() << endl;

    // checking top element
    cout << "Top element: " << st.peek() << endl;

    // checking if stack is empty
    cout << "Is stack empty: " << (st.isEmpty() ? "Yes" : "No") << endl;

    // checking current size
    cout << "Current size: " << st.size() << endl;

    return 0;
}
Java
// Node structure
class Node {
    int data;
    Node next;

    Node(int x) {
        data = x;
        next = null;
    }
}

// Stack implementation using linked list
class myStack {
    Node top;

    // To Store current size of stack
    int count;

    myStack() {
        // initially stack is empty
        top = null;
        count = 0;
    }

    // push operation
    void push(int x) {
        Node temp = new Node(x);
        temp.next = top;
        top = temp;

        count++;
    }

    // pop operation
    int pop() {
        if (top == null) {
            System.out.println("Stack Underflow");
            return -1;
        }
        Node temp = top;
        top = top.next;
        int val = temp.data;

        count--;
        return val;
    }

    // peek operation
    int peek() {
        if (top == null) {
            System.out.println("Stack is Empty");
            return -1;
        }
        return top.data;
    }

    // check if stack is empty
    boolean isEmpty() {
        return top == null;
    }

    // size of stack
    int size() {
        return count;
    }

    public static void main(String[] args) {
        myStack st = new myStack();

        // pushing elements
        st.push(1);
        st.push(2);
        st.push(3);
        st.push(4);

        // popping one element
        System.out.println("Popped: " + st.pop());

        // checking top element
        System.out.println("Top element: " + st.peek());

        // checking if stack is empty
        System.out.println("Is stack empty: " + (st.isEmpty() ? "Yes" : "No"));

        // checking current size
        System.out.println("Current size: " + st.size());
    }
}
Python
# Node structure
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None

# Stack implementation using linked list
class myStack:
    def __init__(self):
        # initially stack is empty
        self.top = None
        self.count = 0

    # push operation
    def push(self, x):
        temp = Node(x)
        temp.next = self.top
        self.top = temp

        self.count += 1

    # pop operation
    def pop(self):
        if self.top is None:
            print("Stack Underflow")
            return -1
        temp = self.top
        self.top = self.top.next
        val = temp.data

        self.count -= 1
        return val

    # peek operation
    def peek(self):
        if self.top is None:
            print("Stack is Empty")
            return -1
        return self.top.data

    # check if stack is empty
    def isEmpty(self):
        return self.top is None

    # size of stack
    def size(self):
        return self.count


if __name__ == "__main__":
    st = myStack()

    # pushing elements
    st.push(1)
    st.push(2)
    st.push(3)
    st.push(4)

    # popping one element
    print("Popped:", st.pop())

    # checking top element
    print("Top element:", st.peek())

    # checking if stack is empty
    print("Is stack empty:", "Yes" if st.isEmpty() else "No")

    # checking current size
    print("Current size:", st.size())
C#
using System;

// Node structure
class Node {
    public int data;
    public Node next;

    public Node(int x) {
        data = x;
        next = null;
    }
}

// Stack implementation using linked list
class myStack {
    Node top;

    // To Store current size of stack
    int count;

    public myStack() {
        // initially stack is empty
        top = null;
        count = 0;
    }

    // push operation
    public void push(int x) {
        Node temp = new Node(x);
        temp.next = top;
        top = temp;

        count++;
    }

    // pop operation
    public int pop() {
        if (top == null) {
            Console.WriteLine("Stack Underflow");
            return -1;
        }
        Node temp = top;
        top = top.next;
        int val = temp.data;

        count--;
        return val;
    }

    // peek operation
    public int peek() {
        if (top == null) {
            Console.WriteLine("Stack is Empty");
            return -1;
        }
        return top.data;
    }

    // check if stack is empty
    public bool isEmpty() {
        return top == null;
    }

    // size of stack
    public int size() {
        return count;
    }

    static void Main(string[] args) {
        myStack st = new myStack();

        // pushing elements
        st.push(1);
        st.push(2);
        st.push(3);
        st.push(4);

        // popping one element
        Console.WriteLine("Popped: " + st.pop());

        // checking top element
        Console.WriteLine("Top element: " + st.peek());

        // checking if stack is empty
        Console.WriteLine("Is stack empty: " + (st.isEmpty() ? "Yes" : "No"));

        // checking current size
        Console.WriteLine("Current size: " + st.size());
    }
}
JavaScript
// Node structure
class Node {
    constructor(x) {
        this.data = x;
        this.next = null;
    }
}

// Stack implementation using linked list
class myStack {
    constructor() {
        // initially stack is empty
        this.top = null;
        this.count = 0;
    }

    // push operation
    push(x) {
        let temp = new Node(x);
        temp.next = this.top;
        this.top = temp;

        this.count++;
    }

    // pop operation
    pop() {
        if (this.top === null) {
            console.log("Stack Underflow");
            return -1;
        }
        let temp = this.top;
        this.top = this.top.next;
        let val = temp.data;

        this.count--;
        return val;
    }

    // peek operation
    peek() {
        if (this.top === null) {
            console.log("Stack is Empty");
            return -1;
        }
        return this.top.data;
    }

    // check if stack is empty
    isEmpty() {
        return this.top === null;
    }

    // size of stack
    size() {
        return this.count;
    }
}

// Driver code
let st = new myStack();

// pushing elements
st.push(1);
st.push(2);
st.push(3);
st.push(4);

// popping one element
console.log("Popped:", st.pop());

// checking top element
console.log("Top element:", st.peek());

// checking if stack is empty
console.log("Is stack empty:", st.isEmpty() ? "Yes" : "No");

// checking current size
console.log("Current size:", st.size());

Output
Popped: 4
Top element: 3
Is stack empty: No
Current size: 3


Advantages-of-Stack-using-Linked-List

Linked List Implementation of Stack in C++
Visit Course explore course icon
Video Thumbnail

Linked List Implementation of Stack in C++

Video Thumbnail

Linked List Implementation of Stack in Java

Video Thumbnail

Linked List Implementation of Stack in Python

Explore