Thread: Dynamicaly Linked List project

  1. #1
    Unregistered
    Guest

    Question Dynamicaly Linked List project

    Hi all! I have this project to do - and I am having a couple of problems implementing the spec.

    Ok - the project has two parts - the first being the design of a..
    "suite of functions that will create and maintain any number of doubly linked lists. These functions should be designed in such a way that they will be able to manipulate a list containing data of any type."
    ...
    "The structure of the list should therefore be similar to the following".
    Code:
    typedef struct list DLL;
    typedef struct node NODE;
    
    struct node
    {
      void *data;
      NODE *prior, *next;
    };
    
    struct list
    {
      long int count;
      NODE *head, *tail;
    };
    Ok - that is fine - but part two really gets me. I have to write a "test" program to test the functions to ensure that the functions will work for any data type. Now the test program is not aloud to access any of the previous structures components directly.

    This is the bit that is confusing me (refering to the second part, the test program):
    "The test program that you are to design should thouroughly test all aspects of the doubly linked list routines. As part of the testing, a seperate linked list is to be created and maintained for each of the following data types: long integers, floats, character strings, and complex structures."
    Code:
    typedef struct
    {
      long int id;
      char name[MAX];
      char address[MAX];
      float salary;
    } PERSON;
    
    typedef struct
    {
      PERSON details;
      char department[MAX];
      char position[MAX];  
    } EMPLOYEE;
    Questions:
    1. Making a list of type int, etc is easy - how would I do the same for the complex structure?

    TIA

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Oh - also, how does one allocate the memory to use *data in this structure?

    Cheers.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >Making a list of type int, etc is easy - how would I do the same
    >for the complex structure?

    Code:
    struct S
    {
        .. /* some data */
        struct S *next;
    };
    Or

    Code:
    struct S list [LISTSIZE];
    >how does one allocate the memory to use *data in this
    >structure?

    Casting. For example:

    struct S *s;
    s = (struct S *) malloc (sizeof (struct S));

    And don't forget to free the memory:

    free (s);

  4. #4
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Thanks Shiro.

    I think I forgot to put some things in the original post. The complex structure that I have to use for the test program is:
    Code:
    typedef struct
    {
      long int id;
      char name[MAX];
      char address[MAX];
      float salary;
    } PERSON;
    
    typedef struct
    {
      PERSON details;
      char department[MAX];
      char position[MAX];  
    } EMPLOYEE;
    This structure could be of any type (but they want this particular structure), it will appear only in the test program (not in my library of functions for manipulating lists).

    I need to use the DLL structure to store (or rather point to) the data in any number of these structures. i.e. User inputs data into the test program (stored in a structure of the type above) this is then passed to the code that inserts linked lists - and using the DLL structure I am to keep track of any number of these structures.

    I am assuming that I point the void *data variable to the structure??

    Hope this is clear!

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I hope I understand this correctly, you need a linked-list structure that can handle any kind of data. There's two ways to do this. I assume you need a list that can be created for any structure.
    Code:
    int main (void)
    {
     list * myList = makeList (sizeof(int));
     list * myList2 = makeList (sizeof(PERSON));
     int i;
    
     for (i = 0; i < 5; i++)
     {
      add2List (myList, i);
     }
    
     add2List (myList2, Harry);
     add2List (myList2, Frank);
     
     return 0;
    }
    Passing sizeof is the trick here. How much memory you allocate for each void * will be decided by the size of the variable type. Copying the variable into the node will be done using memcpy and the variable size.
    Callou collei we'll code the way
    Of prime numbers and pings!

  6. #6
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    I hope I understand this correctly, you need a linked-list structure that can handle any kind of data. There's two ways to do this. I assume you need a list that can be created for any structure.
    That is correct, (Sorry about the description!!).

    I am suppose to write a header file and .c source that contains a couple of structures and functions that can maintain any number of linked lists and any kind of data. It is to act as a black box essentially.

    The functions that they suggest are:

    init_list();
    insert();
    insert_sorted();
    del_node();
    print_list();
    print_list_reverse();
    search();
    count();

    All these functions are pretty much expected to take a pointer to a list, a pointer to a function to handle the type of data (kinda like the way qsort works).

    I was going to use something like DLL *mylist; to maintain the root node.

    NODE *foo; to maintain the the list internally and then do something like...

    foo->data = ptr_to_users_data;

    I think this is where I am coming unstuck mentally. Picturing what I am doing with the foo->data pointer.

    I working under the assumption that I will be allocating memory for a NODE *foo of size NODE - Inserting this into a list of type DLL and then pointing foo->data at the data that the user enters??

    Suggestions, friendly slaps on the wrist?

    Thanks too for the input QuestionC - you actually have clarified a few things for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM