Help: reverse a linked list

Discussion in 'C++' started by Hamster, Sep 7, 2008.

  1. Hamster

    Hamster Guest

    Hi,

    Need help with my code on reversing a singly linked list, here's my
    code:

    void reverseList ( List& listObj)
    {

    ListNode*pHeadnew=listObj.lastPtr, *pTailnew=listObj.firstPtr;

    ListNode* p1=listObj.firstPtr, *p2=listObj.firstPtr->nextPtr, *p3=p2-
    >nextPtr;


    while (p3!=0)
    {
    p2->nextPtr=p1;
    p1=p2;
    p2=p3;
    p3=p3->nextPtr;
    }

    listObj.firstPtr=pHeadnew;
    listObj.lastPtr=pTailnew;
    listObj.lastPtr->nextPtr=0;
    }

    My test code created a 5-node list, after calling this function
    (declared as friend in class List), the resulting list has only 1 node
    left.

    May anyone help spot what is wrong?

    Thanks.
     
    Hamster, Sep 7, 2008
    #1
    1. Advertising

  2. On Sun, 07 Sep 2008 01:41:08 -0700, Hamster wrote:

    > Fixed it myself, thanks.
    >
    > Is there any way to delete your own post?


    If you're religious, you could beg to your deity of choice.
    The answer is no, your post is forever archived.

    --
    OU
     
    Obnoxious User, Sep 7, 2008
    #2
    1. Advertising

  3. Hamster

    Hamster Guest

    Fixed it myself, thanks.

    Is there any way to delete your own post?
     
    Hamster, Sep 7, 2008
    #3
  4. Hamster

    Hamster Guest

    Alf,

    I missed some if/else structure when copying/pasting which left that
    piece of code logically incomplete and confusing.

    Thank you for your time.

    OU,

    Thanks to confirm...would be nice if there were such a feature, imo.
     
    Hamster, Sep 7, 2008
    #4
  5. Re: reverse a linked list

    "Hamster" <> wrote in message
    news:...
    > Hi,
    >
    > Need help with my code on reversing a singly linked list, here's my
    > code:

    ________________________________________________________
    node* reverse(node* list) {
    node* head = NULL;
    while (list) {
    node* const next = list->next;
    list->next = head;
    head = list;
    list = next;
    }
    return head;
    }
    ________________________________________________________
     
    Chris M. Thomasson, Sep 8, 2008
    #5
  6. Re: reverse a linked list

    "Chris M. Thomasson" <> writes:

    > "Hamster" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> Need help with my code on reversing a singly linked list, here's my
    >> code:


    node* reverse1(node* list,node* result){
    if(list){
    node* next=list->next;
    return(reverse1(next,(list->next=result,list)));
    }else{
    return(result);
    }
    }
    node* nreverse(node* list){
    return(reverse1(list,0));
    }
    // Notice that g++ -O3 implements TCO, so there's no recursive call here.


    With better definitions it could be written as:

    node* reverse1(node* list,node* result){
    return(list?reverse1(rest(list),cons(first(list),result)):result); }
    node* reverse(node* list){
    return(reverse1(list,0)); }
    // Still no recursive call, thanks to TCO.


    that is:

    struct node {
    char* first;
    node* rest;
    node(char* aFirst,node* aRest):first(aFirst),rest(aRest){};
    };

    char* first(node* node){return(node->first);}
    ndoe* rest(node* node){return(node->rest);}
    node* cons(char* first,node* rest){return(new node(first,rest));}



    > ________________________________________________________
    > node* reverse(node* list) {
    > node* head = NULL;
    > while (list) {
    > node* const next = list->next;
    > list->next = head;
    > head = list;
    > list = next;
    > }
    > return head;
    > }
    > ________________________________________________________


    This works ok, so what else do you want?

    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Sep 8, 2008
    #6
  7. Hamster

    Jorgen Grahn Guest

    On Sun, 7 Sep 2008 01:41:08 -0700 (PDT), Hamster <> wrote:
    > Fixed it myself, thanks.
    >
    > Is there any way to delete your own post?


    There used to be, but people started disabling it fifteen years ago
    due to abuse. It's called /cancelling/ a posting.

    In either case, it's more polite to show us your error and the
    solution.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
    \X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!
     
    Jorgen Grahn, Sep 8, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Perpetual Snow

    Reverse a linked list

    Perpetual Snow, Nov 26, 2003, in forum: C Programming
    Replies:
    9
    Views:
    3,353
    Derk Gwen
    Nov 27, 2003
  2. RAJASEKHAR KONDABALA

    Reverse search in a singly-linked list

    RAJASEKHAR KONDABALA, Dec 24, 2003, in forum: C Programming
    Replies:
    20
    Views:
    5,880
    saadbinsaulat
    Feb 27, 2011
  3. Daniel
    Replies:
    5
    Views:
    400
  4. fool
    Replies:
    14
    Views:
    540
    Barry Schwarz
    Jul 3, 2006
  5. joshd
    Replies:
    12
    Views:
    699
    John Carson
    Oct 2, 2006
Loading...

Share This Page