Thread: double linked-structure nightmare help

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    18

    double linked-structure nightmare help

    My teatcher wants his students to go crazy using pointers and linked-structures. I dont mind studying it but im really stuck with this one and exam aproaches. I do understand the linked-structure graphycaly, but its a little hard to understand how to programm it.

    I have a structure:

    Code:
    struct Book
    
    {
    
    char *pISBN;
    
    char *pAutor; 
                          
    char *pName;
    
    int   date;    
    
    };
    Now, i have to make a data-structure that looks like the picture i uploaded. The RINGS mark as the books and the SQUARES mark as a new structure:

    Code:
    struct Header
    
    {
    
          struct Book *pBook;
    
          struct Header *pNext;
    
    };
    In the first chain, there must be books whos author begins with a letter A, second chain in B and so on. If there is no author to some letter then ZERO must be added to the pointers vector. (like in the picture, there are no A-letter authors, no C-letter authors and more).

    The function that creates this date-structure must be in prototype:

    Code:
    struct Header **CreateStructure(struct Book *pCollection ,int nNumber_of_books);
    where nNumber_of_books is the number of books in vector "Collection" and pCollection is a pointer to the first book. Memory must be asked. i already have Collection[44] vector (44 books in vector).


    Please someone help me to create this function and explaining the code as detailed as possible...

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Why not start off by typedeffing a struct name so you don't have to keep typing struct <name> all the time

    Also I think you mean you want to point the pNext to NULL right?

    pCollection I'm guessing is the head and where is "vector Collection" ?
    =========================================
    Everytime you segfault, you murder some part of the world

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    You say you are stuck with this problem ? If you did the graph, i'm not exactly sure what stopping you from trying to resolveit.

    So, we know we can't use the "book" struct for linking, so we have the "header" struct who encapsulate one book object and can link to another "header". Alright. So... you also said "In the first chain, there must be books whos author begins with a letter A, second chain in B and so on". Ok, so that looks like we'll need an array (or a vector, as you call them). Since we want each elements of our array to be able to contain a linked list of "books", we don't have much choice, we need an array of "header *". That's it. We have pretty much what we need. The array will need to be allocated dynamically since we want to return it (it can't be a local array) and using the static keyword doesn't make sense in this case.

    Here's an insight of what the function will looks like
    Code:
    struct Header **CreateStructure(struct Book *pCollection ,int nNumber_of_books)
    {
       struct Header** linkedListArray;
       // Allocate memory for you array of Header*
       if (linkedListArray != NULL)
       {
          // Initialize each elements of your array to NULL
          // For each "struct Book" of pCollection
             // Check if it's name begin by an alphabetic letter
             // Add this element to the linked list of author which names begin by this letter
       }
    
       return linkedListArray;
    }
    I hate real numbers.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    18

    Red face

    yes i know typedefing is more easyer but my teatcher wants to leave it. I have heard of this head and tail stuff in my class but i dont understand it, thats why i dont know the answer to the second questions. pNext to NULL if there are no authors beginning with letter C for example, yes

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    foxman, i understand the picture now but i still dont know how to programm it, im very beginner in all this sry

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Example of typedef:
    typedef expression name;

    For example,
    typedef struct mystruct mystruct;

    Now "mystruct" is the same as typing "struct mystruct".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    yes i understand the typedef makes things easyer, ok i use that. but someone help me to programm the function

  8. #8
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Here's a refined version of the pseudo-code i gave you
    Code:
    struct Header **CreateStructure(struct Book *pCollection ,int nNumber_of_books)
    {
       struct Header** linkedListArray;
       // Allocate memory for an array of NB_OF_LETTERS_IN_ALPHABET "struct Header*" elements
       if (linkedListArray != NULL)
       {
          // Initialize each elements of your array to NULL
          // For each "struct Book" of pCollection
             // If the first letter of the author's name is alphabetic
                // Allocate memory for a "struct Header"
                // Allocate memory for a "struct Book" who will contain a copy of the book information
                // Encapsulate your newly allocated "struct Book" object in the "struct Header" you just allocated
                // Update the linked list at the correct index
       }
    
       return linkedListArray;
    }
    I hate real numbers.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    i still need the exact code, i cant programm it by own. Im way too beginner with this....

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's not possible. If you can't do it, then you must step back to what you can do.
    If it means you fail your course, then unfortunately it is so. Giving you code will solve nothing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    thats why i want code+detailed expanation if its not too mutch trouble. If i see a code, i will understand it sooner or later, i have done that before, studying the code. Please help me out here.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Location
    Rochester, NY
    Posts
    27
    I agree with everyone else. Giving you code specifically to resolve your issue will not help. Although I understand seeing code helps to understand the task you are attempting to accomplish.

    Something like the Linked List tutorial on cprogramming.com may help you out: http://www.cprogramming.com/tutorial/lesson15.html

    This may be useful as well: http://www.stsci.edu/~bsimon/linked_list.html

    I have code from a graphics project which utilizes linked lists in a similar way to what you are looking to do. I'm at work currently but when I return home I can post up the code if you haven't resolved your problem by that time.
    Last edited by CornyKorn21; 05-14-2008 at 11:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure of structures + linked list
    By saeculum in forum C Programming
    Replies: 3
    Last Post: 03-06-2009, 08:02 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM