Skip to content
Related Articles

Related Articles

Implementation of stack using Doubly Linked List

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 17 Oct, 2022
Improve Article
Save Article

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 List
struct Node {
    int data;
    struct Node* next;
    struct Node* prev;
};


Java




// Declaration of Doubly Linked List
static class Node {
    int data;
    Node next;
    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:

  1. push()
  2. pop() 
  3. isEmpty()
  4. printstack()
  5. stacksize()
  6. 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;
    }
}


Java




void push(int d)
{
    Noden n = new Node();
    n.data = d;
    if (n.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);
    }
}


Java




void pop()
{
    Node n = top;
    if (n.isEmpty())
        System.out.println("Stack is empty");
    else if (top == start) {
        top = null;
        start = null;
    }
    else {
        top.prev.next = null;
        top = n.prev;
    }
}


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


Java




boolean 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");
    }
}


Java




void printstack()
{
    if (isEmpty())
        System.out.println("Stack is empty");
    else {
        Node ptr = start;
        System.out.println("Stack is :  ");
        while (ptr != null) {
            System.out.print(ptr.data + " ");
            ptr = ptr.next;
        }
        System.out.println();
    }
}


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);
}


Java




void stacksize()
{
    int c = 0;
    if (isEmpty())
        Systen.out.println("Stack is empty");
    else {
       Node ptr = start;
        while (ptr != null) {
            c++;
            ptr = ptr.next;
        }
    }
    Systen.out.println(" Size of the stack is : " + 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);
}


Java




void topelement()
{
    if (isEmpty())
        System.out.println("Stack is empty");
    else
        System.out.println(
               "The element at top of the stack is : " +
            top.data);
}


Time Complexity: O(1)

Implementation of Stack using Doubly Linked List:

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 empty
bool isEmpty()
{
    if (start == NULL)
        return true;
    return false;
}
 
// pushes element onto stack
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;
    }
}
 
// Pops top element from stack
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);
    }
}
 
// Prints top element of the stack
void 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 stack
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);
}
 
// Determines the size of the stack
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");
    }
}
 
// Driver code
int 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;
}


Java




// A complete working java program to
// demonstrate all stack operations using
// a doubly linked list
 
class GFG {
 
  static class Node {
    int data;
    Node prev;
    Node next;
  };
 
  static Node start = null;
  static Node top = null;
 
  // Check if stack is empty
  public static boolean isEmpty() {
    if (start == null)
      return true;
    return false;
  }
 
  // pushes element onto stack
  public static void push(int d) {
    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 stack
  public static void pop() {
    Node n;
    n = top;
    if (isEmpty())
      System.out.println("Stack is empty");
    else if (top == start) {
      top = null;
      start = null;
      n = null;
    } else {
      top.prev.next = null;
      top = n.prev;
      n = null;
    }
  }
 
  // Prints top element of the stack
  public static void topelement() {
    if (isEmpty())
      System.out.println("Stack is empty");
    else
      System.out.println("The element at top of the stack is : " + top.data);
  }
 
  // Determines the size of the stack
  public static void stacksize() {
    int c = 0;
    if (isEmpty())
      System.out.println("Stack is empty");
    else {
      Node ptr = start;
      while (ptr != null) {
        c++;
        ptr = ptr.next;
      }
    }
    System.out.println("Size of the stack is : " + c);
  }
 
  // Determines the size of the stack
  public static void printstack() {
    if (isEmpty())
      System.out.println("Stack is empty");
    else {
      Node ptr = start;
      System.out.print("Stack is :  ");
      while (ptr != null) {
        System.out.print(ptr.data + " ");
        ptr = ptr.next;
      }
      System.out.println("");
    }
  }
 
  // Driver code
  public static void main(String args[]) {
    push(2);
    push(5);
    push(10);
    printstack();
    topelement();
    stacksize();
    pop();
    System.out.println("\nElement popped from the stack ");
    topelement();
    pop();
    System.out.print("\nElement popped from the stack \n");
    stacksize();
  }
}
 
// This code is contributed by Saurabh Jaiswal


C#




// A complete working java program to
// demonstrate all stack operations using
// a doubly linked list
using System;
public class GFG {
 
  class Node {
    public int data;
    public Node prev;
    public Node next;
  }
 
  static Node start = null;
  static Node top = null;
 
  // Check if stack is empty
  public static bool isEmpty()
  {
    if (start == null)
      return true;
    return false;
  }
 
  // pushes element onto stack
  public static void push(int d)
  {
    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 stack
  public static void pop()
  {
    Node n;
    n = top;
    if (isEmpty())
      Console.WriteLine("Stack is empty");
    else if (top == start) {
      top = null;
      start = null;
      n = null;
    }
    else {
      top.prev.next = null;
      top = n.prev;
      n = null;
    }
  }
 
  // Prints top element of the stack
  public static void topelement()
  {
    if (isEmpty()) {
      Console.WriteLine("Stack is empty");
    }
    else {
      Console.WriteLine(
        "The element at top of the stack is : "
        + top.data);
    }
  }
 
  // Determines the size of the stack
  public static void stacksize()
  {
    int c = 0;
    if (isEmpty())
      Console.WriteLine("Stack is empty");
    else {
      Node ptr = start;
      while (ptr != null) {
        c++;
        ptr = ptr.next;
      }
    }
    Console.WriteLine("Size of the stack is : " + c);
  }
 
  // Determines the size of the stack
  public static void printstack()
  {
    if (isEmpty())
      Console.WriteLine("Stack is empty");
    else {
      Node ptr = start;
      Console.Write("Stack is :  ");
      while (ptr != null) {
        Console.Write(ptr.data + " ");
        ptr = ptr.next;
      }
      Console.WriteLine("");
    }
  }
 
  static public void Main()
  {
 
    // Code
    push(2);
    push(5);
    push(10);
    printstack();
    topelement();
    stacksize();
    pop();
    Console.WriteLine(
      "\nElement popped from the stack ");
    topelement();
    pop();
    Console.Write("\nElement popped from the stack \n");
    stacksize();
  }
}
 
// This code is contributed by lokeshmvs21.


Output

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 
 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!