Pages

Friday, September 9, 2016

C++ Program for Multi Stack

Program-

#include<iostream>
using namespace std;
#define MAX 5

class Stack
{
public:
int a[MAX];
int top;

Stack()
{
top=-1;
}

int getdata();
bool IsEmpty();
bool IsFull();
void push();
void pop();
};

int Stack::getdata()
{
int data;
cout<<"\nEnter the Element ";
cin>>data;
return data;
}

bool Stack::IsEmpty()
{
if(top==-1)
return 1;
return 0;
}

bool Stack::IsFull()
{
if(top==MAX)
return 1;
return 0;
}

void display(Stack & r,Stack & g,Stack &b)
{
int i;
if(r.IsEmpty())
cout<<"\nRed stack is empty";
else
{
cout<<"\nItems in  the Red stack \n";
for(i=r.top;i>=0;i--)
cout<<r.a[i]<<endl;
}

if(g.IsEmpty())
cout<<"\nGreen stack is empty";
else
{
cout<<"\nItems in  the Green stack \n";
for(i=g.top;i>=0;i--)
cout<<g.a[i]<<endl;
}
if(b.IsEmpty())
cout<<"\nBlue stack is empty";
else
{
cout<<"\nItems in  the Blue stack \n";
for(i=b.top;i>=0;i--)
cout<<b.a[i]<<endl;
}
}

void Stack::push()
{
if(IsFull())
cout<<"\n stack is full";
else
{
top++;
a[top]=getdata();
}
}

void Stack::pop()
{
if(IsEmpty())
cout<<"\nstack is empty";
else
top--;
}

int main()
{
Stack red,green,blue;
  int ch,temp;

do
  {
  cout<<"\n\t Main menu";
  cout<<"\n 1:Push red";
  cout<<"\n 2:Push green";
  cout<<"\n 3:Push blue";
  cout<<"\n 4:Pop red";
  cout<<"\n 5:Pop green";
  cout<<"\n 6:Pop blue";
  cout<<"\n 7:Show";
  cout<<"\n 8:Exit";
  cout<<"\n Enter Your Choice ";
  cin>>ch;
  switch(ch)
  {
    case 1: red.push(); break;
    case 2: green.push(); break;
    case 3: blue.push(); break;
    case 4: red.pop(); break;
    case 5: green.pop(); break;
    case 6: blue.pop(); break;
    case 7: display(red,green,blue); break;
    case 8: temp=1; break;
    default: cout<<"\n wrong choice";
  }
  }while(ch!=8 && temp!=1);
}

Output-


         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 1

Enter the Element 10

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 1

Enter the Element 20

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 1

Enter the Element 30

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 2

Enter the Element 100

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 2

Enter the Element 200

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 7

Items in  the Red stack
30
20
10

Items in  the Green stack
200
100

Blue stack is empty
         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 4

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 7

Items in  the Red stack
20
10

Items in  the Green stack
200
100

Blue stack is empty
         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 8


No comments:

Post a Comment