-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircluarqueue.cpp
More file actions
72 lines (69 loc) · 1.76 KB
/
Copy pathcircluarqueue.cpp
File metadata and controls
72 lines (69 loc) · 1.76 KB
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
class CircularQueue{
int *arr;
int front,rear;
int size;
public:
// Initialize your data structure.
CircularQueue(int n){
// Write your code here.
size=n;
front=-1;
rear=-1;
arr=new int[size];
}
// Enqueues 'X' into the queue. Returns true if it gets pushed into the stack, and false otherwise.
bool enqueue(int value){
// Write your code here.
if((front==0 && rear==size-1)||(rear==(front-1)%(size-1)))
{
//check for full
return false;
}
else if(front==-1)
{
//first ele to push: queue was empty
front=rear=0;
}
else if(rear==size-1 && front!=0)
{
//rear last me and front kahi bich me
rear=0; //pointing rear to 0th index
}
else
{
//normal case: to push ele
rear++; //increment rear
}
arr[rear]=value;
return true;
}
// Dequeues top element from queue. Returns -1 if the stack is empty, otherwise returns the popped element.
int dequeue(){
// Write your code here.
//check if queue is empty
if(front==-1)
{
return -1;
}
int ans=arr[front];
arr[front]=-1 ; //pop ele
if(front==rear)
{
//single ele is present
front=-1;
rear=-1;
}
else if(front==size-1)
{
//front is at last index of queue
//maintain cyclic nature
front=0;
}
else
{
//normal case
front++;
}
return ans;
}
};