-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
177 lines (130 loc) · 2.69 KB
/
Copy pathlinkedlist.cpp
File metadata and controls
177 lines (130 loc) · 2.69 KB
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
#include "linkedlist.h"
#include <stddef.h>
using namespace std;
linkedlist::linkedlist() {
this->head = new node;
this->tail = new node;
this->head->next = this->tail;
this->tail->next = NULL;
this->pos = this->head->next;
this->length = 0;
}
linkedlist::~linkedlist() {
this->deleteAll();
delete this->head;
delete this->tail;
this->length = 0;
}
void linkedlist::append(personInfo &info) {
node *last = this->moveLast();
node *newNode = new node;
newNode->info = new personInfo(info);
newNode->next = last->next;
last->next = newNode;
this->pos = newNode;
this->length++;
}
node* linkedlist::moveToBeforeNode(int index) {
node *target;
int i = 0;
if ( index <= 0 ) {
target = this->head;
} else {
target = this->moveFirst();
while ( i < index - 1 ) {
target = this->moveNext();
i++;
}
}
return target;
}
void linkedlist::insert(int index, personInfo &info) {
node *target;
node *newNode = new node;
target = this->moveToBeforeNode(index);
newNode->info = new personInfo(info);
newNode->next = target->next;
target->next = newNode;
this->length++;
this->pos = newNode;
}
void linkedlist::deleteNode(int index) {
node *before;
node *target;
before = this->moveToBeforeNode(index);
target = before->next;
before->next = target->next;
delete target->info;
target->next = NULL;
delete target;
this->pos = before;
this->length--;
}
void linkedlist::deleteAll() {
if ( this->length == 0 ) {
return ;
}
while ( this->length > 0 ) {
this->deleteNode(0);
}
this->pos = this->head->next;
}
personInfo linkedlist::viewAt(int index) {
personInfo info;
node *target;
target = moveToBeforeNode(index)->next;
info = *(target->info);
this->pos = target;
target = NULL;
return info;
}
node* linkedlist::moveFirst() {
this->pos = this->head->next;
return this->pos;
}
node* linkedlist::moveLast() {
node *target;
if ( this->length <= 0 ) {
target = this->head;
} else {
target = this->moveToBeforeNode(this->length - 1)->next;
}
this->pos = target;
return target;
}
node* linkedlist::moveNext() {
this->pos = this->pos->next;
return this->pos;
}
bool linkedlist::isTail() {
bool isTail = false;
if ( this->pos->next == NULL ) {
isTail = true;
}
return isTail;
}
int linkedlist::get_length() {
return this->length;
}
node* linkedlist::getNode(int index) {
node *target;
target = this->moveToBeforeNode(index)->next;
return target;
}
int linkedlist::findName(string name) {
int index = 0;
bool isFound = false;
this->moveFirst();
while ( this->isTail() != true ) {
if ( *(this->pos->info) == name ) {
isFound = true;
break;
}
this->moveNext();
index++;
}
if ( isFound == false ) {
index = -1;
}
return index;
}