-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01_queue_fifo_lifo.cpp
More file actions
65 lines (54 loc) · 1.48 KB
/
01_queue_fifo_lifo.cpp
File metadata and controls
65 lines (54 loc) · 1.48 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
#include <iostream>
#include <cstdlib>
#include <string>
#define MAX 100
class Queue {
private:
int data[MAX];
int frontIndex, rearIndex;
public:
Queue() : frontIndex(0), rearIndex(0) {}
bool isEmpty() const { return frontIndex == rearIndex; }
bool isFull() const { return rearIndex == MAX; }
bool enqueue(int value) {
if (isFull()) return false;
data[rearIndex++] = value;
return true;
}
bool dequeue(int &removed) {
if (isEmpty()) return false;
removed = data[frontIndex++];
return true;
}
bool peek(int &value) const {
if (isEmpty()) return false;
value = data[frontIndex];
return true;
}
};
int main(int argc, char* argv[]) {
Queue q;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "dequeue") {
int removed;
if (q.dequeue(removed))
std::cout << "DEQUEUE " << removed << std::endl;
else
std::cout << "DEQUEUE FAILED" << std::endl;
}
else {
int value = std::atoi(arg.c_str());
if (q.enqueue(value))
std::cout << "ENQUEUE " << value << std::endl;
else
std::cout << "ENQUEUE FAILED" << std::endl;
}
}
int frontValue;
if (q.peek(frontValue))
std::cout << "FRONT " << frontValue << std::endl;
else
std::cout << "QUEUE EMPTY" << std::endl;
return 0;
}