-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkList.cpp
194 lines (175 loc) · 5.62 KB
/
LinkList.cpp
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
//========================================================
// Evan Lenahan
// April 2022
// listdriver.cpp
// This file tests the List class.
//========================================================
template <class T>
List<T>::List (void)
{
head = NULL;
}
template <class T>
List<T>::List(const List<T> &mylist)
{
head = NULL;
Node * currentNode = mylist.head;
while (currentNode != NULL)
{
append(currentNode->item); // Append each item in mylist to the new list
currentNode = currentNode->next; // Move to the next node in mylist
}
}
template <class T>
List<T>::~List ( void )
{
clear();
}
template <class T>
List<T> List<T>::operator=(const List<T> &mylist)
{
clear(); // Call the clear() function to remove all items in the list
Node *currentNode = mylist.head;
while (currentNode != NULL)
{
append(currentNode->item); // Append each item in mylist to the new list
currentNode = currentNode->next; // Move to the next node in mylist
}
return *this; // Return a reference to the modified list
}
template <class T>
string List<T>::to_string(void) const
{
string str;
if (head == NULL)
{
str = "0";
return str;
}else
{
Node *currentNode = head;
while (currentNode != NULL){
str = str + std::to_string(currentNode->item) + " "; // Convert the item to a string and append it to the result string
currentNode = currentNode->next; // Move to the next node in the list
}
return str;
}
}
template <class T>
void List<T>::append(const T &item)
{
Node *newNode = new Node;
newNode->item = item; // Set the item value of the new node
newNode->next = NULL;
if (head == NULL)
{
head = newNode; // If the list is empty, set the head to the new node
}else{
Node *currentNode = head;
while (currentNode->next != NULL)
{
currentNode = currentNode->next; // Move to the last node in the list
}
currentNode->next = newNode; // Set the next pointer of the last node to the new node
}
}
template <class T>
T &List<T>::operator[](int index)
{
Node *currentNode = head;
for (int i = 0; i < index; i++)
{
currentNode = currentNode->next; // Move to the node at the specified index
}
return currentNode->item; // Return a reference to the item value of the node at the specified index
}
template <class T>
void List<T>::insert(const T &item, int index)
{
Node *currentNode = head; // Initialize currentNode as head
if (index == 0)
{
Node *newNode = new Node; // Create a new node
newNode->item = item; // Set the item of the new node to the input item
newNode->next = head; // Set the next pointer of the new node to the previous head
head = newNode; // Update the head pointer to point to the new node
}else
{
for (int i = 0; i < index - 1; i++)
{
currentNode = currentNode->next; // Traverse to the node before the insertion point
}
Node *newNode = new Node; // Create a new node
newNode->item = item; // Set the item of the new node to the input item
newNode->next = currentNode->next; // Set the next pointer of the new node to the node that was originally after currentNode
currentNode->next = newNode; // Update the next pointer of currentNode to point to the new node
}
}
template <class T>
void List<T>::remove(int index)
{
if (head == NULL) // Check if the list is empty
{
return;
}
Node *currentNode = head; // Initialize currentNode as head
if (index == 0)
{
Node *tempNode = head; // Create a temporary node that points to the head node
head = head->next; // Update the head pointer to point to the next node
delete tempNode;
}else
{
Node *currentNode = head; // Initialize currentNode as head
for (int i = 0; i < index - 1; i++)
{
currentNode = currentNode->next; // Traverse to the node before the node to be removed
}
Node *tempNode = currentNode->next; // Create a temporary node that points to the node to be removed
currentNode->next = tempNode->next;
delete tempNode;
}
}
template <class T>
List<T> List<T>::operator+(const List<T> &mylist) const
{
List<T> newList = *this;
Node *currentNode = mylist.head; // Initialize a currentNode to the head of the input list
while (currentNode != NULL) // Go through the input list and add each node to the new list
{
newList.append(currentNode->item);
currentNode = currentNode->next;
}
return newList;
}
template <class T>
int List<T>::length(void) const
{
int length = 0;
Node *currentNode = head; // Initialize currentNode as head
while (currentNode != NULL) // Go through the list and increment length for each node
{
currentNode = currentNode->next;
length++;
}
return length;
}
template <class T>
bool List<T>::isEmpty(void) const
{
return (head == NULL); // Check if the head pointer is NULL and return true if it is, false otherwise
}
template <class T>
void List<T>::clear(void)
{
Node *tempNode;
Node *currentNode = head;
// loop through each node in the list until there are no more nodes
while (currentNode != NULL)
{
tempNode = currentNode; // set the tempNode pointer to the current node to be deleted
currentNode = currentNode->next; // set the currentNode pointer to the next node in the list
delete tempNode; // delete the node pointed to by tempNode
}
head = NULL;
}