forked from ASD-ADF/ASD_Task_4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperation.cpp
More file actions
76 lines (63 loc) · 2.01 KB
/
Copy pathoperation.cpp
File metadata and controls
76 lines (63 loc) · 2.01 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
#include "doublelist.h"
#include "operation.h"
#include "my_data.h"
#include "my_data2.h"
void insertAndSort(List &L, address P) {
/**
* IS : List may be empty
* PR : insert an element pointed by P 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)
* FS : elements in List L sorted by ID, P is inside List L
*/
//-------------your code here-------------
//address Fo = findElm(L,info(P));
//if(Fo==NULL){
if(first(L)==NULL){
insertFirst(L,P);
}else if(L.first->info.id >= P->info.id){
insertFirst(L,P);
}else if(L.first->info.id <= P->info.id){
address Q = first(L);
address F = Q;
if(first(L)==last(L)){
insertLast(L,P);
}else{
while(Q!=NULL){
if(Q->info.id <= P->info.id){
F = Q;
}
Q = next(Q);
}
insertAfter(L,F,P);
}
}
//}else{
// cout<<"Id is taken"<<endl;
//}
//----------------------------------------
}
void deletebyID(List &L, infotype x) {
/**
* IS : List L may be empty
* FS : an element with ID info = x.id is deleted from List L (deallocate)
*/
address Prec, P;
//-------------your code here-------------
if(first(L)!=NULL){
Prec = findElm(L,x);
if(Prec==first(L)){
deleteFirst(L,P);
cout<<"Data with ID : "<<P->info.id<<" Deleted "<<endl;
}else if(Prec==last(L)){
deleteLast(L,P);
cout<<"Data with ID : "<<P->info.id<<" Deleted "<<endl;
}else{
deleteAfter(L,prev(Prec),P);
cout<<"Data with ID : "<<P->prev->info.id<<" Deleted "<<endl;
}
}else{
cout<<"List is Empty"<<endl;
}
//----------------------------------------
}