Tuesday, November 22, 2011

1 Stack - Array


#include<iostream>

#define MAX 5



using namespace std;

int top = -1;

int stack[MAX];



void push();
void pop();
void display();


int main()

{

int choice;


 do

 {

 cout<<"\n\n1.Push\n";

 cout<<"2.Pop\n";

 cout<<"3.Display\n";

 cout<<"4.Quit\n";

 cout<<"Enter your choice : ";

 cin>>choice;



  switch(choice)

  {

  case 1 :push();
break;

  case 2:pop();
break;

  case 3:display();
break;

  case 4:exit(1);


  default:cout<<"Wrong choice\n";

  }
 }while(choice!=4);
return 0;
}



void push()

{

int p;


if(top == (MAX-1))

cout<<"Stack Overflow\n";

 else

 {

 cout<<"Enter the item to be pushed in stack : ";

 cin>>p;
 top=top+1;

 stack[top] = p;

 }

}



void pop()

{

 if(top == -1)

 cout<<Stack Underflow\n";

 else

 {

 cout<<"Popped element is : "
 cin>>stack[top];

 top=top-1;

 }

}



void display()

{

int i;

 if(top == -1)

 cout<<"Stack is empty\n";

 else

 {

 cout<<"Stack elements :\n";

 for(i = top; i >=0; i--)

 cout<<"\n"
 cout<<stack[i];

 }

}

No comments:

Post a Comment