Implementation of stack using Doubly Linked List
Stack and doubly linked lists are two important data structures with their own benefits. Stack is a data structure that follows the LIFO technique and can be implemented using arrays or linked list data structures. Doubly linked list has the advantage that it can also traverse the previous node with the help of “previous” pointer.
Doubly Linked List:
- Doubly Linked list (DLL) is a linked list, which contains nodes that are divided into three parts i.e. Data, next pointer and previous pointers.
- The previous pointer points to the previous node and the next pointer points to the node next to the current node.
- The start pointer points to the first node of the linked list.
The structure of a DLL is shown below.
C++
// Declaration of Doubly Linked Liststruct Node { int data; struct Node* next; struct Node* prev;}; |
Stack:
- A stack is a linear data structure in which the elements are accessed through a pointer called the “top” of the stack.
- It follows the LIFO(Last In First Out) technique.
- It can be implemented by array or linked list.
Functions to be Implemented:
Some of the basic functionalities on a stack covered here are:
- push()
- pop()
- isEmpty()
- printstack()
- stacksize()
- topelement()
1. push():
If the stack is empty then take a new node, add data to it and assign “null” to its previous and next pointer as it is the first node of the DLL. Assign top and start as the new node. Otherwise, take a new node, add data to it and assign the “previous” pointer of the new node to the “top” node earlier and next as “null”. Further, update the “top” pointer to hold the value of the new node as that will be the top element of the stack now.
Syntax:
push( d );
Below is the implementation of the method.
C++
void push(int d){ struct Node* n; n = new Node(); n->data = d; if (isEmpty()) { n->prev = NULL; n->next = NULL; // As it is first node // if stack is empty start = n; top = n; } else { top->next = n; n->next = NULL; n->prev = top; top = n; }} |
Time Complexity: O(1)
2. pop():
If the stack is empty, then print that stack is empty, Otherwise, assign top ->prev -> next as “null” and assign top as top->prev.
Syntax:
pop();
Below is the implementation of the method.
C++
void pop(){ struct Node* n; n = top; if (isEmpty()) printf("Stack is empty"); else if (top == start) { top = NULL; start = NULL; free(n); } else { top->prev->next = NULL; top = n->prev; free(n); }} |
Time Complexity: O(1)
3. isEmpty():
Check for the top pointer. If it is “null” then return true. Otherwise, return false.
Syntax:
isEmpty();
Below is the implementation of the method.
C++
bool isEmpty(){ if (start == NULL) return true; return false;} |
Time Complexity: O(1)
4. printstack():
If the stack is empty, then print that stack is empty. Otherwise, traverse the doubly linked list from start to end and print the data of each node.
Syntax:
printstack();
Below is the implementation of the method.
C++
void printstack(){ if (isEmpty()) printf("Stack is empty"); else { struct Node* ptr = start; printf("Stack is : "); while (ptr != NULL) { printf("%d ", ptr->data); ptr = ptr->next; } printf("\n"); }} |
Time Complexity: O(N) where N is the size of the stack
5. stacksize():
If the stack is empty, then return zero else iterate from the start to end and count the number of nodes of the doubly linked list.
Syntax:
stacksize();
Below is the implementation of the method.
C++
void stacksize(){ int c = 0; if (isEmpty()) printf("Stack is empty"); else { struct Node* ptr = start; while (ptr != NULL) { c++; ptr = ptr->next; } } printf(" Size of the stack is : %d \n ", c);} |
Time Complexity: O(N) where N is the size of the stack
6. topelement():
If the stack is empty, then there is no top element. Otherwise, print the element at the top node of the stack.
Syntax:
topelement();
Below is the implementation of the method.
C++
void topelement(){ if (isEmpty()) printf("Stack is empty"); else printf( "The element at top of the stack is : %d \n", top->data);} |
Time Complexity: O(1)
Implementation of Stack using Doubly Linked List:
Below is the implementation of the stack using a doubly linked list.
C++
// A complete working program to// demonstrate all stack operations using// a doubly linked list #include <iostream>struct Node { int data; struct Node* prev; struct Node* next;};Node* start = NULL;Node* top = NULL; // Check if stack is emptybool isEmpty(){ if (start == NULL) return true; return false;} // pushes element onto stackvoid push(int d){ struct Node* n; n = new Node(); n->data = d; if (isEmpty()) { n->prev = NULL; n->next = NULL; // As it is first node if stack // is empty start = n; top = n; } else { top->next = n; n->next = NULL; n->prev = top; top = n; }} // Pops top element from stackvoid pop(){ struct Node* n; n = top; if (isEmpty()) printf("Stack is empty"); else if (top == start) { top = NULL; start = NULL; free(n); } else { top->prev->next = NULL; top = n->prev; free(n); }} // Prints top element of the stackvoid topelement(){ if (isEmpty()) printf("Stack is empty"); else printf( "The element at top of the stack is : %d \n", top->data);} // Determines the size of the stackvoid stacksize(){ int c = 0; if (isEmpty()) printf("Stack is empty"); else { struct Node* ptr = start; while (ptr != NULL) { c++; ptr = ptr->next; } } printf("Size of the stack is : %d \n ", c);} // Determines the size of the stackvoid printstack(){ if (isEmpty()) printf("Stack is empty"); else { struct Node* ptr = start; printf("Stack is : "); while (ptr != NULL) { printf("%d ", ptr->data); ptr = ptr->next; } printf("\n"); }} // Driver codeint main(){ push(2); push(5); push(10); printstack(); topelement(); stacksize(); pop(); printf("\nElement popped from the stack \n"); topelement(); pop(); printf("\nElement popped from the stack \n"); stacksize(); return 0;} |
Stack is : 2 5 10 The element at top of the stack is : 10 Size of the stack is : 3 Element popped from the stack The element at top of the stack is : 5 Element popped from the stack Size of the stack is : 1


