-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStuLinkedList.cpp
More file actions
239 lines (228 loc) · 5.43 KB
/
StuLinkedList.cpp
File metadata and controls
239 lines (228 loc) · 5.43 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
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
/*********************************************************
* file name: StuLinkedList.cpp
* programmer name: Jack Kwok
* date created:4/16/2020
* date of last revision:4/16/2020
* details of the revision: None
* short description: funtions for linked list structure of students
**********************************************************/
#include "StuLinkedList.h"
#include <cassert>
size_t list_length(const Stu* head_ptr)
{
assert(head_ptr != NULL);
const Stu* cursor = head_ptr;
size_t answer = 0;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link)
{
answer++;
}
return answer;
}
void list_head_insert(Stu*& head_ptr, const Student& entry)
{
Stu* temp;
temp = new Stu;
temp->data = entry;
temp->link = head_ptr;
head_ptr = temp;
}
void list_insert(Stu* previous_ptr, Student& entry)
{
Stu* temp = new Stu;
temp->data = entry;
temp->link = previous_ptr->link;
previous_ptr->link = temp;
}
Stu* list_search(Stu* head_ptr, const string& targetName)
{
assert(head_ptr != NULL);
Stu* cursor = NULL;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link)
{
//looks by the student's last name
if (cursor->data.getLastName() == targetName)
{
return cursor;
}
}
return cursor;
}
const Stu* list_search(const Stu* head_ptr, const string& targetName){
assert(head_ptr != NULL);
const Stu* cursor = NULL;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link)
{
//looks by the student's last name
if (cursor->data.getLastName() == targetName)
{
return cursor;
}
}
return cursor;
}
Stu* list_locate(Stu* head_ptr, std::size_t position)
{
if (head_ptr == NULL || position <= 0)
return NULL;
Stu* cursor = head_ptr;
for (size_t i = 1; i < position && cursor != NULL; i++)
{
cursor = cursor->link;
}
return cursor;
}
const Stu* list_locate(const Stu* head_ptr, std::size_t position)
{
if (head_ptr == NULL || position <= 0)
return NULL;
const Stu* cursor = head_ptr;
for (size_t i = 1; i < position && cursor != NULL; i++)
{
cursor = cursor->link;
}
return cursor;
}
void list_head_remove(Stu*& head_ptr) {
Stu* newList = head_ptr->link;
delete head_ptr;
head_ptr = newList;
//Stu* remove = head_ptr;
//head_ptr = head_ptr->link;
//delete remove;
}
void list_remove(Stu* head_ptr, const string& entry) {
assert(head_ptr != NULL);
Stu* cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link) {
if (cursor->link->data.getLastName() == entry) {
Stu* temp;
temp = cursor->link;
cursor->link = temp->link;
delete temp;
}
cursor = cursor->link;
}
}
void list_copy(const Stu* source_ptr, Stu*& head_ptr, Stu*& tail_ptr)
{
head_ptr = tail_ptr = NULL;
if (source_ptr == NULL)
return;
head_ptr = new Stu;
head_ptr->data = source_ptr->data;
tail_ptr = head_ptr;
for (source_ptr = source_ptr->link, source_ptr != NULL; source_ptr = source_ptr->link;)
{
tail_ptr->link = new Stu;
tail_ptr->link->data = source_ptr->data;
tail_ptr = tail_ptr->link;
}
}
void list_clear(Stu*& head_ptr)
{
while (head_ptr != NULL)
{
list_head_remove(head_ptr);
}
}
void list_sort(Stu*& head_ptr, int sortSel) {
Stu* cursor = head_ptr;
int count = 0;
while (cursor != NULL) {
count++;
cursor = cursor->link;
}
if (sortSel == 1) {
for (int i = count; i > 1; i--) {
Stu* temp = NULL, * swap1;
swap1 = head_ptr;
for (int j = 0; j < count - 1; j++) {
if (swap1->data.getLastName() > swap1->link->data.getLastName()) {
Stu* swap2 = swap1->link;
swap1->link = swap2->link;
swap2->link = swap1;
if (swap1 == head_ptr) {
head_ptr = swap2;
swap1 = swap2;
}
else {
swap1 = swap2;
temp->link = swap2;
}
}
temp = swap1;
swap1 = swap1->link;
}
}
}
if (sortSel == 2) {
for (int i = count; i > 1; i--) {
Stu* temp = NULL, * swap1;
swap1 = head_ptr;
for (int j = 0; j < count - 1; j++) {
if (swap1->data.getStuID() > swap1->link->data.getStuID()) {
Stu* swap2 = swap1->link;
swap1->link = swap2->link;
swap2->link = swap1;
if (swap1 == head_ptr) {
head_ptr = swap2;
swap1 = swap2;
}
else {
swap1 = swap2;
temp->link = swap2;
}
}
temp = swap1;
swap1 = swap1->link;
}
}
}
if (sortSel == 3) {
for (int i = count; i > 1; i--) {
Stu* temp = NULL, * swap1;
swap1 = head_ptr;
for (int j = 0; j < count - 1; j++) {
if (swap1->data.getIntendedMajor() > swap1->link->data.getIntendedMajor()) {
Stu* swap2 = swap1->link;
swap1->link = swap2->link;
swap2->link = swap1;
if (swap1 == head_ptr) {
head_ptr = swap2;
swap1 = swap2;
}
else {
swap1 = swap2;
temp->link = swap2;
}
}
temp = swap1;
swap1 = swap1->link;
}
}
}
if (sortSel == 4) {
for (int i = count; i > 1; i--) {
Stu* temp = NULL, * swap1;
swap1 = head_ptr;
for (int j = 0; j < count - 1; j++) {
if (swap1->data.getStatus() > swap1->link->data.getStatus()) {
Stu* swap2 = swap1->link;
swap1->link = swap2->link;
swap2->link = swap1;
if (swap1 == head_ptr) {
head_ptr = swap2;
swap1 = swap2;
}
else {
swap1 = swap2;
temp->link = swap2;
}
}
temp = swap1;
swap1 = swap1->link;
}
}
}
}