-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularQueue_practice.c++
71 lines (62 loc) · 1.06 KB
/
circularQueue_practice.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
#include <iostream>
using namespace std;
class CircularQueue{
int *arr;
int front;
int rear;
int size;
public:
CircularQueue(int n)
{
size = n;
arr = new int[size];
front = rear = 0;
}
bool enqueue(int data)
{
if((front == 0 && rear == size-1) || (rear == (front-1)%(size-1)))
{
return false;
}
else if(front == -1)
{
front = rear = 0;
}
else if((front != 0) && (rear = size-1))
{
rear = 0;
}
else
{
rear++;
}
arr[rear] = data;
return true;
}
int deque()
{
if(front == -1)
{
return -1;
}
int ans = arr[front];
arr[front] = -1;
if(front == rear)
{
front = rear = -1;
}
else if(front == size - 1)
{
front = 0;
}
else
{
front++;
}
return ans;
}
};
int main()
{
return 0;
}