Thread: problem with linked list

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    4

    problem with linked list

    hi
    i have 2 structure and i don't understund the linked lists very well yet so please can anyone tell me the difference between this 2 structure:
    Code:
    struct node{
      int a;
      struct node *next;
                     };
    struct list {
      struct node *head;
                   };
    thankyou

  2. #2
    Sasquatch mog's Avatar
    Join Date
    Dec 2006
    Location
    Caves of Narshe
    Posts
    16
    The first one is a structure containing one int and a pointer to another structure of the same kind.
    The second one is quite useless because you only have a pointer to one (probably the first node of a linked list or the head of a BST) node inside it.

    You can use the following code instead, because it makes more sense:

    Code:
    struct node {
        int a;
        struct node *next;
    };
    
    struct node *head;
    Last edited by mog; 06-22-2007 at 08:51 AM.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mog View Post
    The second one is quite useless because you only have a pointer to one (probably the first node of a linked list or the head of a BST) node inside it.
    It wouldn't be useless if he put, say, a "tail" pointer in there as well. Don't knock something just because it looks redundant -- it could be that way because of an intention to expand it later, or it might have contained other stuff in the past which has since been removed, etc...

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Here's a better idea: You tell us what you think the difference is, and we'll tell you if you are right.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Sasquatch mog's Avatar
    Join Date
    Dec 2006
    Location
    Caves of Narshe
    Posts
    16
    Quote Originally Posted by brewbuck View Post
    It wouldn't be useless if he put, say, a "tail" pointer in there as well. Don't knock something just because it looks redundant -- it could be that way because of an intention to expand it later, or it might have contained other stuff in the past which has since been removed, etc...
    Im not knocking anything away.

    Even the most useless thing would be useful if you put some useful stuff in it..
    I donīt really see the point of replying to me with that.

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 circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  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