• Create Account

Like
0Likes
Dislike

# Introduction to Pointers, Structures and Linked-Lists Part 3

By Chris Bennett aka Dwarfsoft | Published Sep 30 2004 04:41 AM in General Programming

list linked structure null char structures current struct lists
 If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource

Phew, we are now up to the third part of this introduction. The dirty part is Linked-Lists. Truly, though, linked lists are not that bad, and are very beneficial for a LOT of reasons. They are
more dynamic than arrays in that you can keep expanding a linked list until your RAM runs out, and they are also good for collection of relevant data into one structure. Anyway, let us have a bit of

Figure 1 - Simple setup for a linked list structure

The above setup is our structure. It is just a visual representation where Member Data (and the box below it) are just the variables that you have in your structure, and Next is a pointer to a
structure of the same type. Next is required in a linked list, but there may be more. A single linked list is a list where there is only one join between two structures of the same type in the list.
There can be double linked lists, where there is a dual bond between the two structures (usually 'previous' and 'next') or there can be dynamic linked lists (like a current project that I am doing)
which has a dynamic value between 0 and [theoretically] infinite links between structures (or nodes).

Anyway, here is how you define the structure that is listed above, continuing on with our address book idea:



{

<span class="codekeyword">char</span> Name[30];

<span class="codekeyword">char</span> PhoneNo[10];



been defined yet. There is a way to get AddressPtr_t defined at the same time as using it in the structure. This is mainly for ensuring consistency.



{

<span class="codekeyword">char</span> Name[30];

<span class="codekeyword">char</span> PhoneNo[10];



This is legal and it does work, more importantly, you can use this method to link between different structures by defining pointers to structures before the structures themselves. Unlike in
PASCAL, in C/C++ you can define the pointer to a structure ANYWHERE outside of a code block. I recommend that you define all of your typedefs in a header file though, for ease of including.

And from that structure we can do this:

Figure 2 - A link from one element in the list to the next

This can be condinued for eternity (or until you run out of memory) and will work. So how do we get about doing this? First, I would like you to take note of this. Whenever you create a linked
list, reset all of its pointers to NULL, just to be safe. If you do this, then we can safely parse through the list until we hit a NULL dead end. In the above pictures, take it that a 'Next' box
without an arrow from it contains a NULL.





Now, when you want to go through your list, you can use the following code (assuming that you have included 'stdio.h' or 'stdlib.h')



<span class="codekeyword">while</span> (Current)

{

Current->PhoneNo);

Current = Current->Next;

}



The reason why the line 'while (Current)' works is because it keeps going until the pointer 'Current' is equal to the value 'NULL', which is why I said that when you initialise your linked list to
set the values for Next to NULL if they didn't point to anything. The line 'Current = Current->Next' does just what it looks like. It sets what it is currently pointing to, to the next item in the
list. The last thing it will do, is set itself to NULL before exiting. This will print out all of the values in the list (which is just the contents of Address1 and Address2).

So now you are wondering 'what is the point' because you have to initialise all of the variables and put them in a list yourself. There is something else that we are missing, and it is the key

Cheers, Chris Bennett (aka. Dwarfsoft)

Author: Chris Bennett aka Dwarfsoft
Contact: dwarfsoft@hotmail.com
November 26, 2000