-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoomList.h
79 lines (70 loc) · 1.99 KB
/
RoomList.h
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
#ifndef ROOMLIST_H
#define ROOMLIST_H
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#include "RoomNode.h"
// Define a class for linked list
class RoomList
{
private:
RoomNode *head; // Pointer to the head of the list
RoomNode *tail; // Pointer to the tail of the list
public:
// Default constructor that sets the head and tail pointers to null
RoomList() : head(nullptr), tail(nullptr) {}
// Destructor that deletes all the nodes in the list
~RoomList()
{
RoomNode *current = head;
while (current != nullptr)
{
RoomNode *next = current->getNext();
delete current;
current = next;
}
}
// Insert a node at the end of the list
void insertNode(string roomName, string description, Item *item, Treasure *treasure)
{
RoomNode *newNode = new RoomNode(roomName, description, item, treasure);
if (head == nullptr)
{ // If the list is empty
head = newNode;
tail = newNode;
}
else
{ // If the list is not empty
tail->setNext(newNode);
newNode->setPrev(tail);
tail = newNode;
}
}
// Insert a hidden node after a given room name in the list
void insertHidden(string hiddenRoomName, string description, Item *item, Treasure *treasure, string roomName)
{
RoomNode *newNode = new RoomNode(hiddenRoomName, description, item, treasure);
RoomNode *temp = head;
while (temp->getRoomName() != roomName && temp != nullptr)
temp = temp->getNext(); // Set temp to the node with the given room name (library)
if (temp->getHidden() == nullptr)
{ // If there is no hidden node after temp
temp->setHidden(newNode);
newNode->setPrev(temp);
}
else
{ // If there is already a hidden node after temp
newNode->setNext(temp->getHidden());
temp->getHidden()->setPrev(newNode);
temp->setHidden(newNode);
newNode->setPrev(temp);
}
}
// Get the head pointer of the list
RoomNode *getHead() const
{
return head;
}
};
#endif