-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventoryList.h
136 lines (126 loc) · 3.18 KB
/
InventoryList.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
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
#ifndef INVENTORYLIST_H
#define INVENTORYLIST_H
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#include "InventoryNode.h"
// Define a class for linked list
class InventoryList
{
private:
InventoryNode *head; // Pointer to the head of the list
InventoryNode *tail; // Pointer to the tail of the list
public:
// Default constructor that sets the head and tail pointers to null
InventoryList() : head(nullptr), tail(nullptr) {}
// Destructor that deletes all the nodes in the list
~InventoryList()
{
InventoryNode *current = head;
while (current != nullptr)
{
head = current->getNext();
delete current;
current = head;
}
}
// Insert a node at the end of the list
void insertNode(Item *item)
{
InventoryNode *newNode = new InventoryNode(item);
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;
}
}
// Delete a node from the list by item name
void deleteNode(string name)
{
InventoryNode *temp = head;
while (temp != nullptr)
{ // Traverse the list until finding the node with the item name
if (temp->getItem()->getName() == name)
{
break;
}
temp = temp->getNext();
}
if (temp == nullptr)
{ // If the node is not found, do nothing
return;
}
else
{ // If the node is found, delete it from the list
if (temp == head && temp == tail)
{ // If the node is the only node in the list
head = nullptr;
tail = nullptr;
}
else if (temp == head)
{ // If the node is the head of the list
head = head->getNext();
head->setPrev(nullptr);
}
else if (temp == tail)
{ // If the node is the tail of the list
tail = tail->getPrev();
tail->setNext(nullptr);
}
else
{ // If the node is in the middle of the list
temp->getPrev()->setNext(temp->getNext());
temp->getNext()->setPrev(temp->getPrev());
}
delete temp; // Free the memory of the node
}
}
// Check if a node exists in the list by item name
bool existsNode(string name)
{
InventoryNode *temp = head;
while (temp != nullptr)
{ // Traverse the list until finding the node with the item name
if (temp->getItem()->getName() == name)
{
return true; // Return true if found
}
temp = temp->getNext();
}
return false; // Return false if not found
}
// Print the list from head to tail
void printList()
{
InventoryNode *temp = head;
if (head == nullptr)
{ // If the list is empty
cout << "There is nothing in your bag." << endl;
return;
}
cout << "\n> Your bag contains: ";
while (temp != nullptr)
{ // If the list is not empty
cout << temp->getItem()->getName();
if (temp->getNext() != nullptr)
{
cout << ", ";
}
temp = temp->getNext();
}
cout << endl;
}
// Get the head pointer of the list
InventoryNode *getHead() const
{
return head;
}
};
#endif