Thread: what does a linked list structure look like?

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

    what does a linked list structure look like?

    I have read all sample codes of how to access the linked-list, how to travel trough the linked-list, how to add new data to the linked-list. But all the samples asume that i allready have a linked list to process. Can someone give me a small sample how does the data look like?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    You have a linked list structure already defined and a routine to add data so you already know what kind of data you can process, no?

    A linked list is a general container that can store anything, but depending on how it is defined it could only store integers for instance.

    Try to post some code.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    i have struct:
    Code:
    struct Raamat
    {
       char *pISBN;
       char *pAuthor;             
       char *pPealkiri;         
       int Ilmus;              
    };
    who do i link this list below:

    Code:
    struct Raamat Kogu[]= 
          { {"1-800-423-0563", "Adam Drozdek", "Data structures and algoritms in C++",2001},
            {"1-56592-453-3", "Kyle Loudon", "Mastering algoritms with C",1991},
            {"9985-850-32-7","Viktor Leppikson","Programmeerimine C-keeles",1997},
            {"9985-59-255-7","Villem Bender","Fyysika ylesanded",1996},
            {"9985-2-0452-2","Ylo Ugaste","Fyysika gymnaasiumile",2001},
            {"5-440-00060-7","Rein Jyrgenson","Programmeerimine",1989},
            {"978-9949-11-527-3","Virge Soomer","Matemaatilise analyysi algkursus",2007},
            {"5-440-00327-4","Vahur Mägi","Elektrotehnika",1989},
            {"9985-59-413-4","Harri Lensen","Diskreetne Matemaateka",2002},
            {"9949-10-081-X","Jaak L6hmus","Universiumi mikromaailm",2003},
            {"9985-9417-7-2","Ylo Kaasik","Matemaatikaleksikon",2003},
            {"9985-9327-7-3","Guido Knopp","Hitleri kasilased",1996} 
          };

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    That's an array, not a linked list. What are you trying to do?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    I think you want to use entries of the array to fill a linked list, right?

    But the structure you define doesn't allow to build a linked list, you miss at least one field to make the things 'linked'.
    You said you had the routine to add data to the list, could you post that?

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    ok so how about i have structure:
    Code:
    struct Raamat
    {
       char *pISBN;
       char *pAuthor;             
       char *pPealkiri;         
       int Ilmus;
       struct Raamat *Next;
    };
    so now how i make a linked list?

    Kogu->pAutor should be Adam Drozdek
    Kogu->Next->Kogu->pAutor should be Kyle Loudon
    Kogu->Next->Kogu->Next->Kogu->pAutor should be Viktor Leppikson

    and so on...

    How do i do that?

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    You have to allocate each node of the list:

    Code:
    struct Raamat* pNode=malloc(sizeof(struct Raamat));
    then you fill each field of the node:
    Code:
    pNode->pAuthor=...
    pNode->pISBN=...
    ...
    and you set the field 'Next' to link the new node to the list already existing. Obviously for the very first node (or last one depending on how you link the things), you keep this field null as there is no other node yet.
    For instance, assuming you have created two nodes: n1 and n2, and assuming n1 is the first one, you can set n1->Next=n2; and n2->Next=0; This is not the best approach as you have to walk the entire list to add a node.
    You also have to wrap that into a nice function.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    well i suddenly feel very stupid. Im way too beginner to understand this. My assingment is that i have to learn how do create 100*struct Raamat linked list. A list of hundred books and they are linked. I have to know how do add new books to the list, how to delte them, how to search a book and many more. Im really desperate to understand all of this. The sample codes on the net are very complicated to me.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Are you familiar with dynamic memory allocation?
    Do you understand the concept of container?

    Try to proceed by small steps, if you don't understand how to build a list, it probably means there is something more fundamental you missed. Identify you problem, experiment with code if you're not sure (there is no quotas for that, don't hesitate) and then ask precisions/explanations about specific points here.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I think of a linked list, like a chain. Every link of the chain is a node in a list, that are all connected by pointers, to the next link in the chain - the next node in the list.

    The idea of a linked list is very simple, actually. Just like a chain has many links, or a book has many pages, so a linked list can have many nodes. You tell the program what you want those nodes to contain, just like a writer decides what each page of his book, will contain.

    Just as there are two types of books - soft and hard cover, there are two kinds of linked lists - single linked, and double linked. As you might guess the single linked list has just one pointer, and the doubly linked list has two pointers. A doubly linked list is like two - way traffic on a street, the singly linked list is like a one-way traffic road; you can travel down the list, but you can't travel back up it, again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM