-
Notifications
You must be signed in to change notification settings - Fork 0
/
Implementation of circular queue.cpp
102 lines (94 loc) · 1.69 KB
/
Implementation of circular 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
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
using namespace std;
#define MAX 5
class queue{
private:
int a[MAX];
int Front,rear;
public:
queue()
{
Front=rear=-1;
}
int isempty()
{
if(Front==-1&&rear==-1)
return true;
else
return false;
}
int isfull()
{
if((rear+1)%MAX==Front)
return true;
else
return false;
}
void Enqueue(int n)
{
if(isfull())
cout<<"Queue is full"<<endl;
else if(isempty())
{
Front=0;
rear=0;
}
else
{
rear=(rear+1)%MAX;
}
a[rear]=n;
}
void Dequeue()
{
if(isempty())
cout<<"Queue is empty"<<endl;
else if(Front==rear)
{
Front=rear=-1;
}
else
{
Front=(Front+1)%MAX;
}
}
int top()
{
if(isempty())
{
cout<<"Queue is empty"<<endl;
}
else
return a[Front];
}
void display()
{
if(isempty())
{
cout<<"Queue is empty"<<endl;
}
else
{ int c=(rear+MAX-Front)%MAX+1;
for(int i=0;i<c;i++)
{
int index=(Front+i)%MAX;
cout<<a[index]<<" ";
}
}
}
};
int main()
{
queue a;
a.Enqueue(1);
a.Enqueue(6);
a.Enqueue(8);
a.Enqueue(3);
a.Enqueue(23);
a.display();
cout<<endl;
a.Dequeue();
a.Enqueue(4);
a.display();
return 0;
}