Thursday 29 November 2012

Stack operations


/*Program to impelement stack operations
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/
#include <process.h>
#include <conio.h>
#include <iostream.h>
#define max 5

class stack
{
 private:
 int stack_arr[max],top,choice;
 public:
 void push();
 void pop();
 void display();
 stack();
};

void stack::push()
{
 int item;
 if (max==top)
 {
  cout<<"Stack is Overflow\n";
 }
 else
 {
  cout<<"\n               Insert the element for adding in Stack: ";
  cin>>item;
  stack_arr[top]=item;
  top++;
 }
}

void stack::pop()
{
 if (top==0)
 {
  cout<<"stack is Underflow\n";
  return;
 }
 else
 {
  top--;
  cout<<"Element poped from Stack is "<<stack_arr[top];
 }
}

void stack::display()
{
 int i;
 if(top==0)
 cout<<"Stack is Empty\n";
 else
 {
  cout<<"Stack is:\n";
  for(i=0;i<top;i++)
  cout<<stack_arr[i];
  cout<<"\n";
 }
}

stack::stack()
{
 top=0;
 do
 {
  cout<<"\n\t\t\t  STACK\n";
  cout<<"\t\t1. To Push an Element\n";
  cout<<"\t\t2. To Pop an Element\n";
  cout<<"\t\t3. To Display the Elements\n";
  cout<<"\t\t4. Main Page\n";
  cout<<"\t\t5. To Exit from Programme\n";
  cout<<"\n\t\t Enter Your Choice= ";
  cin>>choice;
  switch(choice)
  {
   case 1:
   push();break;
   case 2:
   pop();break;
   case 3:
   display();break;
   case 4:
 return;
   case 5:
 exit(1);
   default:
  cout<<"\n\t\tWrong Choice, Enter Again\n";
  }
 }while(1);
}
/*=========================4Nov.,2010=============================*/

No comments:

Post a Comment

You are welcome, your comment helps me to make this blog more better.