Implement multi stack in a single array.
Source Code
#include<stdio.h>
#include<stdlib.h>
int stack[50],top[50],min[50],max[50];
int ns,size;
int main(){
int ele,ch,skn;
init();
printf("Enter the number of Stacks\n");
scanf("%d",&ns);
size = 50/ns;
createstack();
printf("::Stack Operations::\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
do{
printf("Enter your choice : \n");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\nEnter the stack no : \n");
scanf("%d",&skn);
printf("\nEnter the element : \n");
scanf("%d",&ele);
push(ele,skn);
break;
case 2:
printf("Enter the stack no to pop : \n");
scanf("%d",&skn);
pop(skn);
break;
case 3:
printf("\nEnter the stack no to display : \n");
scanf("%d",&skn);
display(skn);
break;
case 4:
printf("\nProgram Terminating");
break;
default:
printf("\nInvalid Option\n");
}
}while(ch!=4);
return 0;
}
void init(void){
int i;
for(i=0;i<50;++i){
stack[i]=min[i]=max[i] = 0;
top[i]=-1;
}
}
void createstack(){
int i ;
min[0]= -1;
max[0] = size -1;
top[0]=-1;
for(i=1;i<ns;++i){
min[i]= min[i-1] + size;
top[i] = min [i];
}
for(i=1;i<ns;++i){
max[i]= min[i+1];
}
}
void push(int ele,int k){
if(top[k-1]==max[k-1]){
printf("Stack no %d is full i.e overflow\n",k);
return;
}
++top[k-1];
stack[top[k-1]] = ele;
}
void pop(int k){
if(top[k-1]==min[k-1]){
printf("Stack no %d is empty i.e underflow\n",k);
return;
}
printf("%d from stack %d is deleted:\n",stack[top[k-1]],k);
--top[k-1];
}
void display(int k){
int j;
if(top[k-1]==min[k-1]){
printf("\nStack no %d is empty\n",k);
return;
}
printf("\nStack %d -> ",k);
for(j=min[k-1]+1;j<=top[k-1];++j){
printf("%d ",stack[j] );
}
printf("\n");
}
#include<stdlib.h>
int stack[50],top[50],min[50],max[50];
int ns,size;
int main(){
int ele,ch,skn;
init();
printf("Enter the number of Stacks\n");
scanf("%d",&ns);
size = 50/ns;
createstack();
printf("::Stack Operations::\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
do{
printf("Enter your choice : \n");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\nEnter the stack no : \n");
scanf("%d",&skn);
printf("\nEnter the element : \n");
scanf("%d",&ele);
push(ele,skn);
break;
case 2:
printf("Enter the stack no to pop : \n");
scanf("%d",&skn);
pop(skn);
break;
case 3:
printf("\nEnter the stack no to display : \n");
scanf("%d",&skn);
display(skn);
break;
case 4:
printf("\nProgram Terminating");
break;
default:
printf("\nInvalid Option\n");
}
}while(ch!=4);
return 0;
}
void init(void){
int i;
for(i=0;i<50;++i){
stack[i]=min[i]=max[i] = 0;
top[i]=-1;
}
}
void createstack(){
int i ;
min[0]= -1;
max[0] = size -1;
top[0]=-1;
for(i=1;i<ns;++i){
min[i]= min[i-1] + size;
top[i] = min [i];
}
for(i=1;i<ns;++i){
max[i]= min[i+1];
}
}
void push(int ele,int k){
if(top[k-1]==max[k-1]){
printf("Stack no %d is full i.e overflow\n",k);
return;
}
++top[k-1];
stack[top[k-1]] = ele;
}
void pop(int k){
if(top[k-1]==min[k-1]){
printf("Stack no %d is empty i.e underflow\n",k);
return;
}
printf("%d from stack %d is deleted:\n",stack[top[k-1]],k);
--top[k-1];
}
void display(int k){
int j;
if(top[k-1]==min[k-1]){
printf("\nStack no %d is empty\n",k);
return;
}
printf("\nStack %d -> ",k);
for(j=min[k-1]+1;j<=top[k-1];++j){
printf("%d ",stack[j] );
}
printf("\n");
}
Enter the number of Stacks
2
::Stack Operations::
1. Push
2. Pop
3. Display
4. Exit
Enter your choice :
1
Enter the stack no :
1
Enter the element :
2
Enter your choice :
1
Enter the stack no :
1
Enter the element :
4
Enter your choice :
1
Enter the stack no :
1
Enter the element :
6
Enter your choice :
3
Enter the stack no to display :
1
Stack 1 -> 2 4 6
Enter your choice :
3
Enter the stack no to display :
2
Stack no 2 is empty
Enter your choice :
1
Enter the stack no :
2
Enter the element :
1
Enter your choice :
1
Enter the stack no :
2
Enter the element :
3
Enter your choice :
3
Enter the stack no to display :
2
Stack 2 -> 1 3
Enter your choice :
4
Program Terminating
2
::Stack Operations::
1. Push
2. Pop
3. Display
4. Exit
Enter your choice :
1
Enter the stack no :
1
Enter the element :
2
Enter your choice :
1
Enter the stack no :
1
Enter the element :
4
Enter your choice :
1
Enter the stack no :
1
Enter the element :
6
Enter your choice :
3
Enter the stack no to display :
1
Stack 1 -> 2 4 6
Enter your choice :
3
Enter the stack no to display :
2
Stack no 2 is empty
Enter your choice :
1
Enter the stack no :
2
Enter the element :
1
Enter your choice :
1
Enter the stack no :
2
Enter the element :
3
Enter your choice :
3
Enter the stack no to display :
2
Stack 2 -> 1 3
Enter your choice :
4
Program Terminating