Last Updated : 01 Aug, 2025
Try it on GfG Practice
To implement a stack using a singly linked list, we follow the LIFO (Last In, First Out) principle by inserting and removing elements from the head of the list, where each node stores data and a pointer to the next node.
In the stack Implementation, a stack contains a top pointer. which is the "head" of the stack where pushing and popping items happens at the head of the list. The first node has a null in the link field and second node-link has the first node address in the link field and so on and the last node address is in the "top" pointer.
Pop Operation
- Initialise a node
- Update the value of that node by data i.e. node->data = data
- Now link this node to the top of the linked list
- And update top pointer to the current node
Peek Operation
- First Check whether there is any node present in the linked list or not, if not then return
- Otherwise make pointer let say temp to the top node and move forward the top node by 1 step
- Now free this temp node
Display Operation
- Check if there is any node present or not, if not then return.
- Otherwise return the value of top node of the linked list
C++
- Take a temp node and initialize it with top pointer
- Now start traversing temp till it encounters NULL
- Simultaneously print the value of the temp node
#include <bits/stdc++.h>
using namespace std;
// Node structure
class Node {
public:
int data;
Node* next;
Node(int new_data) {
this->data = new_data;
this->next = nullptr;
}
};
// Stack using linked list
class Stack {
Node* head;
public:
Stack() {
this->head = nullptr;
}
// Check if stack is empty
bool isEmpty() {
return head == nullptr;
}
// Push an element onto stack
void push(int new_data) {
Node* new_node = new Node(new_data);
new_node->next = head;
head = new_node;
}
// Pop the top element
void pop() {
if (isEmpty()) return;
Node* temp = head;
head = head->next;
delete temp;
}
// Return the top element
int peek() {
if (!isEmpty()) return head->data;
return INT_MIN;
}
};
int main() {
Stack st;
st.push(11);
st.push(22);
st.push(33);
st.push(44);
cout << st.peek() << endl;
st.pop();
st.pop();
cout << st.peek() << endl;
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
// Check if stack is empty
int isEmpty(struct Node* head) {
return head == NULL;
}
// Push an element onto stack
void push(struct Node** head, int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = *head;
*head = new_node;
}
// Pop the top element
void pop(struct Node** head) {
if (isEmpty(*head)) return;
struct Node* temp = *head;
*head = (*head)->next;
free(temp);
}
// Return the top element
int peek(struct Node* head) {
if (!isEmpty(head)) return head->data;
return INT_MIN;
}
int main() {
struct Node* head = NULL;
push(&head, 11);
push(&head, 22);
push(&head, 33);
push(&head, 44);
printf("%d\n", peek(head));
pop(&head);
pop(&head);
printf("%d\n", peek(head));
return 0;
}
Java
class Node {
int data;
Node next;
Node(int new_data) {
this.data = new_data;
this.next = null;
}
}
// Stack using linked list
class Stack {
Node head;
Stack() {
this.head = null;
}
// Check if stack is empty
boolean isEmpty() {
return head == null;
}
// Push an element onto stack
void push(int new_data) {
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
// Pop the top element
void pop() {
if (isEmpty()) return;
head = head.next;
}
// Return the top element
int peek() {
if (!isEmpty()) return head.data;
return Integer.MIN_VALUE;
}
}
public class Main {
public static void main(String[] args) {
Stack st = new Stack();
st.push(11);
st.push(22);
st.push(33);
st.push(44);
System.out.println(st.peek());
st.pop();
st.pop();
System.out.println(st.peek());
}
}
Python
# Node structure
class Node:
def __init__(self, new_data):
self.data = new_data
self.next = None
# Stack using linked list
class Stack:
def __init__(self):
self.head = None
# Check if stack is empty
def isEmpty(self):
return self.head is None
# Push an element onto stack
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
# Pop the top element
def pop(self):
if self.isEmpty():
return
self.head = self.head.next
# Return the top element
def peek(self):
if not self.isEmpty():
return self.head.data
return float('-inf')
st = Stack()
st.push(11)
st.push(22)
st.push(33)
st.push(44)
print(st.peek())
st.pop()
st.pop()
print(st.peek())
C#
using System;
// Node structure
class Node {
public int data;
public Node next;
public Node(int new_data) {
this.data = new_data;
this.next = null;
}
}
// Stack using linked list
class Stack {
Node head;
public Stack() {
this.head = null;
}
// Check if stack is empty
public bool isEmpty() {
return head == null;
}
// Push an element onto stack
public void push(int new_data) {
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
// Pop the top element
public void pop() {
if (isEmpty()) return;
head = head.next;
}
// Return the top element
public int peek() {
if (!isEmpty()) return head.data;
return int.MinValue;
}
}
class Program {
static void Main() {
Stack st = new Stack();
st.push(11);
st.push(22);
st.push(33);
st.push(44);
Console.WriteLine(st.peek());
st.pop();
st.pop();
Console.WriteLine(st.peek());
}
}
JavaScript
// Node structure
class Node {
constructor(new_data) {
this.data = new_data;
this.next = null;
}
}
// Stack using linked list
class Stack {
constructor() {
this.head = null;
}
// Check if stack is empty
isEmpty() {
return this.head === null;
}
// Push an element onto stack
push(new_data) {
let new_node = new Node(new_data);
new_node.next = this.head;
this.head = new_node;
}
// Pop the top element
pop() {
if (this.isEmpty()) return;
this.head = this.head.next;
}
// Return the top element
peek() {
if (!this.isEmpty()) return this.head.data;
return Number.MIN_SAFE_INTEGER;
}
}
let st = new Stack();
st.push(11);
st.push(22);
st.push(33);
st.push(44);
console.log(st.peek());
st.pop();
st.pop();
console.log(st.peek());
Time Complexity: O(1), for all push(), pop(), and peek(), as we are not performing any kind of traversal over the list.
Auxiliary Space: O(n), where n is the size of the stack
Stacks are used in various real-world scenarios where a last-in, first-out (LIFO) data structure is required. Here are some examples of real-time applications of stacks:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4