简体   繁体   English

如何在 C++ 中创建列表?

[英]How could I create a list in c++?

How can I create a list in C++?如何在 C++ 中创建列表? I need it to create a linked list.我需要它来创建一个链表。 How would I go about doing that?我该怎么做? Are there good tutorials or examples I could follow?是否有我可以遵循的好的教程或示例?

I take it that you know that C++ already has a linked list class, and you want to implement your own because you want to learn how to do it.我认为您知道 C++ 已经有一个链表类,并且您想实现自己的,因为您想学习如何去做。

First, read Why do we use arrays instead of other data structures?一、阅读为什么我们使用数组而不是其他数据结构? , which contains a good answer of basic data-structures. ,其中包含基本数据结构的一个很好的答案。 Then think about how to model them in C++:然后考虑如何在 C++ 中对它们进行建模:

struct Node {
    int data;
    Node * next;
};

Basically that's all you need to implement a list!基本上这就是实现列表所需的全部内容! (a very simple one). (一个非常简单的)。 Yet it has no abstractions, you have to link the items per hand:然而它没有抽象,你必须每手链接项目:

Node a={1}, b={20, &a}, c={35, &b} d={42, &c};

Now, you have have a linked list of nodes, all allocated on the stack:现在,您有一个节点的链表,所有节点都分配在堆栈上:

d -> c -> b -> a
42   35   20   1

Next step is to write a wrapper class List that points to the start node, and allows to add nodes as needed, keeping track of the head of the list (the following is very simplified):下一步是编写一个指向起始节点的包装类List ,并允许根据需要添加节点,跟踪列表的头部(以下非常简化):

class List {
    struct Node {
        int data;
        Node * next;
    };

    Node * head;

public:
    List() {
        head = NULL;
    }

    ~List() {
        while(head != NULL) {
            Node * n = head->next;
            delete head;
            head = n;
        }
    }

    void add(int value) {
        Node * n = new Node;
        n->data = value;
        n->next = head;
        head = n;
    }

    // ...
};

Next step is to make the List a template, so that you can stuff other values (not only integers).下一步是使 List 成为模板,以便您可以填充其他值(不仅仅是整数)。

If you are familiar with smart pointers, you can then replace the raw pointers used with smart pointers.如果您熟悉智能指针,则可以将使用的原始指针替换为智能指针。 Often i find people recommend smart pointers to starters.我经常发现人们向初学者推荐智能指针。 But in my opinion you should first understand why you need smart pointers, and then use them.但在我看来,您应该首先了解为什么需要智能指针,然后再使用它们。 But that requires that you need first understand raw pointers.但这要求您首先需要了解原始指针。 Otherwise, you use some magic tool, without knowing why you need it.否则,您会使用一些神奇的工具,却不知道为什么需要它。

You should really use the standard List class .你真的应该使用标准的 List 类 Unless, of course, this is a homework question, or you want to know how lists are implemented by STL.当然,除非这是一个作业问题,或者您想知道 STL 是如何实现列表的。

You'll find plenty of simple tutorials via google, like this one .你会通过谷歌找到很多简单的教程,就像这个 If you want to know how linked lists work "under the hood", try searching for C list examples/tutorials rather than C++.如果您想知道链表是​​如何在“幕后”工作的,请尝试搜索 C 列表示例/教程而不是 C++。

If you are going to use std::list , you need to pass a type parameter:如果要使用std::list ,则需要传递类型参数:

list<int> intList;  
list<int>* intListPtr = new list<int>;

If you want to know how lists work, I recommending googling for some C/C++ tutorials to gain an understanding of that subject.如果您想了解列表的工作原理,我建议您使用谷歌搜索一些 C/C++ 教程以了解该主题。 Next step would then be learning enough C++ to create a list class, and finally a list template class.下一步是学习足够的 C++ 来创建一个列表类,最后是一个列表模板类。

If you have more questions, ask back here.如果您有更多问题,请在此处提问。

Why reinvent the wheel.为什么要重新发明轮子。 Just use the STL list container.只需使用 STL 列表容器。

#include <list>

// in some function, you now do...
std::list<int> mylist; // integer list

More information...更多信息...

I'm guessing this is a homework question, so you probably want to go here .我猜这是一个家庭作业问题,所以你可能想去 这里 It has a tutorial explaining linked lists, gives good pseudocode and also has a C++ implementation you can download.它有一个解释链表的教程,给出了很好的伪代码,还有一个 C++ 实现,你可以下载。

I'd recommend reading through the explanation and understanding the pseudocode before blindly using the implementation.我建议在盲目使用实现之前通读解释并理解伪代码。 This is a topic that you really should understand in depth if you want to continue on in CS.如果你想继续学习 CS,这是一个你真的应该深入了解的主题。

Create list using C++ templates使用C++ 模板创建列表

ie

template <class T> struct Node 
{
    T data;
    Node * next;
};
    
template <class T> class List 
{
    Node<T> *head,*tail;
        
    public: 
        void push(T const&);  // push element 
        void pop();           // pop element 
        bool empty()          // return true if empty. 
}; 

Then you can write the code like:然后你可以编写如下代码:

List<MyClass>;

The type T is not dynamic in run time.It is only for the compile time.类型T在运行时不是动态的。它仅用于编译时。

For complete example click here .有关完整示例, 请单击此处

For C++ templates tutorial click here .对于 C++ 模板教程,请单击此处

We are already in 21st century!!我们已经在21世纪了!! Don't try to implement the already existing data structures.不要试图实现已经存在的数据结构。 Try to use the existing data structures.尝试使用现有的数据结构。

Use STL or Boost library使用 STL 或 Boost 库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM