Why do linked lists use pointers instead of storing nodes inside of nodes

后端 未结 11 2162
春和景丽
春和景丽 2020-12-07 13:53

I\'ve worked with linked lists before extensively in Java, but I\'m very new to C++. I was using this node class that was given to me in a project just fine

         


        
相关标签:
11条回答
  • 2020-12-07 14:01

    The approach that you describe is compatible not only with C++, but also with its (mostly) subset language C. Learning to develop a C-style linked-list is a good way to introduce yourself to low-level programming techniques (such as manual memory management), but it generally is not a best-practice for modern C++ development.

    Below, I have implemented four variations on how to manage a list of items in C++.

    1. raw_pointer_demo uses the same approach as yours -- manual memory management required with the use of raw pointers. The use of C++ here is only for syntactic-sugar, and the approach used is otherwise compatible with the C language.
    2. In shared_pointer_demo the list management is still done manually, but the memory management is automatic (doesn't use raw pointers). This is very similar to what you have probably experienced with Java.
    3. std_list_demo uses the standard-library list container. This shows how much easier things get if you rely on existing libraries rather than rolling your own.
    4. std_vector_demo uses the standard-library vector container. This manages the list storage in a single contiguous memory allocation. In other words, there aren't pointers to individual elements. For certain rather extreme cases, this may become significantly inefficient. For typical cases, however, this is the recommended best practice for list management in C++.

    Of note: Of all of these, only the raw_pointer_demo actually requires that the list be explicitly destroyed in order to avoid "leaking" memory. The other three methods would automatically destroy the list and its contents when the container goes out of scope (at the conclusion of the function). The point being: C++ is capable of being very "Java-like" in this regard -- but only if you choose to develop your program using the high-level tools at your disposal.


    /*BINFMTCXX: -Wall -Werror -std=c++11
    */
    
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <list>
    #include <vector>
    #include <memory>
    using std::cerr;
    

    /** Brief   Create a list, show it, then destroy it */
    void raw_pointer_demo()
    {
        cerr << "\n" << "raw_pointer_demo()..." << "\n";
    
        struct Node
        {
            Node(int data, Node *next) : data(data), next(next) {}
            int data;
            Node *next;
        };
    
        Node * items = 0;
        items = new Node(1,items);
        items = new Node(7,items);
        items = new Node(3,items);
        items = new Node(9,items);
    
        for (Node *i = items; i != 0; i = i->next)
            cerr << (i==items?"":", ") << i->data;
        cerr << "\n";
    
        // Erase the entire list
        while (items) {
            Node *temp = items;
            items = items->next;
            delete temp;
        }
    }
    

    raw_pointer_demo()...
    9, 3, 7, 1
    

    /** Brief   Create a list, show it, then destroy it */
    void shared_pointer_demo()
    {
        cerr << "\n" << "shared_pointer_demo()..." << "\n";
    
        struct Node; // Forward declaration of 'Node' required for typedef
        typedef std::shared_ptr<Node> Node_reference;
    
        struct Node
        {
            Node(int data, std::shared_ptr<Node> next ) : data(data), next(next) {}
            int data;
            Node_reference next;
        };
    
        Node_reference items = 0;
        items.reset( new Node(1,items) );
        items.reset( new Node(7,items) );
        items.reset( new Node(3,items) );
        items.reset( new Node(9,items) );
    
        for (Node_reference i = items; i != 0; i = i->next)
            cerr << (i==items?"":", ") << i->data;
        cerr<<"\n";
    
        // Erase the entire list
        while (items)
            items = items->next;
    }
    

    shared_pointer_demo()...
    9, 3, 7, 1
    

    /** Brief   Show the contents of a standard container */
    template< typename C >
    void show(std::string const & msg, C const & container)
    {
        cerr << msg;
        bool first = true;
        for ( int i : container )
            cerr << (first?" ":", ") << i, first = false;
        cerr<<"\n";
    }
    

    /** Brief  Create a list, manipulate it, then destroy it */
    void std_list_demo()
    {
        cerr << "\n" << "std_list_demo()..." << "\n";
    
        // Initial list of integers
        std::list<int> items = { 9, 3, 7, 1 };
        show( "A: ", items );
    
        // Insert '8' before '3'
        items.insert(std::find( items.begin(), items.end(), 3), 8);
        show("B: ", items);
    
        // Sort the list
        items.sort();
        show( "C: ", items);
    
        // Erase '7'
        items.erase(std::find(items.begin(), items.end(), 7));
        show("D: ", items);
    
        // Erase the entire list
        items.clear();
        show("E: ", items);
    }
    

    std_list_demo()...
    A:  9, 3, 7, 1
    B:  9, 8, 3, 7, 1
    C:  1, 3, 7, 8, 9
    D:  1, 3, 8, 9
    E:
    

    /** brief  Create a list, manipulate it, then destroy it */
    void std_vector_demo()
    {
        cerr << "\n" << "std_vector_demo()..." << "\n";
    
        // Initial list of integers
        std::vector<int> items = { 9, 3, 7, 1 };
        show( "A: ", items );
    
        // Insert '8' before '3'
        items.insert(std::find(items.begin(), items.end(), 3), 8);
        show( "B: ", items );
    
        // Sort the list
        sort(items.begin(), items.end());
        show("C: ", items);
    
        // Erase '7'
        items.erase( std::find( items.begin(), items.end(), 7 ) );
        show("D: ", items);
    
        // Erase the entire list
        items.clear();
        show("E: ", items);
    }
    

    std_vector_demo()...
    A:  9, 3, 7, 1
    B:  9, 8, 3, 7, 1
    C:  1, 3, 7, 8, 9
    D:  1, 3, 8, 9
    E:
    

    int main()
    {
        raw_pointer_demo();
        shared_pointer_demo();
        std_list_demo();
        std_vector_demo();
    }
    
    0 讨论(0)
  • 2020-12-07 14:02

    It's not just better, it's the only possible way.

    If you stored a Node object inside itself, what would sizeof(Node) be? It would be sizeof(int) + sizeof(Node), which would be equal to sizeof(int) + (sizeof(int) + sizeof(Node)), which would be equal to sizeof(int) + (sizeof(int) + (sizeof(int) + sizeof(Node))), etc. to infinity.

    An object like that can't exist. It's impossible.

    0 讨论(0)
  • 2020-12-07 14:03

    On a side note, if the very first member of a class or struct is the next pointer (so no virtual functions or any other feature of a class that would mean next isn't the first member of a class or struct), then you can use a "base" class or structure with just a next pointer, and use common code for basic linked list operations like append, insert before, retrieve from front, ... . This is because C / C++ guarantees that the address of the first member of a class or structure is the same as the address of the class or structure. The base node class or struct would only have a next pointer to be used by the basic linked list functions, then typecasting would be used as needed to convert between the base node type and the "derived" node types. Side note - in C++, if the base node class only has a next pointer, then I assume that derived classes can't have virtual functions.

    0 讨论(0)
  • 2020-12-07 14:04

    C++ is not Java. When you write

    Node m_next;
    

    in Java, that is the same as writing

    Node* m_next;
    

    in C++. In Java, the pointer is implicit, in C++ it is explicit. If you write

    Node m_next;
    

    in C++, you put an instance of Node right there inside the object that you are defining. It is always there and cannot be omitted, it cannot be allocated with new and it cannot be removed. This effect is impossible to achieve in Java, and it is totally different from what Java does with the same syntax.

    0 讨论(0)
  • 2020-12-07 14:07

    You use a pointer, otherwise your code:

    class Node
    {
       //etc
       Node m_next; //non-pointer
    };
    

    …would not compile, as the compiler cannot compute the size of Node. This is because it depends on itself — which means the compiler cannot decide how much memory it would consume.

    0 讨论(0)
  • 2020-12-07 14:12

    In Java

    Node m_node
    

    stores a pointer to another node. You don't have a choice about it. In C++

    Node *m_node
    

    means the same thing. The difference is that in C++ you can actually store the object as opposed to a pointer to it. That's why you have to say you want a pointer. In C++:

    Node m_node
    

    means store the node right here (and that clearly can't work for a list - you end up with a recursively defined structure).

    0 讨论(0)
提交回复
热议问题