forked from pavini11/hello12345
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue using stack.c
100 lines (90 loc) · 1.58 KB
/
queue using 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
#include <stdio.h>
#define N 5
int s1[N], s2[N];
int top1=-1, top2=-1;
int count=0;
void push1(int data)
{
if (top1==N-1)
printf("Overflow");
else
s1[++top1]=data;
}
int pop1()
{
return s1[top1--];
}
void push2(int data)
{
if (top2==N-1)
printf("Overflow");
else
s2[++top2]=data;
}
int pop2()
{
return s2[top2--];
}
void enqueue(int x)
{
push1(x);
count++;
}
void dequeue()
{
if (top1==-1 && top2==-1)
printf("Empty");
else
{
int i;
for(i=0; i<count; i++)
push2(pop1());
int b=pop2();
printf("Dequeued element is %d\n", b);
count--;
for (i=0; i<count; i++)
push1(pop2());
}
}
void display()
{
int i;
for (i=0; i<=top1; i++)
printf("%d ", s1[i]);
printf("\n");
}
int main()
{
while (1)
{
printf("Press 1 to add 2 to delete and 3 to print else 4");
int choice;
scanf("%d", *choice);
switch(choice)
{
case 1:
{
printf("Enter value");
int val;
scanf("%d", &val);
enqueue(val);
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
display();
break;
}
case 4:
{
exit(0);
}
}
}
return 0;
}