-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_a_queue.cpp
89 lines (77 loc) · 1.18 KB
/
reverse_a_queue.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
#include<iostream>
using namespace std;
class Queue
{
private:
int front;
int rear;
int a[];
int size;
public:
Queue(int s) //constructor to set size of array
{
size=s;
front=-1;
rear=-1;
}
int empty() //checks if queue is empty or not
{
if(front==-1 && rear==-1)
return 1;
else
return 0;
}
int full() //checks if queue is full or not
{
if(rear==size-1)
return 1;
else
return 0;
}
void enqueue(int data) //insert data in queue
{
if(empty())
{
front=rear=0;
}
if(full())
{
return;
}
else
{
rear=rear+1;
}
a[rear]=data;
}
void reverse()
{
for(int i=rear;i>=front+1;i--)
{
cout<<a[i]<<" ";
}
}
void show() //print data in the queue
{
for(int i=front+1;i<=rear;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
};
int main()
{
Queue q(7);
q.enqueue(2);
q.enqueue(3);
q.enqueue(-4);
q.enqueue(5);
q.enqueue(-6);
cout<<"data in queue: ";
q.show();
cout<<endl;
cout<<"queue after reversing: ";
q.reverse();
return 0;
}