-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinkedList.cpp
277 lines (224 loc) · 6.49 KB
/
LinkedList.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// A class template for holding a linked list.
// The node type is also a class template.
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
//*********************************************
// The ListNode class creates a type used to *
// store a node of the linked list. *
//*********************************************
template <class T>
class ListNode
{
public:
T value; // Node value
ListNode<T> *next; // Pointer to the next node
// Constructor
ListNode(T nodeValue)
{
value = nodeValue;
next = nullptr;
}
ListNode()
{
next = nullptr;
}
};
//*********************************************
// LinkedList class *
//*********************************************
template <class T>
class LinkedList
{
private:
ListNode<T> *head; // List head pointer
public:
// Constructor
LinkedList()
{
head = nullptr;
}
// Destructor
~LinkedList();
// Linked list operations
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList() const;
};
//**************************************************
// appendNode appends a node containing the value *
// pased into newValue, to the end of the list. *
//**************************************************
template <class T>
void LinkedList<T>::appendNode(T newValue)
{
ListNode<T> *newNode; // To point to a new node
ListNode<T> *nodePtr; // To move through the list
// Allocate a new node and store newValue there.
newNode = new ListNode<T>(newValue);
//cout << newNode->value << endl; system("pause");
// If there are no nodes in the list
// make newNode the first node.
if (!head)
{
head = newNode;
//cout << newValue << ":" << head->next << " is first node \n"; system("pause");
}
else// Otherwise, insert newNode at end.
{
// Initialize nodePtr to head of list.
nodePtr = head;
//cout << "Initialize nodePtr to head of list: " << nodePtr->next << endl; system("pause");
// Find the last node in the list.
while (nodePtr->next)
{
nodePtr = nodePtr->next;
}
// Insert newNode as the last node.
nodePtr->next = newNode;
//cout << "New node: " << newNode << endl;
//cout << "Prev node linked to :" << nodePtr->next << endl;
//system("pause");
}
}
//**************************************************
// displayList shows the value stored in each node *
// of the linked list pointed to by head. *
//**************************************************
template <class T>
void LinkedList<T>::displayList() const
{
ListNode<T> *nodePtr; // To move through the list
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr points to a node, traverse
// the list.
while (nodePtr)
{
// Display the value in this node.
cout << nodePtr->value << endl;
// Move to the next node.
nodePtr = nodePtr->next;
}
}
//**************************************************
// The insertNode function inserts a node with *
// newValue copied to its value member. *
//**************************************************
template <class T>
void LinkedList<T>::insertNode(T newValue)
{
ListNode<T> *newNode; // A new node
ListNode<T> *nodePtr; // To traverse the list
ListNode<T> *previousNode = nullptr; // The previous node
//cout << "Insert a node " << newValue << "*****" << endl;
// Allocate a new node and store newValue there.
newNode = new ListNode<T>(newValue);
// If there are no nodes in the list
// make newNode the first node
if (!head)
{
head = newNode;
newNode->next = nullptr;
}
else // Otherwise, insert newNode
{
// Position nodePtr at the head of list.
nodePtr = head;
// Initialize previousNode to nullptr.
previousNode = nullptr;
// Skip all nodes whose value is less than newValue.
while (nodePtr != nullptr && nodePtr->value < newValue)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If the new node is to be the 1st in the list,
// insert it before all other nodes.
if (previousNode == nullptr)
{
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise insert after the previous node.
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
//*****************************************************
// The deleteNode function searches for a node *
// with searchValue as its value. The node, if found, *
// is deleted from the list and from memory. *
//*****************************************************
template <class T>
void LinkedList<T>::deleteNode(T searchValue)
{
ListNode<T> *nodePtr; // To traverse the list
ListNode<T> *previousNode; // To point to the previous node
previousNode = new ListNode<T>(); // Instantiate a node for previousNode
//cout << "delete: " << searchValue << endl;
// If the list is empty, do nothing.
if (!head)
{
return;
}
// Determine if the first node is the one.
if (head->value == searchValue)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is
// not equal to num.
while (nodePtr != nullptr && nodePtr->value != searchValue)
{
previousNode = nodePtr; // link to perv node
nodePtr = nodePtr->next; // link to next node
//cout << "searching node: " << nodePtr->value << endl;
}
// If nodePtr is not at the end of the list,
// link the previous node to the node after
// nodePtr, then delete nodePtr.
if (nodePtr)
{
//cout << "at node: " << nodePtr->value << endl;
//cout << "prev node: " << previousNode->value << endl;
previousNode->next = nodePtr->next; // Link prev node to next node
delete nodePtr; // Remove current node
}
else
{
cout << "at end or not found" << endl;
}
}
}
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
template <class T>
LinkedList<T>::~LinkedList()
{
ListNode<T> *nodePtr; // To traverse the list
ListNode<T> *nextNode; // To point to the next node
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr is not at the end of the list...
while (nodePtr != nullptr)
{
// Save a pointer to the next node.
nextNode = nodePtr->next;
// Delete the current node.
delete nodePtr;
// Position nodePtr at the next node.
nodePtr = nextNode;
}
}
#endif