-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path083_Implement a Queue.cpp
123 lines (85 loc) · 1.85 KB
/
083_Implement a 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// From Scratch
#include <bits/stdc++.h>
class Queue {
private:
int *arr;
int qfront;
int rear;
int size;
public:
Queue() {
// Implement the Constructor
size = 5001;
arr = new int[size];
qfront = rear = 0;
}
/*----------------- Public Functions of Queue -----------------*/
bool isEmpty() {
// Implement the isEmpty() function
return (rear == qfront ? 1 : 0);
}
void enqueue(int data) {
// Implement the enqueue() function
if (rear != size) {
arr[rear] = data;
++rear;
}
}
int dequeue() {
// Implement the dequeue() function
if(rear == qfront)
{
return -1;
}
else
{
int ans = arr[qfront];
++qfront;
if(qfront == rear){
qfront = rear = 0;
}
return ans;
}
}
int front() {
// Implement the qfront() function
if(qfront == rear)
return -1;
return arr[qfront];
}
};
// Using Vector
#include <bits/stdc++.h>
class Queue {
private:
vector<int> v;
public:
Queue() {
// Implement the Constructor
}
/*----------------- Public Functions of Queue -----------------*/
bool isEmpty() {
// Implement the isEmpty() function
return v.empty();
}
void enqueue(int data) {
// Implement the enqueue() function
v.push_back(data);
}
int dequeue() {
// Implement the dequeue() function
int ans = -1;
if(!v.empty())
{
ans = v[0];
v.erase(v.begin());
}
return ans;
}
int front() {
// Implement the front() function
if(!v.empty())
return v[0];
return -1;
}
};