Thread: Linked list of linked lists???

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    30

    Question Linked list of linked lists???

    Hi, How would I go about making a structure implementation of a linked list of linked lists eg list of shops and each shop has another list of customers, i already have an implementation of the main linked list, i am just a little confused as to how i make another list of based on a common element in the main list, the way I thought of approching it was to put the common element into both lists and search for the element and compile the list based on another list that way, but it seems flawn and i think thats against the point of the exercise.

    Any help is much appreciated, Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Can a customer visit more than one shop?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    If a customer can visit more than one shop. in terms of a database.... it would be better to have customer as the main list and the shops they visit as a list under them..

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    30
    Hi, yes a customer can visit mroe than one shop, but I wanted the list to be shops as the main list and an internal list of customers that visit it, yes it is easier to have customers as the main list but i want to display it as list of shops with customers that visit each shop as a sub list, would this be accomplished by using a nested structure or listing a pointer to the list of customers in the shops list and a common element to both lists in each, if so would that make the lists linked or would I still have to do a seach for then organise by common element. Also wouldnt a search mean I would have an idividual list for each shop, or possibly recycle the initial list per shop, but I want to make the 2 linked without a search or is that not possible.

    Thanks
    Last edited by Qui; 03-27-2004 at 12:45 PM.

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Code:
    struct customer
               {
    
               struct  customer *next;
               customer details........
    
                }
    
    
    struct shop
               {
              struct shop *next;
             struct customer *head_of_customer_list;
     
                shop details....................
    
                }



    well i would use a structure as above to do it... You can have a seperate link list where each customer link list will hold customer details for 1 shop.. and you can attach it to the ship structure and you can have a link list of shops....

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    30
    Hi, oki so I dont need a common element? or do I? and if so is it not still gonna have to do a search to find the customer who is linked by the common element to that specific shop. I have taken your approach and have made 2 structures with a common element, shop name. but is it not cheating if i have to search for the customer whos got that shop name in their structure, as oppose to having the shop structure know which customers are linked to it? how would I go about making the shop know which customer is linked to it without doing a search through the lists. or is that not possible?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >so I dont need a common element? Correct. but there is no law against it, that I know of.

    >how would I go about making the shop know which customer is linked to it without doing a search through the lists. or is that not possible?

    If you want the shops to maintain a customer list, then the form already presented should do the trick. To find out if a given customer is on the list for a given shop you'll need to search the list of shops for the given shop, then search that shop's list of customers for the given customer. As part of the customer details, you could have a list of shops visited if you want, and get the same information, that is, this will also allow you to determine if a given customer visit a given shop. But the searching would be easier if the master list were a list of customers. In other words, you can come at this from either end, depending on what point of view you want to start with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  3. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM