STACK IN DATA STRUCTURES


stack of books

Stack of books


pile of dishes



Pile of dishes

stack of discs


Stack of discs



Stack Operations


operations on stack

Stack Implementation

Stack can be implemented in two ways :
  1. Static implementation
    • Static implementation uses arrays for creating stack. It is considered as a very simple technique. A one-dimensional array can be used to hold all the elements of a stack.A variable "top" is used to keep track of the index of the top most element present in the stack.Initially, top is set to -1 as the stack is empty. After "push" operation top is updated to top+1 this indicates growing of stack. After "pop" operation top is decremented by 1 this indicates the shrinking of stack.When the value of top becomes Size-1 after a series of insertions or push operations, it is said to be full .Now no elements can be pushed onto stack. Similarly, before popping an element from the stack we first check if the stack still contains elements and is not empty.
  2. static representation

    Program for implementing stack using an array in C++


    #include < iostream > using namespace std; #define SIZE 5 int S[SIZE]; int top = -1; void push(int value) { if(top==SIZE-1) { cout<<"Stack is full!\n"; } else { top++; S[top]=value; } } bool empty() { if(top==-1) return true; else return false; } void pop() { if(empty()) cout<<"Stack is empty!\n"; else cout<<"Element popped from the stack:"<< S[top]; top--; } void stack_top() { if(empty()) cout<<"Stack is empty!\n"; else cout<<"Element at top is: "<< S[top]<<"\n"; } void displayStack() { if(empty()) { cout<<"Stack is empty!\n"; } else { for(int i=0 ; i<=top; i++) cout<< S[i]<<" "; cout<<"\n"; } } int main() { char ch, flag=1;int value; while( flag == 1) { cout<<"\na)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT"; cout<<"\n----------------------------------------------"; cout<<"\nYour choice:" ; cin>>ch; switch (ch) { case 'a': cout<<"Enter Value:"; cin>>value; push(value); break; case 'b':pop(); break; case 'c': stack_top(); break; case 'd':displayStack(); break; case 'e': flag = 0; cout<<"-Exit-"; break; default:cout<<"Invalid choice"; } } return 0; }

    Output:

    
       a)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT
       ----------------------------------------------
       Your choice:a
       Enter Value:14
       a)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT
       ----------------------------------------------
        Your choice:a
       Enter Value:21
       a)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT
       ----------------------------------------------
       Your choice:b
       Element popped from the stack:21
       a)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT
       ----------------------------------------------
       Your choice:a
       Enter Value:48
       a)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT
       ----------------------------------------------
       Your choice:d
       14 48
       a)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT
       ----------------------------------------------
       Your choice:c
       Element at top is: 48
       a)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT
       ----------------------------------------------
       Your choice:e
       -Exit-
    
    

    Drawbacks of using array for implementation of stack:
    -We cannot increase the size of the array if we have more elements to insert as array stores only a fixed number of data values which is predefined at the time of declaration by specifying size.
    -Considering the above point if we create a very large array, a lot of memory space will be wasted.

  3. Dynamic implementation
    • Dynamic implementation is also called linked list representation. Pointers are used for dynamic implementation of stack. It is said to be an efficient way of representing stack as in this technique we can dynamically increase the size of the stack as per the requirement. In this kind of representation we use a singly connected linked list where the head pointer/top pointer keeps track of the top most element in the stack.
    • The push operation involves inserting a new node to the linked list. Initially when the Stack (Linked List) is empty, the top pointer will be NULL. After a node is pushed onto the stack the top will now point to this newly created node. The pop operation is similar to deletion of a node from the starting of a linked list. We will first equate a temporary pointer to the top pointer then the top is made to point at the node which was initially inserted before the top most node. Finally, we will delete the node which the temporary pointer is pointing at.
    • Remember that deletion and insertion in stack takes place in the last in first out(LIFO) format so the newly inserted element will be the first one to be popped off the stack. Again, before popping an element/node we have to check if the stack is empty or not i.e. if top == NULL, it means that the stack is empty and therefore pop operation cannot be carried out.
  4. dynamic representation

    Program for implementing stack using linked list in C++


    #include < iostream > using namespace std; //Structure of the Node struct Node { int data; Node *link; }; //top pointer to keep track of the top of the stack Node *top = NULL; //Function to determine if stack is empty or not bool isempty() { if(top == NULL) return true; else return false; } //Function to insert an element in stack void push (int value) { Node *ptr = new Node(); ptr->data = value; ptr->link = top; top = ptr; } //Function to pop an element from the stack void pop ( ) { if ( isempty() ) cout<<"Stack is Empty"; else { Node *ptr = top; cout<<"Element popped from the stack:"<data; top = top -> link; delete(ptr); } } //Function to display element at the top of the stack void stack_top() { if ( isempty() ) cout<<"Stack is Empty"; else cout<<"Element at top is : "<< top->data; } // Function to Display the stack void displayStack() { if ( isempty() ) cout<<"Stack is Empty"; else { Node *temp=top; while(temp!=NULL) { cout<data<<" "; temp=temp->link; } cout<<"\n"; } } int main() { char ch, flag=1; int value; while( flag == 1) { cout<<"\na)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT"; cout<<"\n----------------------------------------------"; cout<<"\nYour choice:"; cin>>ch; switch (ch) { case 'a': cout<<"Enter Value:"; cin>>value; push(value); break; case 'b':pop(); break; case 'c': stack_top(); break; case 'd':displayStack(); break; case 'e': flag = 0; cout<<"-Exit-"; break; default:cout<<"Invalid choice"; } } return 0; }

    Output:


    a)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT ---------------------------------------------- Your choice:a Enter Value:22 a)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT ---------------------------------------------- Your choice:a Enter Value:13 a)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT ---------------------------------------------- Your choice:b Element popped from the stack:13 a)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT ---------------------------------------------- Your choice:c Element at top is : 22 a)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT ---------------------------------------------- Your choice:a Enter Value:32 )PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT ---------------------------------------------- Your choice:d 32 22 a)PUSH b)POP c)SHOW TOP d)DISPLAY STACK e)EXIT ---------------------------------------------- Your choice:e -Exit-

Applications Of Stack




Article Contributed By :
Zahra Dehghan

CSE 2nd year Undergraduate @Trinity College Of Engineering And Research, Pune
Programming languages familiar with: C++, Python, Java
Area of Interest: Web/App Development, Machine Learning