Stack Linked List Implementation (original) (raw)

Last Updated : 13 Sep, 2025

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:

Try It Yourselfredirect icon

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.

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.

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.

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.

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