-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.h
More file actions
125 lines (101 loc) · 3.04 KB
/
Queue.h
File metadata and controls
125 lines (101 loc) · 3.04 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
Queue.h
Author: [Your Name]
Roll Number: [Your Roll Number]
Project Title: Xonix Game - Data Structures and Algorithms Project
Description:
This file contains the implementation of a simple Queue data structure.
The Queue is used for the Game Room functionality, where players wait
in a First-In-First-Out (FIFO) manner for matchmaking.
*/
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using namespace std;
// Maximum number of players that can wait in the queue
#define MAX_QUEUE_SIZE 100
// Structure to store player information in the queue
struct QueueNode {
int playerID; // Unique identifier for the player
// Default constructor
QueueNode() {
playerID = -1;
}
// Parameterized constructor
QueueNode(int id) {
playerID = id;
}
};
class Queue {
private:
QueueNode items[MAX_QUEUE_SIZE]; // Array to store queue elements
int front; // Index of the front element
int rear; // Index of the rear element
int size; // Current number of elements in the queue
public:
// Constructor
Queue() {
front = 0;
rear = -1;
size = 0;
}
// Returns true if the queue is empty
bool isEmpty() {
return size == 0;
}
// Returns true if the queue is full
bool isFull() {
return size == MAX_QUEUE_SIZE;
}
// Returns the current size of the queue
int getSize() {
return size;
}
// Enqueue a new player into the queue
void enqueue(int playerID) {
if (isFull()) {
cout << "Queue is full. Cannot add more players." << endl;
return;
}
// Increase rear circularly
rear = (rear + 1) % MAX_QUEUE_SIZE;
items[rear] = QueueNode(playerID);
size++;
}
// Dequeue a player from the queue (FIFO)
QueueNode dequeue() {
if (isEmpty()) {
cout << "Queue is empty." << endl;
return QueueNode();
}
QueueNode frontItem = items[front];
// Increase front circularly
front = (front + 1) % MAX_QUEUE_SIZE;
size--;
return frontItem;
}
// Peek at the front player in the queue without removing
QueueNode peek() {
if (isEmpty()) {
cout << "Queue is empty." << endl;
return QueueNode();
}
return items[front];
}
// Display the queue (for debugging)
void display() {
if (isEmpty()) {
cout << "Queue is empty." << endl;
return;
}
cout << "Queue (Game Room Waiting List):" << endl;
int count = 0;
int i = front;
while (count < size) {
cout << "Position " << (count + 1) << ": PlayerID " << items[i].playerID << endl;
i = (i + 1) % MAX_QUEUE_SIZE;
count++;
}
}
};
#endif // QUEUE_H