forked from ASD-ADF/ASD_Task_3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperation.cpp
More file actions
108 lines (96 loc) · 2.39 KB
/
operation.cpp
File metadata and controls
108 lines (96 loc) · 2.39 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
#include "list.h"
#include "operation.h"
#include "my_data.h"
void insertAndSort(List &L, infotype x) {
/**
* IS : List may be empty
* PR : insert a new element into an already sorted-by-ID List L
* so that the elements inside List L is still sorted by ID.
* procedure must also check if such ID is already exists (No Duplicate ID).
* If new data has duplicate ID, new data is rejected.
* FS : elements in List L sorted by ID, P is inside List L
*/
//-------------your code here-------------
address P = allocate(x);
if (first(L) == NULL || info(first(L)).ID > x.ID)
{
insertFirst(L,P);
}
else
{
address Q = first(L);
while ( next(Q) != NULL && x.ID >= info(next(Q)).ID )
{
Q = next(Q);
}
if (info(Q).ID != x.ID)
{
insertAfter(L,Q,P);
}
else
{
insertLast(L,P);
}
}
//----------------------------------------
}
void deletebyID(List &L, int id_x) {
/**
* IS : List L may be empty
* FS : an element with ID info = id_x is deleted from List L (deallocate)
*/
address Prec, P;
//-------------your code here-------------
P = first(L);
if (id_x == info(P).ID)
{
deleteFirst(L,P);
}
else if ( info(last(L)).ID == id_x)
{
deleteLast(L,P);
}
else
{
address Q;
while (next(Q) != NULL && info(next(Q)).ID != id_x)
{
Q = next(Q);
}
Prec = Q;
deleteAfter(L, Prec,P);
}
//----------------------------------------
}
void savePassedMember(List &L, List &L2){
/**
* IS : List L and L2 may be empty
* FS : any element with score greater than 80 is moved to L2
*/
address P;
//-------------your code here-------------
P = first(L);
address Q = P;
while (Q != NULL)
{
P = Q;
if ( info(P).score > 80)
{
insertAndSort(L2, info(P));
Q = next(Q);
if (prev(P) == NULL)
{
deleteFirst(L,P);
}
else
{
deleteAfter(L,prev(P), P);
}
}
else
{
Q = next(Q);
}
}
//----------------------------------------
}