forked from musa-1410/Stumps_Cricket_Simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattingOrderQueue.cpp
More file actions
81 lines (70 loc) · 2.09 KB
/
BattingOrderQueue.cpp
File metadata and controls
81 lines (70 loc) · 2.09 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
#include <iostream>
#include "Player.cpp"
using namespace std;
// Linked List Node for Queue
struct QueueNode {
Player data;
QueueNode* next;
QueueNode(Player p) : data(p), next(nullptr) {}
};
// Queue Implementation
class LinkedListQueue {
private:
QueueNode* frontNode; // Pointer to the front node
QueueNode* rearNode; // Pointer to the rear node
int size; // To track the size of the queue
public:
LinkedListQueue() : frontNode(nullptr), rearNode(nullptr), size(0) {}
// Enqueue: Add a player to the rear of the queue
void push(Player p) {
QueueNode* newNode = new QueueNode(p);
if (rearNode == nullptr) {
frontNode = rearNode = newNode;
} else {
rearNode->next = newNode;
rearNode = newNode;
}
size++;
}
// Dequeue: Remove a player from the front of the queue (does not return anything)
void pop() {
if (empty()) {
throw std::runtime_error("Queue is empty. Cannot pop.");
}
QueueNode* temp = frontNode;
frontNode = frontNode->next;
if (frontNode == nullptr) {
rearNode = nullptr; // Queue is now empty
}
delete temp;
size--;
}
// Front: Get the player at the front of the queue
Player& front() {
if (empty()) {
throw std::runtime_error("Queue is empty. Cannot access front.");
}
return frontNode->data;
}
// Back: Get the player at the rear of the queue
Player& back() {
if (empty()) {
throw std::runtime_error("Queue is empty. Cannot access back.");
}
return rearNode->data;
}
// Is Empty: Check if the queue is empty
bool empty() const {
return frontNode == nullptr;
}
// Size: Get the current size of the queue
int sizeQueue() const {
return size;
}
// Destructor: Clean up memory
~LinkedListQueue() {
while (!empty()) {
pop();
}
}
};