Click here to Login

Multiple Stack Using a single array


Multiple stack using Single array

#include
#include>
int arr[100];
int top1,top2,maxsize;
int isEmpty1(){
if(top1==-1){
return 1;
}
else
return 0;
}
int isFull1(){
if(top1==maxsize/2-1){
return 1;
}
else
return 0;
}

void push1(int x){
if(isFull1()){
printf("stack1 is Full");
}
else{
top1++;
arr[top1]=x;
}
}
void pop1(){
if(isEmpty1()){
printf("Stack is empty");
}
else{
top1--;
}
}
void traverse1(){
int i;
if(isEmpty1()){
printf("Stack is empty");
}
else{
for(i=0;i<=top1;i++){
printf("%d",arr[i]);
}
}
}

int isEmpty2(){
if(top2==-maxsize/2-1){
return 1;
}
else
return 0;
}
int isFull2(){
if(top2==maxsize-1){
return 1;
}
else
return 0;
}

void push2(int x){
if(isFull2()){
printf("stack1 is Full");
}
else{
top2++;
arr[top2]=x;
}
}
void pop2(){
if(isEmpty2()){
printf("Stack is empty");
}
else{
top2--;
}
}
void traverse2(){
int i;
if(isEmpty2()){
printf("Stack is empty");
}
else{
for(i=maxsize/2;i<=top2;i++){
printf("%d",arr[i]);
}
}
}

main()
{
int x,opt;
printf("enter the size of array");
scanf("%d",&maxsize);
top1=-1;
top2=maxsize/2-1;
do{
printf("1. PUSH1   2.POP1 3.TRAVERSE1 4.PUSH2 5.POP2 6.TRAVERS2 7.EXIT");
scanf("%d",&opt);
switch(opt){
case 1:
printf("enter elemnet ");
scanf("%d",&x);
push1(x);
break;
case 2:
pop1();
break;
case 3:
traverse1();
break;
case 4:
printf("enter elemnet ");
scanf("%d",&x);
push2(x);
break;
case 5:
pop2();
break;
case 6:
traverse2();
break;

}
}while(opt!=7);
getch();
}

Sample Output

enter the size of array10
1. PUSH1        2.POP1  3.TRAVERSE1             4.PUSH2         5.POP2  6.TRAVER
S2      7.EXIT1
enter elemnet 10
1. PUSH1        2.POP1  3.TRAVERSE1             4.PUSH2         5.POP2  6.TRAVER
S2      7.EXIT1
enter elemnet 20
1. PUSH1        2.POP1  3.TRAVERSE1             4.PUSH2         5.POP2  6.TRAVER
S2      7.EXIT3
10201. PUSH1    2.POP1  3.TRAVERSE1             4.PUSH2         5.POP2  6.TRAVER
S2      7.EXIT4
enter elemnet 50
1. PUSH1        2.POP1  3.TRAVERSE1             4.PUSH2         5.POP2  6.TRAVER
S2      7.EXIT4
enter elemnet 60
1. PUSH1        2.POP1  3.TRAVERSE1             4.PUSH2         5.POP2  6.TRAVER
S2      7.EXIT6
50601. PUSH1    2.POP1  3.TRAVERSE1             4.PUSH2         5.POP2  6.TRAVER
S2      7.EXIT


0 comments:

Post a Comment