Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Help: reverse a linked list (http://www.velocityreviews.com/forums/t634084-help-reverse-a-linked-list.html)

Hamster 09-07-2008 07:42 AM

Help: reverse a linked list
 
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.

Obnoxious User 09-07-2008 08:23 AM

Re: Help: reverse a linked list
 
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

Hamster 09-07-2008 08:41 AM

Re: Help: reverse a linked list
 
Fixed it myself, thanks.

Is there any way to delete your own post?

Hamster 09-07-2008 08:18 PM

Re: Help: reverse a linked list
 
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.

Chris M. Thomasson 09-08-2008 01:57 PM

Re: reverse a linked list
 
"Hamster" <freehamster2008@gmail.com> wrote in message
news:7bb690da-eb68-4783-ade0-9e900d03c28c@p10g2000prf.googlegroups.com...
> 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;
}
__________________________________________________ ______


Pascal J. Bourguignon 09-08-2008 03:39 PM

Re: reverse a linked list
 
"Chris M. Thomasson" <no@spam.invalid> writes:

> "Hamster" <freehamster2008@gmail.com> wrote in message
> news:7bb690da-eb68-4783-ade0-9e900d03c28c@p10g2000prf.googlegroups.com...
>> 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),r esult)):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__

Jorgen Grahn 09-08-2008 05:39 PM

Re: Help: reverse a linked list
 
On Sun, 7 Sep 2008 01:41:08 -0700 (PDT), Hamster <freehamster2008@gmail.com> 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!


All times are GMT. The time now is 09:12 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.