-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.cpp
162 lines (142 loc) · 3.57 KB
/
function.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
#include "function.h"
void Implement::InsertBack(int data)
{
if(!IsEmpty()){
Node* current = head;
while(current->next != NULL){
current = current->next;
}
current->next = new Node(data, NULL);
}else{
head = new Node(data, NULL);
}
}
void Implement::InsertFront(int data)
{
if(!IsEmpty()){
Node* newNode = new Node(data, head);
head = newNode;
}else
head = new Node(data, NULL);
}
void Implement::InsertAfter(int data,int data_ref)
{
Node* current = head;
while(current != NULL){
if(current->data == data_ref){
current->next = new Node(data, current->next);
break;
}else{
current = current->next;
}
}
return;
}
void Implement::InsertBefore(int data,int data_ref)
{
Node* current = head;
if(!IsEmpty()){
if (head->data == data_ref) {
Node* temp = new Node(data, head);
head = temp;
return;
}
while(current->next != NULL){
if(current->next->data == data_ref){
current->next = new Node(data, current->next);
break;
}else{
current = current->next;
}
}
}else
return;
}
void Implement::Delete(int data)
{
Node* current = head;
Node* previous = NULL;
while (current != 0 && current->data != data) { // Traversal
previous = current; // 如果current指向NULL
current = current->next; // 或是current->data == x
} // 即結束while loop
if(current == NULL)
return;//no such node to delete or lits is empty
else if (current == head){
head = current -> next;
delete current;
current = NULL;
}
else{
previous->next = current->next;
delete current;
current = NULL;
}
}
void Implement::DeleteFront()
{
Node* current = head;
if(!IsEmpty()){
head = current -> next;
delete current;
current = NULL;
}
}
void Implement::DeleteBack()
{
if (IsEmpty()) {
return;
}
if (head->next == NULL) {
delete head;
head = NULL;
return;
}
Node* current = head;
Node* previous = NULL;
while(current->next != NULL){
previous = current;
current = current->next;
}
previous->next = NULL;
delete current;
current = NULL;
}
void Implement::Reverse(){
Node* current = head;
if(current == NULL || current->next == NULL){
return;
}
Node* previous = NULL;
Node* preceding = head->next;
while (preceding != NULL) {
current->next = previous; // 把current->next轉向
previous = current; // previous往後挪
current = preceding; // current往後挪
preceding = preceding->next; // preceding往後挪
} // preceding更新成NULL即跳出while loop
current->next = previous;
head = current;
}
void Implement::Rotate(int k)
{
if (k == 0)
return;
Node* current = head;
int count = 1;
while (current->next != NULL)
{
++count;
current = current->next;
}
k = k % count;
current->next = head;
Node* temp = head;
Node* previous = NULL;
for (int i = 0; i < count - k; i++) {
previous = temp;
temp = temp -> next;
}
head = temp;
previous->next = NULL;
}