Multiple stacks in 1 Dimensional Array
#include<stdio.h>
#include<stdlib.h>
#define MAX 12
int stack[MAX];
int m=MAX, n;
int top[10], boundary[10];
void push(int i, int item )
{
if(top[i] == boundary[i+1])
{
printf("\nStack%d overflow", i);
}
else
{
top[i] = top[i] +1;
stack[top[i]] = item;
}
}
void pop(int i)
{
int item;
if(top[i] == boundary[i])
{
printf("\nStack%d underflow", i);
}
else
{
item = stack[top[i]];
printf("\nItem that got deleted from stack%d is %d", i, item);
top[i] = top[i] - 1;
}
}
void display(int i)
{
int j;
if(top[i] == boundary[i])
{
printf("\nStack%d is empty", i);
return;
}
else
{
printf("\n");
for(j=top[i]; j>=boundary[i]+1; j--)
printf("| %d |\n",stack[j]);
}
}
void main()
{
int ch, stackno, item,i;
printf("\nEnter the number of stacks: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
top[i] = boundary[i] = (i*(m/n)) - 1;
printf("\ntop[%d] = %d and boundary[%d] = %d",i,top[i], i, boundary[i]);
}
boundary[i] = MAX-1;
while(1)
{
printf("\n\n~~~MENU~~~");
printf("\n1.Push operation");
printf("\n2.Pop operation");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nPlease enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter a item to be pushed: ");
scanf("%d",&item);
printf("\nEnter the stack number: ");
scanf("%d",&stackno);
push(stackno, item);
break;
case 2: printf("\nEnter the stack number: ");
scanf("%d",&stackno);
pop(stackno);
break;
case 3: for(stackno=0;stackno<n;stackno++)
{
printf("\nStack%d contents are: ", stackno);
display(stackno);
}
break;
case 4:exit(0);
default: printf("\nPlease enter a valid choice");
break;
}
}
}
Output:
Enter the number of stacks: 3
top[0] = -1 and boundary[0] = -1
top[1] = 3 and boundary[1] = 3
top[2] = 7 and boundary[2] = 7
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 11
Enter the stack number: 0
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 12
Enter the stack number: 0
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 13
Enter the stack number: 0
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 14
Enter the stack number: 0
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 15
Enter the stack number: 0
Stack0 overflow
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 21
Enter the stack number: 1
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 22
Enter the stack number: 1
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 23
Enter the stack number: 1
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 31
Enter the stack number: 2
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 32
Enter the stack number: 2
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 33
Enter the stack number: 2
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 3
Stack0 contents are:
| 14 |
| 13 |
| 12 |
| 11 |
Stack1 contents are:
| 23 |
| 22 |
| 21 |
Stack2 contents are:
| 33 |
| 32 |
| 31 |
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 2
Enter the stack number: 1
Item that got deleted from stack1 is 23
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 3
Stack0 contents are:
| 14 |
| 13 |
| 12 |
| 11 |
Stack1 contents are:
| 22 |
| 21 |
Stack2 contents are:
| 33 |
| 32 |
| 31 |
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 2
Enter the stack number: 1
Item that got deleted from stack1 is 22
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 2
Enter the stack number: 1
Item that got deleted from stack1 is 21
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 2
Enter the stack number: 1
Stack1 underflow
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 3
Stack0 contents are:
| 14 |
| 13 |
| 12 |
| 11 |
Stack1 contents are:
Stack1 is empty
Stack2 contents are:
| 33 |
| 32 |
| 31 |
~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 4
No comments:
Post a Comment