-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.h
More file actions
238 lines (199 loc) · 5.86 KB
/
LinkedList.h
File metadata and controls
238 lines (199 loc) · 5.86 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
LinkedList.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 Linked List data structure.
The Linked List is used for storing friends lists for each player
and for tracking game tiles in save game functionality.
*/
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;
// Structure for a generic linked list node
// We make it template-based so it can be used for both friend IDs and game tiles
template <typename T>
struct Node {
T data; // Data stored in the node (can be friendID or tile coordinates)
Node<T>* next; // Pointer to the next node
// Constructor
Node(T value) {
data = value;
next = nullptr;
}
};
// The main linked list class
template <typename T>
class LinkedList {
private:
Node<T>* head; // Pointer to the first node in the list
int size; // Number of nodes in the list
public:
// Constructor
LinkedList() {
head = nullptr;
size = 0;
}
// Destructor to free memory
~LinkedList() {
clear();
}
// Add a new node at the beginning of the list
void addFirst(T value) {
Node<T>* newNode = new Node<T>(value);
newNode->next = head;
head = newNode;
size++;
}
// Add a new node at the end of the list
void addLast(T value) {
Node<T>* newNode = new Node<T>(value);
// If the list is empty, the new node becomes the head
if (head == nullptr) {
head = newNode;
}
else {
// Find the last node and add the new node after it
Node<T>* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}
size++;
}
// Remove the first occurrence of a value from the list
bool remove(T value) {
// If the list is empty, nothing to remove
if (head == nullptr) {
return false;
}
// If the head is the node to remove
if (head->data == value) {
Node<T>* temp = head;
head = head->next;
delete temp;
size--;
return true;
}
// Search for the node to remove
Node<T>* current = head;
while (current->next != nullptr && current->next->data != value) {
current = current->next;
}
// If the value was found
if (current->next != nullptr) {
Node<T>* temp = current->next;
current->next = current->next->next;
delete temp;
size--;
return true;
}
return false;
}
// Check if the list contains a specific value
bool contains(T value) {
Node<T>* current = head;
while (current != nullptr) {
if (current->data == value) {
return true;
}
current = current->next;
}
return false;
}
// Get the number of elements in the list
int getSize() {
return size;
}
// Check if the list is empty
bool isEmpty() {
return head == nullptr;
}
bool contains(T value) const {
// Start from the head node
Node<T>* current = head;
// Traverse through the list
while (current != nullptr) {
// If the current node's data matches the value, return true
if (current->data == value) {
return true;
}
// Move to the next node
current = current->next;
}
// If we got here, the value was not found
return false;
}
// Clear the entire list (delete all nodes)
void clear() {
while (head != nullptr) {
Node<T>* temp = head;
head = head->next;
delete temp;
}
size = 0;
}
// Display the list contents (for debugging)
void display() {
Node<T>* current = head;
cout << "List contents: ";
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
// Get the head node of the list for traversal
Node<T>* getHead() const {
return head;
}
// Get the data at a specific index
T get(int index) {
if (index < 0 || index >= size) {
cout << "Index out of bounds" << endl;
// Return a default value (may need adjusting based on T)
return T();
}
Node<T>* current = head;
for (int i = 0; i < index; i++) {
current = current->next;
}
return current->data;
}
};
// Define some specific structs for different use cases
// For friends list
struct FriendNode {
int friendID;
FriendNode() {
friendID = -1;
}
FriendNode(int id) {
friendID = id;
}
// For comparison in contains() method
bool operator==(const FriendNode& other) const {
return friendID == other.friendID;
}
};
// For game tiles
struct TileNode {
int x, y;
int type; // 0: empty, 1: border, 2: player trail
TileNode() {
x = y = type = 0;
}
TileNode(int xPos, int yPos, int tileType) {
x = xPos;
y = yPos;
type = tileType;
}
// For comparison in contains() method
bool operator==(const TileNode& other) const {
return x == other.x && y == other.y && type == other.type;
}
};
#endif // LINKEDLIST_H