-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain.cpp
More file actions
38 lines (33 loc) · 872 Bytes
/
chain.cpp
File metadata and controls
38 lines (33 loc) · 872 Bytes
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
#include "chain.h"
chain::chain() : first(nullptr) {}
void chain::insert_front(const chainNode& newNode) {
chainNode* newChainNode = new chainNode(newNode);
if (first == nullptr) {
first = newChainNode;
}
else {
newChainNode->next = first;
first = newChainNode;
}
}
void chain::delete_node(const chainNode& nodeToDelete) {
if (first == nullptr) {
return;
}
chainNode* current = first;
chainNode* previous = nullptr;
while (current != nullptr) {
if (current->data == nodeToDelete.data) {
if (previous == nullptr) {
first = current->next;
}
else {
previous->next = current->next;
}
delete current;
return;
}
previous = current;
current = current->next;
}
}