Need Method for Linked List
Hi everyone!
I've created a linked list with the help of dynamic memory. I've got already functions like "add new node","delete list","print list". But what I need is a function that would turn the list "around". That is - if the list was previously like 1,2,3,4,5 then after the function "turnaround" it should be like 5,4,3,2,1.
I'd appreciate any help you guys can give me on this one!
Thanks,
Edijs
[448 byte] By [
edijs.vee] at [2007-11-19 19:42:52]

# 1 Re: Need Method for Linked List
Then What's the Problem you are Getting know as Reverse linked List.Can you Post your Code how u r trying to do this.to make it in easy way i will suggest to use vector so easily you can perform your calculations without any problem.
and for this please show your code how u r trying to do this.
and tell what type of list it is Sinle Double or circular
thanx.
# 2 Re: Need Method for Linked List
Ok,here's the code:
void apgr(){
node *tmp,*tmp2;
if (p == NULL)//goto last node
{ cout << "\nList is empty\n";
}
else
{ tmp2 = p;
while (tmp2->nxt != NULL)
{ tmp2 = tmp2->nxt;
}
tmp=tmp2;
}//tmp now points to last node
tmp2=p;
while(tmp2->nxt!=tmp){//goto beforelast node
tmp2=tmp2->nxt;
}//tmp2 now points to the beforelast node
cout << tmp2->num;
tmp->nxt=tmp;
}
This function is not yet completed, obviously, but I imagine,that it could start like that.
# 3 Re: Need Method for Linked List
If its a Doubly linked list then i guess you should have no problem in reversing it as it only depends on the way ur traverse it ...
else fpr a singly linked list ...one way 2 go about would be to swap the whole list....that is first and the last , second and the second last and so on.. ...
on an after thought...why not just reverse the values in the nodes rather than the nodes ?
# 4 Re: Need Method for Linked List
on an after thought...why not just reverse the values in the nodes rather than the nodes ?
Not a good idea, IMO. You should refrain from putting in such dependencies... it may work under the given conditions but kills code re-usability. Regards.
# 5 Re: Need Method for Linked List
I forgot to mention that it's a one-way linked list.
# 6 Re: Need Method for Linked List
Why call the function agrp? What's wrong with reverse or invert? How am I supposed to know what "agrp" means?
# 7 Re: Need Method for Linked List
To: NMTop40
actually "apgr" is a abbreviation of "apgriezt" which in my native language means "reverse". That's all.
# 8 Re: Need Method for Linked List
I can give you pseudocode
Go linearly through nodes, and for each node:
make the next node point to the current node
// be careful with pointer modifications (temporary pointer variables are needed).
Finally, the initial pointer must points to the old last node (or to NULL if the list is empty)
Note : You should pass the p pointer as a parameter of the apgr function, not an ugly global variable!
And, the new pointer to the first node should be either returned by the function, or its modification must be reflected to the caller, using a pointer to pointer to node as parameter:
For instance, here are the two possible prototypes for your function:
void apgr(node**);
Or:
node* apgr(node*);