Stack operations

Jumat, 11 April 2014


/*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
";

 }
 else
 {
  cout<<"
              Insert the element for adding in Stack: ";

  cin>>item;
  stack_arr[top]=item;
  top++;
 }
}

void stack::pop()
{
 if (top==0)
 {
  cout<<"stack is Underflow
";

  return;
 }
 else
 {
  top--;
  cout<<"Element poped from Stack is "<<stack_arr[top];
 }
}

void stack::display()
{
 int i;
 if(top==0)
 cout<<"Stack is Empty
";

 else
 {
  cout<<"Stack is:
";

  for(i=0;i<top;i++)
  cout<<stack_arr[i];
  cout<<"
";

 }
}

stack::stack()
{
 top=0;
 do
 {
  cout<<"
 STACK
";

  cout<<" 1. To Push an Element
";

  cout<<" 2. To Pop an Element
";

  cout<<" 3. To Display the Elements
";

  cout<<" 4. Main Page
";

  cout<<" 5. To Exit from Programme
";

  cout<<"
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<<"
Wrong Choice, Enter Again
";

  }
 }while(1);
}
/*=========================4Nov.,2010=============================*/

Related Posts by Categories

0 komentar:

Posting Komentar

Copyright © 2010 All About Tech Information | Powered By Blogger