-
Notifications
You must be signed in to change notification settings - Fork 0
/
queuearr.cpp
94 lines (85 loc) · 2.5 KB
/
queuearr.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
#include<iostream>
using namespace std;
#define SIZE 10
class Queue
{
public:
Queue()
{
read = write = 0;
}
bool empty()
{
if(read == write)
return true;
return false;
}
bool full()
{
if((write==SIZE-1&&read==0)||(write==read-1))
return true;
return false;
}
void enqueue(int value)
{
if(full())
{
cout<<"Cannot enqueue"<<endl;
return;
}
array[write]=value;
write = (write+1)%SIZE;
}
int dequeue()
{
if(empty())
return -9999;
int ret = array[read];
read = (read+1)%SIZE;
return ret;
}
private:
int array[SIZE];
int read,write;
};
int main()
{
Queue Q;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
Q.enqueue(3);
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
Q.enqueue(3);
Q.enqueue(4);
Q.enqueue(5);
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
Q.enqueue(3);
Q.enqueue(4);
Q.enqueue(5);
Q.enqueue(6);
Q.enqueue(7);
Q.enqueue(8);
Q.enqueue(9);
Q.enqueue(10);
Q.enqueue(11);
Q.enqueue(12);
Q.enqueue(13);
Q.enqueue(14);
cout<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
cout<<Q.dequeue()<<" "<<Q.empty()<<" "<<Q.full()<<endl;
return 0;
}