Mmm...well I had another thread working on trying to compare reversed ints, but I decided to wipe that and try again
i want to compare two linked lists, each storing a digit, that goes backwards
for example, the numbers 127 and 436
linked list a stores 7->2->1, and 6->3->4
my whole method is finished except for the last part where the numbers are same length starting with same digit so the number would be
5496 and 5724
if they are stored backwards, how do i compare them? ive tried a multitude of different ways but they are failing because of pointer issues with linked lists =/
this is the method if you don't get what i mean
Code:
//compares p and q; if p is smaller, then return -1; if q is smaller, return 1
//return 0 if its exactly the same
int compare(struct integer* p, struct integer* q) {
int count=1, count2=1;
struct integer *ptest=p, *qtest=q;
while(ptest->next!=NULL) {
ptest=ptest->next;
count++;
}
printf("count p= %d\n",count);
while(qtest->next!=NULL) {
qtest=qtest->next;
count2++;
}
//PTEST AND QTEST ARE CURRENTLY POINTING TO LAST DIGIT OF P AND Q
printf("count q= %d\n",count2);
if(count<count2) {
printf("p is less than q; returning -1\n");
return -1;
}
else if(count>count2) {
printf("q is less than p; returning 1\n");
return 1;
}
else {
printf("p=%d and q are equal length; no return yet\n",ptest->digit);
if(ptest->digit<qtest->digit) {
printf("returning -1\n");
return -1;
}
else if(ptest->digit>qtest->digit) {
printf("returning 1\n");
return 1;
}
else {
}
return 0;
}
}