Friday, 29 June 2012

Stack Implemenation Using C++


#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
#include<graphics.h>
 int stack[50],n,tos=0;


 void main()
 {
   int i,o;
   void menu();
   void push();
   void pop();
   void display();
   clrscr();
    cout<<endl<<"ENTER THE NO. OF ELMENTS :";
    cin>>n;
    while(1)
    {
       menu();
       cout<<endl<<endl<<"ENTER YOUR CHOICE :";
       cin>>o;
       switch(o)
       {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
cout<<endl<<endl<<"EXITING. . . .";
delay(1000);
exit(0);
default:
cout<<endl<<endl<<"WRONG CHOICE. . .TRY AGAIN. . ";
getch();
       }
    }
//   getch();
 }
  void menu()
  {
    clrscr();
    cout<<endl<<endl<<"\t\t\t\t=========STACK OPERATIONS==========";
    cout<<endl<<"\t\t\t\t1.PUSH                               SIZE = "<<n;
    cout<<endl<<"\t\t\t\t2.POP";
    cout<<endl<<"\t\t\t\t3.DISPLAY";
    cout<<endl<<"\t\t\t\t4.EXIT";
  }


   void push()
   {
     if(tos>=n)
     {
       cout<<endl<<endl<<"SORRY STACK IS FULL. . OVERFLOW. . .";
       getch();
     }
     else
     {
       ++tos;
       int v;
       cout<<endl<<endl<<"ENTER THE VALUE TO BE INSERTED IN STACK : ";
       cin>>v;
       stack[tos]=v;
     }
   }


    void pop()
    {
      if(tos==0)
      {
cout<<endl<<endl<<"SORRY STACK IS EMPTY. . . UNDERFLOW. . .";
getch();
      }
      else
      {
tos--;
      }
    }
     void display()
     {
int i;
 if(tos==0)
 {
 cout<<endl<<endl<<"NO ELEMENTS TO DISPLAY. . . USE PUSH TO INSERT";
 }
 else
 {
  cout<<endl<<endl<<"\t\tELEMENTS IN STACK ARE . . . .                 TOS = "<<tos;
//   cout<<tos;
   for(i=tos;i>=1;i--)
   {
     cout<<endl<<endl<<"\t\t\tSTACK["<<i<<"] = "<<stack[i];
   }
 }
 getch();
     }

No comments:

Post a Comment