Stack implementation using Array in C++

In this post, I will provide information about how to implement a stack using an array using a C++ programming language.

There are various types of data structures out of which stack is the most common form of data structure which is used in various activities.

C++ program to implement stack using array

A stack is a form of data structure(linear data structure). It is based on the LIFO concept, where LIFO stands for LAST IN FIRST OUT. In other words, a stack is a form of data structure in which both insertions of elements and deletion of elements happens from the same end /side.

The end from which both insertion and deletion take place in the stack is called Top of the Stack.

TYPES OF OPERATIONS PERFORMED ON THE STACK:

There are mainly three types of Operations performed on a stack, which are as follows

  1. PUSH operation: This operation is related to the insertion of an element on to the top of the stack.
  2. POP  operation: This operation is related to the deletion of an element from the top of the stack.
  3. DISPLAY operation: This operation is used to display the elements currently present in the stack.

In stack implementation using array both insertion and deletion happens from TOP.

Stack implementation using Array in C++

CODE for understanding Stack implementation using Array in C++:-

//stack implementation using C++
#include<iostream>
using namespace std;


int stack[100];
int n,i,top,x;

void push();
void pop();
void display();
int main()
{ 
  int ch;
  cout<<"Enter the number of elements in the stack";
  cin>>n;
  i=1;
  top=-1;
  while(i)
  { cout<<"Enter your choice (1-4)<<endl;
    cout<<"1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n";
    cin>>ch;
    switch(ch)
    {
      case 1:push();
             break;
      case 2:pop();
             break;
      case 3:display();
             break;
      case 4:i=0;
             break;
      default:cout<<"Wrong Choice!!!!"<<endl;
             break;
    }
  }
return 0;	
}

void push()
{  
    if(top>=n-1)           // OVERFLOW i.e. if stack top goes beyond the size of the stack
    {
    	cout<<"STACK IS OVERFLOW"<<endl;
    
  }
  else
  {
  
    cout<<"Enter the value to be added :";
  cin>>x;
  top=top+1;
  stack[top]=x;
    }
}

void pop()
{
  if(top<=-1)                // Underflow i.e. stack is at index -1
  {
    cout<<"STACK IS UNDERFLOW"<<endl;

  }
  else
  {
    cout<<"Value :"<<stack[top]<<"GOT deleted."<<endl;
    top--;
  }
}
void display()
{
  if(top>=0)
  {
    cout<<"Elements in the stack are :";
    for(i=top;i>=0;i--)
       cout<<stack[i]<<" ";
    cout<<"\n";
  }
  else
   cout<<"STACK IS EMPTY."<<endl;
}

OUTPUT:

Enter the number of elements in the stack: 5

Enter your choice (1-4)
  1. PUSH
  2. POP
  3. DISPLAY
  4. EXIT
1

Enter the value to be added:5
  1. PUSH
  2. POP
  3. DISPLAY
  4. EXIT
1

Enter the value to be added:6
  1. PUSH
  2. POP
  3. DISPLAY
  4. EXIT
3

Elements in the stack are:6 5
  1. PUSH
  2. POP
  3. DISPLAY
  4. EXIT
2

Value 6GOT deleted
  1. PUSH
  2. POP
  3. DISPLAY
  4. EXIT
3

Elements in the stack are  :5
  1. PUSH
  2. POP
  3. DISPLAY
  4. EXIT
2

Value 5GOT deleted
  1. PUSH
  2. POP
  3. DISPLAY
  4. EXIT
3

STACK IS EMPTY.
  1. PUSH
  2. POP
  3. DISPLAY
  4. EXIT
2

STACK IS UNDERFLOW
  1. PUSH
  2. POP
  3. DISPLAY
  4. EXIT

 

You may also like:

Leave a Reply

Your email address will not be published.