help with reversing something in a linklist

This is a discussion on help with reversing something in a linklist within the C Programming forums, part of the General Programming Boards category; Actually, it probably segfaults on the printf, where you dereference temp3. I think you should test if test3 is NULL ...

  1. #31
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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.

  2. #32
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Have you tried quzah's ??
    Code:
    for( temp = first node; temp; temp = temp2 )
    {
        temp2 = temp->next;
        temp->next = newlist head;
        newlist head = temp;
    }
    *Edited

    You have a big problem naming your variables. temp1, temp2, temp3. Confusing aint it??

  3. #33
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by loko
    Have you tried quzah's ??
    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.
    Quote Originally Posted by loko
    Have you tried quzah's ??
    You have a big problem naming your variables. temp1, temp2, temp3. Confusing aint it??
    i only did that to keep inline with quzah's example and if i had any problems it'd be easier to explain.


    Actually, it probably segfaults on the printf, where you dereference temp3. I think you should test if test3 is NULL before you dereference it.

    Code:
    
    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;
    
    }
    segmentation fault without printing nothing

  4. #34
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  5. #35
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    i only did that because dwks said i should test if test3 is NULL before i dereference it.

  6. #36
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( foo )
    {
    }
    printf( do something with foo no matter what, becaue this is outside the if )
    See, it doesn't do any good to test it if you're going to just print it anyway.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #37
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    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

  8. #38
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    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;
    }
    [edit]
    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 03:19 AM.
    Hope is the first step on the road to disappointment.

  9. #39
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    the problem was:

    Code:
    revlist(list);
    i wasn't saving what was happening in "start" (in main)

    i changed it to start = revlist(list);

    works now, thanks.

  10. #40
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    another problem was:

    Code:
        printf( "%c %c\n", temp3->numberone, temp3->numbertwo );
    i was attempting to print inside the reverse method, the new fully reversed list wasn't being saved. Instead, an incomplete one was printed.

  11. #41
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Axel
    the problem was:

    Code:
    revlist(list);
    i wasn't saving what was happening in "start" (in main)

    i changed it to start = revlist(list);

    works now, thanks.
    You mean sort of like what I already told you to do?
    Quote Originally Posted by quzah
    You should be doing:
    Code:
    newlist = reverset( start );
    printList( newlist );

    Quzah.



    Quzah.
    Hope is the first step on the road to disappointment.

  12. #42
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by quzah
    Code:
    if( foo )
    {
    }
    printf( do something with foo no matter what, becaue this is outside the if )
    See, it doesn't do any good to test it if you're going to just print it anyway.


    Quzah.
    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.

    This thread will just go on and on.

  13. #43
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    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.

  14. #44
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    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?

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reversing a string, again..
    By Disident in forum C Programming
    Replies: 5
    Last Post: 06-15-2009, 09:01 PM
  2. Reversing output in this function
    By rogster001 in forum C Programming
    Replies: 8
    Last Post: 02-07-2008, 06:36 AM
  3. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  4. MergeSort implementation with linklist.
    By Kam in forum C Programming
    Replies: 3
    Last Post: 10-21-2002, 12:04 PM
  5. Quick LinkList Help!!adding at the End
    By simly01 in forum C++ Programming
    Replies: 13
    Last Post: 07-28-2002, 08:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21