2010-09-15, 08:39 PM
Yeah, my code's kinda messed up cause I had one of the if statements backwards.
I should probably also note that although I have used "<-" in pseudocode, I mean it as being equivalent to the assignment operator (=), not some pointer thing.
There's also the version for people who like playing fast and dangerous with concepts like true and false... this may not actually return +1 and -1 but some other positive/negative number...
Code:
compare (*p, *q) {
if (p == NULL) return -1;
if (q == NULL) return 1;
// or vice versa
if (p->next == NULL && q -> next == NULL)
c = 0;
else
c = compare(p->next, q->next);
if (c == 0) {
if (p > q) return 1;
if (q > p) return -1;
else return 0;
}
else return c;
}I should probably also note that although I have used "<-" in pseudocode, I mean it as being equivalent to the assignment operator (=), not some pointer thing.
There's also the version for people who like playing fast and dangerous with concepts like true and false... this may not actually return +1 and -1 but some other positive/negative number...
Code:
compare (*p, *q) {
if (p == NULL) return -1;
if (q == NULL) return 1;
// or vice versa
if (p->next == NULL && q -> next == NULL)
return (p>q)-(q>p);
else
c = compare(p->next, q->next);
if (c==0) return (p>q)-(q>p);
}
else return c;
}
