Created
December 6, 2018 13:59
-
-
Save krishnaprajapat/1b567a6d370a3ffaec7a4b087771cd32 to your computer and use it in GitHub Desktop.
Multiple stack handling using single array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#define MAX 10 | |
int arr[MAX],top1=-1,top2=MAX-1; | |
void push_stack1(int item) | |
{ | |
if(top1<top2) | |
{ | |
top1=top1+1; | |
arr[top1]=item; | |
} | |
else | |
printf("\nstack is overflow "); | |
} | |
void push_stack2(int item) | |
{ | |
if(top1<top2-1){ | |
top2=top2-1; | |
arr[top2]=item; | |
} | |
else | |
printf("\n stack overflow "); | |
} | |
void pop_stack1() | |
{ int temp; | |
if(top1>=0) | |
{ | |
temp=arr[top1]; | |
top1=top1-1; | |
} | |
else | |
printf(" \nstack under flow"); | |
} | |
void pop_stack2() | |
{ | |
int temp; | |
if(top2>=MAX-1) | |
{ | |
temp=arr[top2]; | |
top2=top2+1; | |
} | |
else | |
printf("\n underflow"); | |
} | |
void display_1() | |
{int i | |
for(i=0;i<=top1;i++) | |
printf("\n%d",arr[i]); | |
} | |
void display_2() | |
{int i; | |
for(i=MAX-1;i>=top2;i--) | |
printf("\n%d",arr[i]); | |
} | |
int main() | |
{ | |
int ch; | |
printf("\n 1:stack1 push \n 2: stack2push\n 3:stack1p0p\n 4: stack2pop \n 5:stack1display \n 6: stack2display"); | |
while(1) | |
{ | |
printf("\n enter choice"); | |
scanf("%d",&ch); | |
switch(ch) | |
{ | |
case 1: | |
{ | |
int data; | |
printf("\n enter data for stack1"); | |
scanf("%d",&data); | |
push_stack1(data); | |
break; | |
} | |
case 2: | |
{ | |
int data; | |
printf("\n enter data for stack2"); | |
scanf("%d",&data); | |
push_stack2(data); | |
break; | |
} | |
case 3: | |
{ | |
pop_stack1(); | |
break; | |
} | |
case 4: | |
{ | |
pop_stack2(); | |
break; | |
} | |
case 5: | |
{ | |
display_1(); | |
break; | |
} | |
case 6: | |
{ | |
display_2(); | |
} | |
default: | |
printf("\n invalid choice"); | |
} | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment