Doubly liked lists...HELP PLEASE 
Author Message
 Doubly liked lists...HELP PLEASE

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.

--



Sun, 17 Nov 2002 03:00:00 GMT  
 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.

You must be ditching class too much. If the concept of dynamic
allocation is foreign to you,
I urge you to go talk to your instructor real soon now. You need
malloc() or some variant of it.

I get the feeling that you think the double link means two items of data
per node (i.e. name and phone number). This is not necessarily true. You
can make your node struct have as many and whatever kinds of data items
you like, but many people prefer one pointer to a struct rather than
putting everything in the node. That way your data item can be a void *
and the code is very reusable:

struct node {
        void *data;
        struct node *prev;
        struct node *next;

Quote:
};

Josh
--



Mon, 18 Nov 2002 03:00:00 GMT  
 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
--



Mon, 18 Nov 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Doubly liked lists...HELP PLEASE

2. Please I need some help -- traversing trough a doubly linked list--

3. Doubly Linked Lists...HELP PLEASE (sorry if this a repost)

4. HELP PLEASE: singly/doubly linked lists

5. Need help with doubly linked lists

6. Help on doubly-link lists on an ADT.....

7. help! doubly linked lists

8. HELP: problem with doubly linked list

9. Help wanted for doubly linked lists

10. help with doubly linked list

11. liked lists in c

12. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

 

 
Powered by phpBB® Forum Software