Sunday, 14 April 2013

C++ program for stack operations using array

/*Program for stack using array
  By sanjay kumar singh,ggsestc*/



#include<iostream.h>
#include<conio.h>
#define size 10

void push();
void pop();
void display();
void peek();
int top=-1,s[size];

void main()

{
        int n;
        do
        {
            cout<<"enter the choice from the following\n";
            cout<<"1...push\n2...pop\n3...display\n4...peek\n";
            cin>>n;
            switch(n)
            {
            case 1:cout<<"...push operation...\n";
                         push();
                         break;
                  case 2:cout<<"...pop operation...\n";
                         pop();
                         break;
                  case 3:cout<<"...display operation...\n";
                         display();
                         break;
                  case 4:cout<<"...peep operation...\n";
                         peek();
                         break;
             }
        }while(n<5);
      getch();
}

void push()
{
      int val;
      cout<<"enter element u want to insert:";
      cin>>val;
      if(top==size-1)
      cout<<"stack is overflow";
      else
      {
          top++;
            s[top]=val;
      }
}

void pop()
{
      int val;
      if(top==-1)
      cout<<"stack underflow";
      else
      {
      val=s[top];
                top--;
                cout<<"\nelement deleted is:"<<val;
      }
}

void display()
{
      if(top==-1)
      cout<<"stack is empty";
      else
      {
          for(int i=top;i>=0;i--)
            cout<<s[i]<<"\t";
      }
}

void peek()
{
      if(top==-1)
      cout<<"stack is empty";
      else
      cout<<s[top];
}

No comments:

Post a Comment