Pages

Monday, August 29, 2016

C Program for Multi Stack

Program-

//Implementation of Multi Stack
#include<stdio.h>
#define MAX 10

struct s
{
 int stack[10];
 int top;
}red,green,blue;

void push(int);
void pop(int);
void show();


void main()
{
int ch;
red.top=green.top=blue.top=-1;
int temp;
do
{
printf("\n\t Main menu");
printf("\n 1:Push red");
printf("\n 2:Push green");
printf("\n 3:Push blue");
printf("\n 4:Pop red");
printf("\n 5:Pop green");
printf("\n 6:Pop blue");
printf("\n 7:Show");
printf("\n 8:Exit");
printf("\n Enter Your Choice ");
scanf("%d",&ch);
switch(ch)
{
case 1: push(1); break;
case 2: push(2); break;
case 3: push(3); break;
case 4: pop(4); break;
case 5: pop(5); break;
case 6: pop(6); break;
case 7: show(); break;
case 8: temp=1; break;
default: printf("\n wrong choice");
}
}while(ch!=8 && temp!=1);
}

void push(int no)
{
int item;
if(no==1)
{

if(red.top==MAX-1)
printf("\n overflow");
else
{
printf("\n Enter the value ");
scanf("%d",&item);
red.top++;
red.stack[red.top]=item;
}
}

if(no==2)
{

if(green.top==MAX-1)
printf("\n overflow");
else
{
printf("\n Enter the value ");
scanf("%d",&item);
green.top++;
green.stack[green.top]=item;
}
}

if(no==3)
{

if(blue.top==MAX-1)
printf("\n overflow");
else
{
printf("\n Enter the value ");
scanf("%d",&item);
blue.top++;
blue.stack[blue.top]=item;
}
}
}

void pop(int no)
{
int item;

if(no==4)
{

if(red.top==-1)
printf("\n Underflow");
else
{
item=red.stack[red.top];
printf("\n Deleted element is %d",item);
red.top--;
}
}
if(no==5)
{

if(green.top==-1)
printf("\n Underflow");
else
{
item=green.stack[green.top];
printf("\n Deleted element is %d",item);
green.top--;
}
}
if(no==6)
{

if(blue.top==-1)
printf("\n Underflow");
else
{
item=blue.stack[blue.top];
printf("\n Deleted element is %d",item);
blue.top--;
}
}
}

void show()
{
int i;

if(red.top==-1)
printf("\n red stack is empty");
else
{
printf("\n Elements of Red stack are \n");
for(i=0;i<=red.top;i++)
{
printf("%d\t",red.stack[i]);
}
}

if(green.top==-1)
printf("\n green stack is empty");
else
{
printf("\n Elements of green stack are \n");
for(i=0;i<=green.top;i++)
{
printf("%d\t",green.stack[i]);
}
}

if(blue.top==-1)
printf("\n blue stack is empty");
else
{
printf("\n Elements of blue stack are \n");
for(i=0;i<=blue.top;i++)
{
printf("%d\t",blue.stack[i]);
}
}
}

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 value 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 value 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 value 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 value 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 value 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

 Elements of Red stack are
10      20      30
 Elements of green stack are
100     200
 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

 Deleted element is 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 7

 Elements of Red stack are
10      20
 Elements of green stack are
100     200
 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