forked from ASD-ADF/ASD_Task_2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperation.cpp
More file actions
34 lines (29 loc) · 999 Bytes
/
operation.cpp
File metadata and controls
34 lines (29 loc) · 999 Bytes
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
#include "list.h"
#include "operation.h"
void insert_sorted(List &L, infotype x) {
/**
* IS : List may be empty
* PR : insert an infotype x into an already sorted List L
* so that the elements inside List L is still sorted in ascending order,
* procedure must also check if such number is already exists (No Duplicate number),
* allocate new element only if the conditions are met
* FS : elements in List L sorted in ascending order, x is inside List L
*/
address Q,P;
if((first(L)==NULL) || (x<info(first(L)))) {
P = allocate(x);
insertFirst(L,P);
} else if(findElm(L,x)==NULL){
Q = first(L);
while((next(Q)!=NULL) && (x>info(next(Q)))){
Q = next(Q);
if(next(Q)==NULL){
P = allocate(x);
insertLast(L,P);
} else if(info(P)!=info(Q)){
P = allocate(x);
insertAfter(L,P,Q);
}
}
}
}