-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack2.c
97 lines (94 loc) · 2.15 KB
/
stack2.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
#include<stdio.h>
#define MAX_SIZE 50
int stack[MAX_SIZE],top1=-1,top2=MAX_SIZE;
void pop(int stackno){
if (stackno==1){
if (top1==-1){
printf("\nStack 1 empty");
}
printf("The popped element is %d",stack[top1--]);
}
else if(stackno==2){
if (top2==MAX_SIZE){
printf("\nStack 2 empty");
}
printf("The popped element is %d",stack[top2++]);
}
}
void display(int stackno){
if (stackno==1){
if (top1==-1){
printf("\nStack 1 empty");
}
else{
printf("\nElements of stack 1\n");
for(int i=top1;i>=0;i--){
printf("%3d \n",stack[i]);}
}
}
else if(stackno==2){
if (top2==MAX_SIZE)
{
printf("\nStack 2 empty");
}
printf("\nElements of stack 2\n");
for(int i=top2;i<=MAX_SIZE-1;i++)
printf("%3d\n", stack[i]);
}
}
void push(int stackno,int element){
if (stackno==1){
if (top1==top2 - 1){
printf("\nStack 1 full");
}
else{
stack[++top1]=element;
}
}
else if(stackno==2){
if (top2==top1+1){
printf("\nStack 2 full");
}
else{
stack[--top2]=element;
}
}
}
int main()
{
int stackno, choice, element, popped;
printf("Enter stack number (1 / 2): ");
scanf("%d", &stackno);
do
{
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.To change stack number");
printf("\n5.To exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter element to be inserted: ");
scanf("%d",&element);
push(stackno,element);
break;
case 2:
pop(stackno);
break;
case 3:
display(stackno);
break;
case 4:
printf("\nEnter stack number (1 or 2): ");
scanf("%d", &stackno);
break;
case 5:
printf("\nExit from the stack\n");
break;
default:
printf("\nInvalid choice\n");
}
} while(choice!=5);
}