Powered by Blogger.

Program Of Stack..

//THE PROGRAM FOR STACK



#include<iostream.h>
#include<conio.h>
#include<process.h>
#define SIZE 5
int top=-1;
void push(int [],int);
int  pop(int []);
int  peep(int [],int);
void change(int [],int,int);
void main()
{
  int stack[SIZE],x,c,i;
  clrscr();
  do
  {
     cout<<"\n1.PUSH OPERATION";
     cout<<"\n2.POP  OPERATION";
     cout<<"\n3.PEEP";
     cout<<"\n4.CHANGE";
     cout<<"\n5.DISPLAY STACK";
     cout<<"\n6.EXIT";
     cout<<"\nEnter your choice:";
     cin>>c;
     switch(c)
     {
       case 1:
     cout<<"\nEnter number:";
     cin>>x;
     push(stack,x);
     break;
       case 2:
     x=pop(stack);
     cout<<"\nThe POP number is:"<<x;
     break;
       case 3:
     cout<<"'\nEnter position you want to PEEP:";
     cin>>i;
     x=peep(stack,i);
     cout<<"\nThe value is:"<<x;
     break;
       case 4:
     cout<<"\nEnter position you want to CHANGE:";
     cin>>i;
     cout<<"\nEnter  no:";
     cin>>x;
     change(stack,i,x);
     break;
       case 5:
     for(i=top;i>=0;i--)
      cout<<"\n"<<stack[i];
     break;
      default:
exit(0);
     }
  }while(c!=6);
  getch();
}
void push(int s[SIZE],int x)
{
   if(top>=SIZE-1)
   {
      cout<<"stack overflow";
      return;
   }
   top++;
   s[top]=x;
}
int pop(int s[SIZE])
{
   if(top==-1)
   {
     cout<<"Stack underflow";
     return(0);
   }
   top--;
   return(s[top+1]);
}
int peep(int s[SIZE],int i)
{
   if(top-i+1<=-1)
   {
      cout<<"Stack Underflow on peep";
      return(0);
   }
   return(s[top-i+1]);
}
void change(int s[SIZE],int i,int x)
{
    if(top-i+1<=-1)
    {
      cout<<"Stack Underflow on change";
      return;
    }
    s[top-i+1]=x;
    return;
}

Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment