Thread: linked list I think

  1. #1
    Unregistered
    Guest

    linked list I think

    Is this the idea of the concept linked list if not why and if possible
    more guidance on linked lists would be appreciated and any sites
    with link list info.


    #include<stdio.h>
    #include<conio.h>

    int main()
    {
    struct linkedlist{
    int a,b,c;
    struct linkedlist *next_linkedlist;
    };

    clrscr();

    struct linkedlist *ptr; /* pointer to structure */
    /* 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 */
    getch();

    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.

  2. #2
    Unregistered
    Guest
    As a pointer, your "ptr" variable is simply a memory address that is pointing to god only know where at the start of your program. By attempting to set the "a" member variable of the struct, you may end up accessing areas of memory that are forbidden and could cause a program to crash. You need to actually allocate some space dynamically using some flavor of the "malloc" function or "new" operator and assign the address of the allocated space to your "ptr" variable before you start to use it.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <malloc.h>
    #include <stdlib.h>
    
    struct linkedlist
    {
        int a, b, c;
        struct linkedlist *next_linkedlist;
    }
    
    int main()
    {
        // Create a variable and initialize it to point to an area in memory
        // of the appropriate size.
    
        struct linkedlist *ptr = malloc( sizeof(struct linkedlist) );
    
        // Now you can assign values to the struct members.
    
        ptr->a = 10;
    
        // It is an EXTREMELY good idea to always initialize any pointers
        // in a newly allocated link node to be NULL.  Try to do this as
        // soon as possible after creating the nodes.
    
        ptr->next_linkedlist = NULL;
    
        // Since the ptr->next_linkedlist pointer doesn't point anywhere,
        // we must now create another node again using malloc before
        // we can do any assignments to its members.
    
        ptr->next_linkedlist = malloc( sizeof(struct linkedlist) );
    
        // Now, like before we can assign values to the second node
        // in the list.
    
        ptr->next_linkedlist->next_linkedlist = NULL;
        ptr->next_linkedlist->a = 37;
    
        // Do you printf statements.
    
        printf("%d\n",ptr->a);
        printf("%d",ptr->next_linkedlist->a);
        getch();
    
        // Now we should return our dynamically allocated memory back
        // to the system before we exit.  Do this in reverse order from
        // that used to allocate the nodes.
    
        free(ptr->next_linkedlist); // Free up last node first
        free(ptr);  // Free up first node last
    
        // Exit program.
    
        return 0;
    
    }
    Most of the time you will want to write functions to add new nodes to the list and delete the list when finished instead of writing everything down manually in the "main" function, for your functions to be able to deal with your struct, the definition for the struct should be global which is why I moved it out of the "main" function. Doing things the way you were doing gets very tedious if you are adding lots of nodes to your list. If you need some guidance on how to write these functions I may be able to help.

    As to your question about why the variables a, b, and c are not overwritten, they are in different areas of memory. The first node we create using "malloc" is at one memory address and the variables a, b, c, and next_linkedlist are stored at offsets to that memory location. When we create the second node again using a call to "malloc", this new node gets created at a different memory location from the first and all the offsets to this second node's member variables point to different memory areas than the first nodes' variables. So, nothing gets overwritten if you do things properly.

    Better yet, learn some C++ since it seems to have built in "list" and "vector" templates that handle this sort of thing for you.

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, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM