Thread: struggling with linked lists

  1. #1
    Unregistered
    Guest

    struggling with linked lists

    /* Trying to get to grips with linked lists, I know each node in a linked list */
    /* contains data members and a pointer to the next node */
    /* 1) In the following code I have two objects of type node my_node and */
    /* my_next_node first I fill the array in the first node with some input */
    /* then the pointer in this first node points to my second object of type node */
    /* I don't really see any benefit here I could access both arrays through the */
    /* objects of type node directly anyway my_node.word and my_next_node.word */

    /* 1) I know i'm missing something here but what (maybe a brain) */
    /* 2) how would I greate additional nodes and then delete them */


    #include<stdio.h>

    struct node{
    char word[20];
    node *ptr_to_next_node;
    };

    int main()
    {
    node my_node,my_next_node; /* Two objects of type node */

    cin>>my_node.word; /* get input for first node */

    ptr_to_next_node->my_next_node; /* first node pointer points to next node */


    } /* note to myself INCOMPLETE CODE */

    /* Thanks all for the help */

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    27

    Memory allocation and lists

    Hi,
    Link lists are used when you don't know how many nodes you are going to have.
    Each time you need a new node you allocate memory ( malloc() etc.) for it. The pointer returned from the allocation is used as the pointer of the last node and thus the last node points to the new node. Usually the last node in the list points to NULL until a new node is allocated.

    Hope this helps you understand.
    Pappy
    You learn something new everyday.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM