I assume that add_back() adds a node to the end of the list. Yes?
The following function (untested) should do the trick, but first here's the
sequence of events you need to go through to do it. It's best to write out
in plain english what you want to happen before writing any code. It helps
prevent errors and makes you sure of what you are doing.
1. Create new node.
2. Make new node an end node (by making next NULL in your case)
3. Put data into new node.
4. Get the list (in your case the list pointer to the first node in the
list)
5. If no list exists then make new node the first one and goto 8 (exit).
(in other words make sure list isn't NULL)
6. Find the end of the list (you didn't do that in your version)
7. Link new node onto end of list.
8. Return new node (optional, but nice), or NULL for failure.
Most of that is pretty basic, except for number 6, which needs a little more
detail.
1. Get start node in the list and make it the current one.
2. If there is no next node in current, goto 5.
3. Get the next node and make it currant.
4. Goto 2.
5. The current node is the end node.
These two numberd lists are known as Algorithms (you probably already know
this), which is a description of a sequence of events. From these you can
draw a couple of flowcharts showing the sequence of events graphically. You
can then take them and convert them to some form of pseudo code, where you
add known structures to the code i.e. if-then-else, for-next, while-do, and
repeat-until (do-while in C/C++) loops, and such. I'll skip all that. Also
note that when you get a case of expanding out a statement in one algorithm
into another algorithm, then that new algorithm is a candidate for being
made into a function of it's own. I'm sorry if this is all old stuff to you,
but I felt you needed to understand algorithms and linked-lists better. What
you are doing shows that you aren't quite certain of what is supposed to
take place.
The code I'd make from that is as follows:
node* add_back(node *&list, int info)
// note that I'm returning a pointer
{
node *end = new node;
/* if new fails it should throw an exception and exit the program
but that depends upon how up to date the compiler is
older compilers will make the pointer NULL */
if(end == NULL)
return NULL; // failure
// Initialise end
end->next = NULL;
end->data = info;
node *skip = list; // get start point
if(skip != NULL)
{
// find current end of list
while(skip->next != NULL)
skip = skip->next; // number 6
skip->next = end; // add new end node
}
else
{
// no nodes so make list the new node
list = end;
}
// All done, just return end.
return end;
}
I hope that helps. The main thing you forgot was that you needed to find the
end of the list. I recomend that you learn more about how a linked list
works. The usual examples involve diagrams with boxes and arrows.
Jason.
PS: Yet again I've written an essay when I didn't need to. I hope all you
people apreciate how much free time I have on my hands at the moment :-D
----- Original Message -----
From: "chromatonus 712" <chr...@ho...>
To: <dev...@li...>
Sent: Sunday, November 11, 2001 9:54 PM
Subject: [Dev-C++] adding nodes to the end of linked list
> hi, my add_back function only adds one number to the list, it has to add
> nodes until i display the list (not included) , problem with the else
> section and probably more,
>
> thanks!!
>
> struct node
> {
> int data;
> node *next;
> };
>
> node add_back(node *&list, int info)
> {
> node *skip = new node;
> node *tail = new node;
> if (list == NULL)
> {
> tail -> data = info;
> tail -> next = NULL;
> list = tail;
> skip = tail;
> delete tail; // probably wrong...
> }
> else
> {
> tail -> data = info;
> tail -> next = NULL;
> skip -> next = tail;
> skip = tail;
> delete tail;
> }
> }
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|