-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.cpp
More file actions
100 lines (85 loc) · 1.75 KB
/
Copy pathQueue.cpp
File metadata and controls
100 lines (85 loc) · 1.75 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
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
#include "Queue.h"
#include <iostream>
using namespace std;
//Array Queue
QueueArray::QueueArray(int size) {
capacity=size;
arr=new int[capacity];
front = 0;
rear = -1;
count=0;
}
QueueArray::~QueueArray() {
delete[] arr;
}
bool QueueArray::isEmpty() {
return count==0;
}
bool QueueArray::isFull() {
return count==capacity;
}
void QueueArray::enqueue(int x) {
if (isFull()) {
cout << "Full\n";
return;
}
rear = (rear + 1) % capacity;
arr[rear] = x;
count++;
}
int QueueArray::dequeue() {
if (isEmpty()) {
cout << "Empty\n";
return -1;
}
int x = arr[front];
front = (front + 1) % capacity;
count--;
return x;
}
int QueueArray::peek() {
if (isEmpty()) {
cout << "Queue is empty!" << endl;
return -1;
}
cout << "Front value is: " << arr[front] << endl;
return arr[front];
}
//Linked List Queue
QueueLL::QueueLL() : front(nullptr), rear(nullptr) {}
QueueLL::~QueueLL() {
while (!isEmpty())
dequeue();
}
void QueueLL::enqueue(int value) {
Node* newNode = new Node{value, nullptr};
if (rear == nullptr) {
front = rear = newNode;
return;
}
rear->next = newNode;
rear = newNode;
}
int QueueLL::dequeue() {
if (isEmpty()) {
cout << "Queue Empty\n";
return -1;
}
Node* temp = front;
int val = temp->data;
front = front->next;
if (front == nullptr) rear = nullptr;
delete temp;
return val;
}
bool QueueLL::isEmpty() {
return front == nullptr;
}
int QueueLL::peek() {
if (isEmpty()) {
cout << "Queue is empty!" << endl;
return -1;
}
cout << "Front value is: " << front->data << endl;
return front->data;
}