forked from a-r-nida/HactoberFest2020-Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainqusingstack.c
144 lines (127 loc) · 1.72 KB
/
mainqusingstack.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <stdlib.h>
#define N 5
int top1=-1,top2=-1;
int s1[N]={},s2[N]={};
void push(int val,int n)
{
if(n==-1)
{
if(top1!=N-1)
{
s1[++top1]=val;
}
else
{
printf("\n Queue is full");
}
}
else
{
s2[++top2]=val;
}
}
int pop(int n)
{
int temp;
if(n==-1)
{
temp=s1[top1--];
}
else
{
temp=s2[top2--];
}
return temp;
}
void insertvalue(int value)
{
push(value,1);
}
int deletevalue()
{
int temp;
if(top1==-1&&top2==-1)
{
printf("Queue is empty");
}
else
{
if(top2!=-1)
{
pop(2);
}
else
{
while(top1!=-1)
{
temp=pop(1);
push(temp,2);
}
pop(2);
}
}
}
void display()
{
int i;
for(i=top2;i>-1;i--)
{
printf("%d\n",s2[i]);
}
for(i=0;i<=top1;i++)
{
printf("%d\n",s1[i]);
}
printf("\n");
}
int main(int argc, char *argv[]) {
int n,value,temp;
printf("\n 1.Enqueue \t\n 2.Dequeue \t\n 3.Display \t\n 4.EXIT \t\n");
do
{
printf("\nEnter choice = ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\n Enter value to be inserted = ");
scanf("%d",&value);
insertvalue(value);
break;
case 2:
temp=deletevalue();
printf("%d is deleted\n",temp);
break;
case 3:
display();
break;
case 4:
printf("EXIT");
}
}while(n!=4);
return 0;
}
/* ""OUTPUT""
1.Enqueue
2.Dequeue
3.Display
4.EXIT
Enter choice = 1
Enter value to be inserted = 10
Enter choice = 1
Enter value to be inserted = 20
Enter choice = 1
Enter value to be inserted = 30
Enter choice = 3
30
20
10
Enter choice = 2
30 is deleted
Enter choice = 3
20
10
Enter choice = 4
EXIT
*/