-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_stack.c
106 lines (103 loc) · 1.57 KB
/
multi_stack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#define max 10
int top1, top2, b1, b2, arr[max];
void push();
void pop();
void display();
void main()
{
int ch;
top1=b1=-1;
top2=b2=max-1/2;
do{
printf("\n 1:push\n 2:pop\n 3:display\n 4:exit\n choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:printf("exiting from program\n");
break;
default:printf("wrong choice\n");
break;
}
}while(ch!=4);
}
void push()
{
int x,n, ch;
printf("enter a no \n");
scanf("%d",&x);
printf("\n press 1 to push in stack1 or press 2 for stack2:");
scanf("%d",&ch);
if(ch==1){
if(top1==b2)
{
printf("stack overflow \n");
return;
}
else
arr[++top1]=x;
}
if(ch==2)
{
if(top2==n-1)
{
printf("stack overflow \n");
return;
}
else
arr[++top2]=x;
}
}
void pop()
{
int y,ch;
printf("\n press 1 to pop from stack1 or press 2 for stack2");
scanf("%d",&ch);
if(ch==1)
{
if(top1==-1)
{
printf("stack underflow\n");
return;
}
y=arr[top1];
arr[top1--]=0;
}
else{
if(top2==b2)
{
printf("stack underflow\n");
return;
}
y=arr[top2];
arr[top2--]=0;
}
printf("\n%d element is successfully poped from stack \n", y);
return;
}
void display()
{
int i;
if (top1 == -1)
printf("stack 1 is empty \n");
else{
printf("elements of Stack 1 are : \n");
for (i = 0; i<= top1; i++)
printf("%d\n",arr[i]);
}
if(top2 == b2)
printf("stack 2 is empty \n");
else
{
printf("elements of Stack 2 are : \n");
for (i =b2+1; i<=top2; i++)
printf("%d\n",arr[i]);
}
return ;
}