Two Stacks in a Single Array
#include<stdio.h>#define MAX 5
int s[MAX];
int top1 = -1, top2 = MAX;
void push(int stackno, int item)
{
if(stackno==1)
{
if(top1+1==top2)
{
printf("\nStack is full");
}
else
{
top1 = top1+1;
s[top1] = item;
}
}
else if(stackno==2)
{
if(top2-1== top1)
{
printf("\nStack is full");
}
else
{
top2 = top2-1;
s[top2] = item;
}
}
}
void pop(int stackno)
{
int item;
if(stackno == 1)
{
if(top1 == -1)
{
printf("\nStack1 is empty");
}
else
{
item = s[top1];
top1 = top1-1;
printf("\nItem that got deleted from stack1 is %d", item);
}
}
else if(stackno == 2)
{
if(top2 == MAX)
{
printf("\nStack2 is empty");
}
else
{
item = s[top2];
top2 = top2+1;
printf("\nItem that got deleted from stack2 is %d", item);
}
}
}
void display()
{
int i;
if(top1 == -1)
{
printf("\nStack 1 is empty");
}
else
{
printf("\nStack 1 contents are:\n");
for(i=top1;i>=0;i--)
printf("| %d |\n", s[i]);
}
if(top2==MAX)
{
printf("\nStack 2 is empty");
}
else
{
printf("\nStack 2 contents are:\n");
for(i=top2;i<MAX;i++)
printf("| %d |\n", s[i]);
}
}
void main()
{
int stackno, ch, item;
while(1)
{
printf("\n\n~~~Menu~~~");
printf("\n1.Push operation");
printf("\n2.Pop operation");
printf("\n3.Display");
printf("\n4.exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the stack number: ");
scanf("%d",&stackno);
printf("\nEnter the item: ");
scanf("%d", &item);
push(stackno, item);
display();
break;
case 2: printf("\nEnter the stack number: ");
scanf("%d", &stackno);
pop(stackno);
display();
break;
case 3: display();
break;
case 4:exit(0);
default: printf("\nPlease enter a valid choice: ");
}
}
}
Output:
~~~Menu~~~
1.Push operation
2.Pop operation
3.Display
4.exit
Enter your choice: 1
Enter the stack number: 1
Enter the item: 11
Stack 1 contents are:
| 11 |
Stack 2 is empty
~~~Menu~~~
1.Push operation
2.Pop operation
3.Display
4.exit
Enter your choice: 1
Enter the stack number: 1
Enter the item: 12
Stack 1 contents are:
| 12 |
| 11 |
Stack 2 is empty
~~~Menu~~~
1.Push operation
2.Pop operation
3.Display
4.exit
Enter your choice: 1
Enter the stack number: 1
Enter the item: 13
Stack 1 contents are:
| 13 |
| 12 |
| 11 |
Stack 2 is empty
~~~Menu~~~
1.Push operation
2.Pop operation
3.Display
4.exit
Enter your choice: 1
Enter the stack number: 2
Enter the item: 21
Stack 1 contents are:
| 13 |
| 12 |
| 11 |
Stack 2 contents are:
| 21 |
~~~Menu~~~
1.Push operation
2.Pop operation
3.Display
4.exit
Enter your choice: 1
Enter the stack number: 2
Enter the item: 22
Stack 1 contents are:
| 13 |
| 12 |
| 11 |
Stack 2 contents are:
| 22 |
| 21 |
~~~Menu~~~
1.Push operation
2.Pop operation
3.Display
4.exit
Enter your choice: 1
Enter the stack number: 2
Enter the item: 23
Stack is full
Stack 1 contents are:
| 13 |
| 12 |
| 11 |
Stack 2 contents are:
| 22 |
| 21 |
~~~Menu~~~
1.Push operation
2.Pop operation
3.Display
4.exit
Enter your choice: 2
Enter the stack number: 2
Item that got deleted from stack2 is 22
Stack 1 contents are:
| 13 |
| 12 |
| 11 |
Stack 2 contents are:
| 21 |
~~~Menu~~~
1.Push operation
2.Pop operation
3.Display
4.exit
Enter your choice: 2
Enter the stack number: 1
Item that got deleted from stack1 is 13
Stack 1 contents are:
| 12 |
| 11 |
Stack 2 contents are:
| 21 |
~~~Menu~~~
1.Push operation
2.Pop operation
3.Display
4.exit
Enter your choice: 3
Stack 1 contents are:
| 12 |
| 11 |
Stack 2 contents are:
| 21 |
~~~Menu~~~
1.Push operation
2.Pop operation
3.Display
4.exit
Enter your choice: 4