Archived

This topic is now archived and is closed to further replies.

joeyg2477

Linked List Help!!!

Recommended Posts

Ok i have a project for school and am needing someone to atleast lead me in the right direction. We are doing linked list right now at school and i am needing help writing a insertion function dealing with linked list. This is the code I have now: //insertnode function definition void list::insertnode(char Char) { //grab a block of memory and make p point to it node *p; p = new node; //assign info field of node to char p->info = Char; //assign pointer part of node p->next =first; //have first point to p first = p; } Well this puts the list in backwards order. So if i input GAME it would output EMAG. What I need to do is switch the code around so that it will print in the right order. I have been trying to solve this for awhile now, so now i come to you guys for help. Thanks for all who respond..

Share this post


Link to post
Share on other sites
consider using a pointer last. Just as first points to the first node, last will point to the last. Then you would just have to change
 //assign pointer part of node

p->next =first;

//have first point to p

first = p;

to

//assign pointer part of node

last->next = p;
last = last->next;

of course, you will have to check that last!=NULL
if it does then simply
first = last = new node;

Well, this is what I use. I''m still learning linked lists myself, so I could be wrong. Hope this helps.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The insertion type you''re doing is known as a head-insert. What you ought to do is develop a body-insert. A head insert puts the new data at the front of the list, while a body insert puts the new data after a given node. In your case, you would perform successive body inserts while passing in the tail of the list each time. Note that you will need to make your list representation semi-opaque to do this, as in your body insert function will have a pointer-to-node parameter. Or, you could maintain a tail pointer and just write a tail insert function.

Share this post


Link to post
Share on other sites
Instead of writing a general insert method, you could save time by writing an append method. Since that is what you are really trying to do.

Share this post


Link to post
Share on other sites
what aaron_ds said:

traverse the list until you get to the end
(searching_p->next == NULL)
then point the tail to your new node
(searching_p->next = new_p
and cap the new tail
(new_p->next = NULL

Or, if you want your program to die on large lists, you could create a recursive print function [8^)


void PrintList (node* p)
{
if (p->next != NULL)
PrintList (p->next);
cout << p->Info;
return;
}


lonesock

Piranha are people too.

Share this post


Link to post
Share on other sites
I understand what yall are saying (i think ) but i''m not trying to add to an existing list.... im trying to figure out how to initial the list to insert to the right of the previous node.....Is this going to need a while loop;

Share this post


Link to post
Share on other sites