Thread: Linked Lists -- Again!!! Help!!

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    Linked Lists -- Again!!! Help!!

    Ok, I somewhat understand linked lists now. There is a pointer that keeps on pointing to the next object in the struct so you can change each one. Is that right? But I am still very confused with them. Don't tell me to look at the tutorial because I already did and it helped me only a little bit. Could someone give the whole schpiel on linked lists like what a node is and what linked lists are used for and how to set one up, like with a code example. And please comment extensively. Thank you very much.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    I am too busy to write code currently but i'll do what i can.


    A linked list is a list of items(known as nodes). However, unlike arrays, a linked list has no definate length. This is one of the advantages of it. By the way, what you understand so far is correct.

    Steps to create a linked list
    1. create a class or structure that contains data of some and a pointer to the next class

    2. create a head node and set the head node's pointer to a node

    3. continue this process

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    ok

    ok never mind. I understand what they are used for, but still do not fully understand how to use them. I just read though that classes can do all that stuff and I will be using classes instead of the out-of-date structs.

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    classes are fine.... just make sure to differentiate between PRIVATE and PUBLIC data members....

    simply said....: linked lists are also like "phone lines" you can call anyone by pressing numbers on your phone (using the pointer) and that person (object) doesn't have to be next door...

    array has a set size, linked list doesn't b/c all it is just bunch of objects (that have pointers) spread around anywhere, they are not associated prior to you setting where the pointer will point...

    you can arrange them in any way you want, all you change is the "direction" in which the pointer points to.... if you need another member of a linked list, all you do is declare another object and have i.e. the last object's pointer point to that new object (node)....

    there's a lot , a lot, more to LINKED LIST... if you are looking for a good book with a great full explanation and ways of utilizing linked lists, i recommend "Data Abstraction and Problem Solving with C++ (Walls and Mirrors)" written by Carrano-Helman-Verhoff.......
    there's a whole chapter with a ton of exercises on this topic...

    hope this helps...

    Regards,
    matheo917

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    You keep a pointer to the first node. Then, you go from node to node until you reach the end node, whose "next" pointer is NULL. That's all there is to it.

    Here's some sample C++ source that may help.
    PHP Code:

    //A node is an individual "link" in a linked list
    struct node
    {
        
    int x;
        
    node next;
    };

    node beginning = new node;
    node current beginning;
    int n;        //Will temporarily store numbers that we get with cin

    cout << "Enter numbers.  Enter a non-numeric character to finish.\n";

    while(
    cin >> n
    {
             
    current->n;            //Put data in node
             
    current->next = new node;    //Create new node
             
    current current->next;    //Move to next (new) node
             
    current->next NULL;        //Make new node last node
    }

    cout << "We will now display the numbers you input\n";

    current beginning;                //Start at the beginning
    while(current != NULL)                //If we haven't moved past the last node
    {
        
    cout >> current->x;
        
    current current->next;        //move to next node
    }

    cout << "Done.  Now we'll delete.\n";

    node next;                        //Temporarily holds next
    current beginning;                //Start at the beginning
    while(current != NULL)                //If we haven't moved past the last node
    {
        
    next current->next;    //hold next so we have it when we delete current
        
    delete current;            //delete current node
        
    current next;            //move to next node
    }

    cout << "And now we've cleaned up and we're done.\n"
    Last edited by TerranFury; 01-22-2002 at 08:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  3. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 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