Linked List

This is a discussion on Linked List within the C Programming forums, part of the General Programming Boards category; I have the following data structure for a linked list: Code: struct product_data { int product_code; int product_size; PRODUCT_DATA *next; ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    Question Linked List

    I have the following data structure for a linked list:
    Code:
    struct product_data {
     int product_code;
     int product_size;
     PRODUCT_DATA *next;
    };
    
    typedef struct product_data PRODUCT_DATA;
    PRODUCT_DATA *productsEnd = NULL;
    So to create a new link, i would just do this:
    Code:
    PRODUCT_DATA *products;
    and point next to productsEnd? then i can do this:
    Code:
    PRODUCT_DATA *products;
    and will that create a new link again as well?
    Does *products have to be a different name for every different node?
    Last edited by taurus; 10-29-2008 at 02:07 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If your typedef is after the struct definition, it will not compile.

    To create a node from a pointer, you would first have to allocate memory for it. Then you use the next pointer to link the elements together.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    But would you do this for every node you create:
    PRODUCT_DATA *products;?
    and this
    newproduct = malloc(sizeof(PRODUCT_DATA)))

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Anyone can help me understand this?

    thanks

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by taurus View Post
    But would you do this for every node you create:
    PRODUCT_DATA *products;?
    and this
    newproduct = malloc(sizeof(PRODUCT_DATA)))
    Yes. And then you need to manipulate the next pointer of either that object to insert it into the beginning of the list, or manipulate the next pointer of the end of the list to point to this object (or manipulate BOTH the next pointer of this object and some point in the list to insert it somewhere other than the end of the list).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    so even though i do this to create a new node
    PRODUCT_DATA *products;
    newproduct = malloc(sizeof(PRODUCT_DATA)));

    It does not over-right the last one since it has the name??

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by taurus View Post
    so even though i do this to create a new node
    PRODUCT_DATA *products;
    newproduct = malloc(sizeof(PRODUCT_DATA)));

    It does not over-right the last one since it has the name??
    newproduct is a pointer. When you malloc() the first time, it gets a block of memory at (say) address 1000 (so newproducts contains 1000). As long as you do no free the memory, that memory will remain in use by the data you fill into the memory. When you call malloc a second time, you will get a different lump of memory, say at address 1050. So the content of address 1000, which is the previous element, is still there in memory. You now need to link the two together via the next pointer of one pointing to the other. So you will need to have at least one other pointer that retains one end of your linked list (commonly these are called "head" and/or "tail", but there are other possible names for the ends of a list).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    ah i see thanks.
    so i have to have a head and tail, why cant i just create a node then create another one and link that to the first one?
    is this head and tail used to determine the memory size bounds then?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by taurus View Post
    ah i see thanks.
    so i have to have a head and tail, why cant i just create a node then create another one and link that to the first one?
    is this head and tail used to determine the memory size bounds then?
    If you want a LINKED LIST, you need one node-pointer to hold some point in the list that you can link the other elements on to. You do not necessarily need a head AND a tail, but you need at least one of those to attach something to the linked list. [Theoretically, you could do it without the separate variable - but I'm not going there right now, as it's a bit beyond your "first attempt at linked lists"].

    You could make links for a chain individually, but if you want a CHAIN you will need to connect your new link to the existing part of chain before shutting it completely, right? A linked list is like a chain.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Oh cause i did it in java and what i did what set the first next to point to null and go on from there.

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21