Previous Index Next

Static Stack

A stack is a data structure that stores and retrieves items in a last-in-first-out (LIFO) manner. Static stack has a fixed size and is implemented as array. A stack has two primary operations: push and pop. The push operation causes a value to be stored, or pushed onto the stack. The pop operation retrieves (and hence, removes) a value from the stack.

#include<iostream.h>
#include<conio.h>
#define size 4
class stack
{
     int data[size];
     int top;
public:
     stack()
     {
          top=-1;
     }
     void push();
     void pop();
     void display();
};

void stack::push()
{
     if(top==size-1)
     {
          cout<<"\nStack is full";
          return;
     }
     else
     {
          top++;
          cout<<"Enter Data : ";
          cin>>data[top];
     }
}

void stack::pop()
{
     if(top==-1)
          cout<<"\n Stack is empty";
     else
     {
          cout<<data[top]<<"deleted "<<endl;
          top--;
     }
}

void stack::display()
{
     int t=top;
     while(t>=0)
     {
          cout<<data[t]<<endl;
          t--;
     }
}

void main()
{
     stack st;
     int ch;
     do
     {
          cout<<"\n1. Push\n2. Pop\n3. Display \n4.Quit\nEnter Choice(1-4) ";
          cin>>ch;
          switch(ch)
          {
               case 1: st.push();break;
               case 2: st.pop();break;
               case 3: st.display();
          }
     }while(ch!=4);
}

Previous Index Next