Thread: linked list (good example)

  1. #1

    linked list (good example)

    /* is the following a good example of a linked list and if not why */


    struct linkedlist{
    int a,b,c;
    struct linkedlist *next_linkedlist;

    int main()
    struct linkedlist list,*ptr; /* pointer to structure */
    ptr=&list; /* of type linkedlist */

    ptr->a=10; /* pointer points to first data member/variable a */
    ptr->next_linkedlist->a=37; /* pointer now points to the next */
    /* structure then it's first data member which is a */

    printf("%d",ptr->a); /* output 10 */
    printf("\n%d",ptr->next_linkedlist->a); /* output 37 */

    return 0;
    /* The identifier a has been used twice in the above code */
    /* has it been used correctly, To create more lists would */
    /* I do this in the outer most structure add more statements */
    /* like so struct linklist *next1_linkedlist; */
    /* struct linklist *next2_linkedlist; */
    /* ... */
    /* ... */
    /* ... */
    /* Therefor being able to use a,b,c over and over and... */
    /* But if all these pointers point to abc how come there not */
    /* overwriten */

    thanks for all the help

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    No, because the list as given only has one node, pointed to by "ptr", with its "next_linkedlist" pointer pointing to god knows where. The assignment of 37 to the "a" member variable of the second node might cause the program to crash because it (the 2nd node) does not exist yet. You would have to set the "next_linkedlist" ptr of the first node to the return value of a "malloc" call and then you could set its "a" variable to 37. Also, as a matter of good habit, you would want to immediately initialize any pointers of newly allocated nodes to be NULL.

    Incidentally, before I registered myself on this board, I thought I had answered this question or something VERY similar to it on either this or the C++ board a week or so ago. Hmmm....
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Join Date
    Aug 2001
    Groningen (NL)
    Not really. It doesn't show how the list is build. And I would not call the structure linkedlist, but something like node. Since the list is build up of nodes. In the example it looks like if the pointer in the structure points to a different list, but it's just pointing to the following node in the list.

    struct node
        int data;
        struct node *next_node;
    A good example about linked lists should show how a list is build. So it should make clear how to create a linked list, something like this should be present.

    /* The list */
    struct node list;
    /* A pointer required to traverse the list */
    struct node *list_ptr;
    /* Initialise list */ = JUST_A_VALUE;
    list.next_node = NULL;
    /* Add a node to the list */
    new_node = create_node (sample_data);
    if (new_node != NULL)
        list_ptr->next_node = new_node;
    This should be part of an intro to linked lists.

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. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 09:38 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 03:53 PM
  5. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM
Website Security Test