Actually, it probably segfaults on the printf, where you dereference temp3. I think you should test if test3 is NULL before you dereference it.
Actually, it probably segfaults on the printf, where you dereference temp3. I think you should test if test3 is NULL before you dereference it.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Have you tried quzah's ??
*EditedCode:for( temp = first node; temp; temp = temp2 ) { temp2 = temp->next; temp->next = newlist head; newlist head = temp; }
You have a big problem naming your variables. temp1, temp2, temp3. Confusing aint it??
yes, his last suggestion was to set temp3 to null and change the newlist head's to temp3. This gives a print out of the nodes in their original order.Originally Posted by loko
i only did that to keep inline with quzah's example and if i had any problems it'd be easier to explain.Originally Posted by loko
Actually, it probably segfaults on the printf, where you dereference temp3. I think you should test if test3 is NULL before you dereference it.
segmentation fault without printing nothingCode:charNode *reverse(charNode *current) { charNode *temp2; charNode *temp3 = NULL; for( charNode *temp = current; temp; temp = temp2 ) { temp2 = temp->next; if (temp3 != NULL) { temp->next = temp3; temp3 = temp2; } printf("\n%c%c\n", temp3->numberone, temp3->numbertwo); } return temp3; }
Why did you put the if check in there? That's not right. I give up.
Quzah.
Hope is the first step on the road to disappointment.
i only did that because dwks said i should test if test3 is NULL before i dereference it.
See, it doesn't do any good to test it if you're going to just print it anyway.Code:if( foo ) { } printf( do something with foo no matter what, becaue this is outside the if )
Quzah.
Hope is the first step on the road to disappointment.
indeed. Sorry i forgot to mention, I actually did try placing the printf statement inside the if statement but as usual it gives me a segmentation fault at the last element.
+--+ reversing... +--+
BB
CC
abnormal program termination: segmentation fault
Well I don't know how you can possibly be screwing it up this bad. I can't explain it any more. I'm right. My code is right. You're just messing it up some how. There's no way this should be that hard. My first example was 100% correct.[edit]Code:#include<stdio.h> #include<stdlib.h> struct node { struct node *next; int data; }; void printlist( struct node * ); struct node *revlist( struct node * ); int main( void ) { struct node *list = NULL, *newnode; int x; for( x = 0; x < 10; x++ ) { newnode = malloc( sizeof( *newnode ) ); if( newnode == NULL ) { printf("So sad.\n"); exit( 0 ); } newnode->next = list; newnode->data = x; list = newnode; } printlist( list ); list = revlist( list ); printlist( list ); for( ; list; list = newnode ) { newnode = list->next; free( list ); } return 0; } void printlist( struct node *n ) { for( ; n; n = n->next ) { printf( "data is %d\n", n->data ); } } struct node *revlist( struct node *n ) { struct node *temp = NULL, *temp2 = NULL, *temp3 = NULL; for( temp = n; temp; temp = temp2 ) { temp2 = temp->next; temp->next = temp3; temp3 = temp; } return temp3; }
It doesn't really matter though. If you can't wrap your head around the code, having it doesn't do you any good.
[/edit]
Quzah.
Last edited by quzah; 10-23-2005 at 02:19 AM.
Hope is the first step on the road to disappointment.
the problem was:
i wasn't saving what was happening in "start" (in main)Code:revlist(list);
i changed it to start = revlist(list);
works now, thanks.
another problem was:
i was attempting to print inside the reverse method, the new fully reversed list wasn't being saved. Instead, an incomplete one was printed.Code:printf( "%c %c\n", temp3->numberone, temp3->numbertwo );
You mean sort of like what I already told you to do?Originally Posted by Axel
Originally Posted by quzah
Quzah.
Hope is the first step on the road to disappointment.
Thats the same thing i was telling him in the other threads he made. He thinks that becoz something isnt printing correctly that something is wrong. Well he was printing somthing that is not even related to what he was doing that.Originally Posted by quzah
This thread will just go on and on.
loko, in most cases, when something isn't printed somewhere, i.e. if you stick a printf statement inside a while loop and it's not printing it gives you an idea that the while is evaluating to false and its a great indication that something you assumed in your code was wrong.
Another example is if in an array element X in an array is empty or has the wrong character than you expected, then a printf statement can be quiet handy to debug it and to understand what's actually inside the array.
I may of not been clear with how i explained the problem; sorry about that.
printf is good for testing things in your code. Correct, but thats if you put it in the right place. If you look at quzah's code its a small peice of code and its obvious that itll work. Your printf didnt do its job to tell you if the code works or not.
In your reversing function you dont print the result until the function is done. What your doing is your printing while its reversing. And when you dont get a reverse order in your printing, you say its not working.
What about try to print only when the reversing is done?? Maybe youll get a reverse order?