-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstackUsingQueue.c
125 lines (103 loc) · 1.49 KB
/
stackUsingQueue.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
// Stack Using Queue
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define CAPACITY 5
int f1 = -1;
int r1 = -1;
int f2 = -1;
int r2 = -1;
int queue1[CAPACITY];
int queue2[CAPACITY];
bool isEmpty1()
{
if (f1 == -1 || f1 > r1)
{
return true;
}
return false;
}
bool isEmpty2()
{
if (f2 == -1 || f2 > r2)
{
return true;
}
return false;
}
bool isFull()
{
if (r1 == CAPACITY - 1)
{
return true;
}
return false;
}
void enQueue(int data)
{
if (isFull())
{
printf("Stack is Full");
return;
}
if (f1 == -1)
{
f1++;
}
while (!isEmpty1())
{
if (f2 == -1)
{
f2++;
}
queue2[++r2] = queue1[r1--];
}
queue1[++r1] = data;
while (!isEmpty2())
{
if (f1 == -1)
{
f1++;
}
queue1[++r1] = queue2[r2--];
}
}
void deQueue()
{
if (isEmpty1())
{
printf("Queue is Empty");
return;
}
f1++;
}
int peek()
{
if (isEmpty1())
{
printf("Queue is Empty");
exit(0);
}
return queue1[f1];
}
void printQueue()
{
for (int i = r1; i >= f1; i--)
{
printf("%d ", queue1[i]);
}
printf("\n");
}
void main()
{
enQueue(5);
enQueue(15);
enQueue(20);
enQueue(25);
enQueue(30);
printQueue();
deQueue();
deQueue();
printQueue();
printf("Element at first : %d\n", peek());
}