- #1
evinda
Gold Member
MHB
- 3,836
- 0
Hi! (Smile)
Given two sorted lists $M_1, M_2$, I want to write an algorithm, that creates a new list $M_3$, that contains the elements of $M_1$, that do not exist in $M_2$.
That's what I have tried:
Could you tell me if it is right or if I have done something wrong? (Thinking)
Is there also a better way to do this?
Given two sorted lists $M_1, M_2$, I want to write an algorithm, that creates a new list $M_3$, that contains the elements of $M_1$, that do not exist in $M_2$.
That's what I have tried:
Code:
Algorithm(M1,M2){
pointer q=M1, p=M2, M3;
int same=0;
if (M1==NULL) return error;
else if (M2==NULL) return M1;
while (q!=NULL){
while (p!=NULL){
if (p->data==q->data){
same=1;
}
p=p->next;
}
if (same==0){
M3=q;
M3=M3->next;
}
q=q->next;
}
return M3;
Could you tell me if it is right or if I have done something wrong? (Thinking)
Is there also a better way to do this?