-
Notifications
You must be signed in to change notification settings - Fork 0
/
04- Queue Using Array.cpp
73 lines (68 loc) · 1.79 KB
/
04- Queue Using Array.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
//in the name of God
#include <iostream>
#include <limits>
using namespace std;
struct Node {
int data;
Node *next;
};
class Queue {
private:
int *list, _front, _rear, size, capacity;
Node* createNode(int data) {
Node *nd = new Node();
nd->data = data;
nd->next = NULL;
return nd;
}
public:
Queue(unsigned int sz) {
this->capacity = sz;
list = new int[sz];
_front = 0, size = 0, _rear = sz - 1;
}
int isFull() {
return size == capacity;
}
int isEmpty() {
return !size;
}
void enqueue(int data) {
if (!isFull()) {
_rear = (_rear + 1) % capacity;
list[_rear] = data;
size = size + 1;
}
}
int dequeue() {
if (!isEmpty()) {
int rs = list[_front];
_front = (_front + 1) % capacity;
size = size - 1;
return rs;
}
return INT_MIN;
}
int front() {
if (!isEmpty()) {
list[_front];
}
return INT_MIN;
}
int rear() {
if (!isEmpty()) {
return list[_rear];
}
return INT_MIN;
}
};
int main() {
Queue *queue = new Queue(1000);
queue->enqueue(10);
queue->enqueue(20);
queue->enqueue(30);
queue->enqueue(40);
queue->enqueue(50);
cout << "Dequeued item is" << queue->dequeue() << endl;
return 0;
}