-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular-Queue.cpp
More file actions
43 lines (40 loc) · 1.26 KB
/
Copy pathCircular-Queue.cpp
File metadata and controls
43 lines (40 loc) · 1.26 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
// https://www.codingninjas.com/studio/problems/circular-queue_1170058?leftPanelTab=0&campaign=LoveBabbar_Codestudio&utm_source=youtube&utm_medium=affiliate&utm_campaign=LoveBabbar_Codestudio
#include <bits/stdc++.h>
class CircularQueue{
public:
vector<int> arr;
int front, rear, size;
CircularQueue(int n){
size = n;
front = rear = -1;
arr.resize(n, -1);
}
// Enqueues 'X' into the queue. Returns true if it gets pushed into the stack, and false otherwise.
bool enqueue(int value){
if(front == -1){
front = rear = 0;
arr[rear] = value;
return true;
}else{
if((rear+1)%size == front) return false;
rear = (rear+1) % size;
arr[rear] = value;
return true;
}
}
// Dequeues top element from queue. Returns -1 if the stack is empty, otherwise returns the popped element.
int dequeue(){
if(front == -1) return -1;
else{
if(front == rear){
int x = arr[front];
front = rear = -1;
return x;
}else{
int x = arr[front];
front = (front+1)%size;
return x;
}
}
}
};