To implement a stack using the singly linked list concept, all the singly linked list operations should be performed based on Stack operations LIFO(last in first out) and with the help of that knowledge, we are going to implement a stack using a singly linked list.
So we need to follow a simple rule in the implementation of a stack which is last in first out and all the operations can be performed with the help of a top variable. Let us learn how to perform Pop, Push, Peek, and Display operations in the following article:
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.
The main advantage of using a linked list over arrays is that it is possible to implement a stack that can shrink or grow as much as needed. Using an array will put a restriction on the maximum capacity of the array which can lead to stack overflow. Here each new node will be dynamically allocated. so overflow is not possible.
Below is the implementation of the above operations
C++
// C++ program to Implement a stack
// using singly linked list
#include <bits/stdc++.h>
using namespace std;
// creating a linked list;
class Node {
public:
int data;
Node* link;
// Constructor
Node(int n)
{
this->data = n;
this->link = NULL;
}
};
class Stack {
Node* top;
public:
Stack() { top = NULL; }
void push(int data)
{
// Create new node temp and allocate memory in heap
Node* temp = new Node(data);
// Check if stack (heap) is full.
// Then inserting an element would
// lead to stack overflow
if (!temp) {
cout << "\nStack Overflow";
exit(1);
}
// Initialize data into temp data field
temp->data = data;
// Put top pointer reference into temp link
temp->link = top;
// Make temp as top of Stack
top = temp;
}
// Utility function to check if
// the stack is empty or not
bool isEmpty()
{
// If top is NULL it means that
// there are no elements are in stack
return top == NULL;
}
// Utility function to return top element in a stack
int peek()
{
// If stack is not empty , return the top element
if (!isEmpty())
return top->data;
else
exit(1);
}
// Function to remove
// a key from given queue q
void pop()
{
Node* temp;
// Check for stack underflow
if (top == NULL) {
cout << "\nStack Underflow" << endl;
exit(1);
}
else {
// Assign top to temp
temp = top;
// Assign second node to top
top = top->link;
// This will automatically destroy
// the link between first node and second node
// Release memory of top node
// i.e delete the node
free(temp);
}
}
// Function to print all the
// elements of the stack
void display()
{
Node* temp;
// Check for stack underflow
if (top == NULL) {
cout << "\nStack Underflow";
exit(1);
}
else {
temp = top;
while (temp != NULL) {
// Print node data
cout << temp->data;
// Assign temp link to temp
temp = temp->link;
if (temp != NULL)
cout << " -> ";
}
}
}
};
// Driven Program
int main()
{
// Creating a stack
Stack s;
// Push the elements of stack
s.push(11);
s.push(22);
s.push(33);
s.push(44);
// Display stack elements
s.display();
// Print top element of stack
cout << "\nTop element is " << s.peek() << endl;
// Delete top elements of stack
s.pop();
s.pop();
// Display stack elements
s.display();
// Print top element of stack
cout << "\nTop element is " << s.peek() << endl;
return 0;
}
Output
44 -> 33 -> 22 -> 11
Top element is 44
22 -> 11
Top element is 22
Time Complexity: O(1), for all push(), pop(), and peek(), as we are not performing any kind of traversal over the list. We perform all the operations through the current pointer only.
Auxiliary Space: O(N), where N is the size of the stack
119 docs|30 tests
|
|
Explore Courses for Computer Science Engineering (CSE) exam
|