Like Tree1Likes
  • 1 Post By AndrewHunter

Linked Lists (I don't understand...at all?)

This is a discussion on Linked Lists (I don't understand...at all?) within the C++ Programming forums, part of the General Programming Boards category; I finished reading the article on Linked Lists in C++ , and I don't get...well, I don't get pretty much ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    8

    Linked Lists (I don't understand...at all?)

    I finished reading the article on Linked Lists in C++, and I don't get...well, I don't get pretty much anything.

    The way that the tutorial described linked lists seemed somewhat vague unspecific, at least to me, and the example code didn't help much, like it usually does. In fact, when I thought I knew what the explanation was trying to say, I would look at the code and it would make me doubt my understanding. The same would happen the other way around.

    Can someone, briefly, explain how to use linked lists, how/why they work, and how/why they are useful? Or at least point me somewhere that explains it in a different way?

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    A good tutorial can be found: Prelude's Linked List Tutorial. Give that a read and it should answer most of your questions. Mind you, beyond academia, the implementation of linked lists in C++ should be delegated to the STL's std::List.
    rags_to_riches likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    What do you not get ? Linked lists are lists made up of links.
    How to use linked lists:
    Code:
    void use_linked_list(node* start) {
        node* mover = start;
        while(mover != 0) {
            // do something with mover->data
            mover = mover->next;
        }
    }
    How/why they work:
    As stated in the tutorial each node has a data component and a link to the next node which in turn contains data and another link and so on. In order to be able to remember where the list start you create a root node which should not be modified and the end is symbolised by a null pointer. If you want to create a new node, you can just allocate memory to the "next" pointer. This works because you always have a pointer to the next element thus even though the elements aren't necessarily grouped like arrays, you still can access all the elements easily.

    Why are they useful ?
    In C++ I would much rather use std::vector or std::list. In C (or C style C++) it makes working with arrays which need to grow and reduce in size often easier (imo)

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    a linked list is a device to process a collection of items where the values of the objects are not necessarily stored in a contiguous space (i.e. an array)

    each unit, or node, in a linked list must contain a means to access some data at the current node, and a means to access the next node/link in the chain.

    i.e. it tells you what is at the current location, and it tells you how to get to the next one (if it exists).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  2. Replies: 21
    Last Post: 10-14-2006, 04:38 AM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 01:49 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  5. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21