-
Notifications
You must be signed in to change notification settings - Fork 129
/
reversingqueue.cpp
125 lines (117 loc) · 1.83 KB
/
reversingqueue.cpp
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
//reversing queue
#include <bits/stdc++.h>
# define MAX 5
using namespace std;
/* Structure with queue size and pointers */
struct queue
{
int s[MAX];
int front,rear;
}st;
stack <int> stack1;
/* Function to check if the queue is full */
int full()
{
if(st.rear >= MAX - 1)
return 1;
else
return 0;
}
/* Function to check if the queue is empty */
int empty()
{
if(st.front == -1)
return 1;
else
return 0;
}
/* Function to insert elements in a queue */
void enqueue(int num)
{
if(st.front == -1)
st.front++;
st.rear++;
st.s[st.rear] = num;
}
/* Function to delete elements from the queue */
int dequeue()
{
int x;
x = st.s[st.front];
if(st.front==st.rear)
st.front=st.rear=-1;
else
st.front++;
return x;
}
/* Function to display queue elements */
void display()
{
int i;
if(empty())
printf("\nEMPTY QUEUE\n");
else
{
printf("\nQUEUE ELEMENTS : ");
for(i = st.front ; i <= st.rear ; i++)
printf("%d ",st.s[i]);
}
printf("\n");
}
/* Function to reverse a queue using a stack */
void reverse_queue_using_stack()
{
while(!(st.front == st.rear))
{stack1.push(dequeue());}
stack1.push(dequeue());
printf("\nREVERSED QUEUE : ");
while(!stack1.empty())
{
printf("%d ",stack1.top()); // Print the top element of the stack
stack1.pop();
}
printf("\n");
exit(0);
}
/* Main function */
int main()
{
int num,choice;
st.front = st.rear = -1;
printf("\nREVERSING A QUEUE USING STACKS\n");
printf("\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.REVERSE\n5.EXIT\n");
while(1)
{
printf("\nEnter the choice : ");
scanf("%d",&choice);
switch (choice)
{
case 1:
if(full())
{
printf("\nQUEUE IS FULL\n");
}
else
{
printf("\nEnter data : ");
scanf("%d",&num);
enqueue(num);
}
break;
case 2:
if (empty())
{
printf("\nEMPTY QUEUE\n");
}
else
printf("\nDequeued Element : %d",dequeue());
break;
case 3:
display();
break;
case 4: reverse_queue_using_stack();
break;
default: exit(0);
}}
return 0;
}