-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter3-singly-linked-list.cpp
129 lines (115 loc) · 2.09 KB
/
chapter3-singly-linked-list.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
// My implementation for singly linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int _val = 0): val(_val), next(nullptr) {};
};
class SinglyLinkedList {
public:
SinglyLinkedList() {
m_size = 0;
m_head = nullptr;
m_tail = nullptr;
}
void insertFront(int val) {
if (m_size == 0) {
m_head = m_tail = new ListNode(val);
} else {
ListNode *ptr = new ListNode(val);
ptr->next = m_head;
m_head = ptr;
}
++m_size;
}
void insertBack(int val) {
if (m_size == 0) {
m_head = m_tail = new ListNode(val);
} else {
m_tail->next = new ListNode(val);
m_tail = m_tail->next;
}
++m_size;
}
void insertNode(int pos, int val) {
int i;
if (i <= 0) {
insertFront(val);
} else if (i >= m_size) {
insertBack(val);
} else {
ListNode *ptr1, *ptr2;
ptr1 = m_head;
for (i = 0; i < pos - 1; ++i) {
ptr1 = ptr1->next;
}
ptr2 = new ListNode(val);
ptr2->next = ptr1->next;
ptr1->next = ptr2;
++m_size;
}
}
void deleteNode(int pos) {
if (pos < 0 || pos > m_size - 1) {
return;
}
ListNode *ptr1, *ptr2;
if (pos == 0) {
ptr1 = m_head;
if (m_size == 1) {
m_head = m_tail = nullptr;
} else {
m_head = m_head->next;
}
delete ptr1;
} else {
ptr1 = m_head;
for (int i = 0; i < pos - 1; ++i) {
ptr1 = ptr1->next;
}
ptr2 = ptr1->next;
ptr1->next = ptr2->next;
if (ptr2->next == nullptr) {
m_tail = ptr1;
}
delete ptr2;
}
--m_size;
}
void updateNode(int pos, int val) {
if (pos < 0 || pos > m_size - 1) {
return;
}
ListNode *ptr = m_head;
for (int i = 0; i < pos; ++i) {
ptr = ptr->next;
}
ptr->val = val;
}
ListNode *findNode(int val) {
ListNode *ptr = m_head;
while (ptr != nullptr) {
if (ptr->val == val) {
return ptr;
}
ptr = ptr->next;
}
return nullptr;
}
~SinglyLinkedList() {
ListNode *ptr = m_head;
while (m_head != nullptr) {
m_head = m_head->next;
delete ptr;
ptr = m_head;
}
m_head = m_tail = nullptr;
}
private:
int m_size;
ListNode *m_head;
ListNode *m_tail;
};
int main()
{
return 0;
}