Doubly liked lists...HELP PLEASE
Quote:
> Hi, I am trying to finish a C programming assignment due tomorrow and I am
> having real trouble declaring and using a doubly (i.e. next, previous)
> linked list. I was wondering if anyone out there had a simple piece of code
> I could follow, and from it understand this topic, i.e. something as simple
> as a list
> that contains names and phone numbers or alike. I am just having trouble
> with the concept that they are dynamic in the sense that they can grow and
> shrink to save memory, won't you have to declare an array of a fixed length.
> I don't want to have to declare an array of fixed length and I can't see how
> to get around it.
> Mandy Russell.
> --
The practical application of this is accomplished by first creating a structure
to hold your data. By your description lets make it like this:
struct phone_directory
{
char name{50];
char phone_number[15];
struct phone_directory *next;
struct phone_directory *prev;
Quote:
};
Ok, the next and prev members of the structure are pointers to structures
of type phone_directory. You will need two pointer variables of the phone_
directory structure type for maninpulating your list as it's created. You
malloc
off one structure and set one of your points equal to it's address (call it
root).
The root node's prev pointer will point to null (or the last member of the list
if
this is circular). You then malloc off another structure, setting your other
pointer
equal to it's address, set roots next member equal to this second pointer, set
the new nodes prev member equal to root and it's next member equal to null.
You can now reuse the second pointer to malloc off the next node in the list
because the previous nodes address is contained in roots next member.
Using this general scheme you can create a list of any length, using only as
much memory as you need.
Hope this helps,
James Kimble
--