我如何在 C++ 中创建一个列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/397895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How could i create a list in c++?
提问by jessemiel
How can I create a list in 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?
如何在 C++ 中创建列表?我需要它来创建一个链表。我该怎么做?是否有我可以遵循的好的教程或示例?
回答by Johannes Schaub - litb
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 verysimple 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.
如果您熟悉智能指针,则可以将使用的原始指针替换为智能指针。我经常发现人们向初学者推荐智能指针。但在我看来,您应该首先了解为什么需要智能指针,然后再使用它们。但这要求您首先需要了解原始指针。否则,您会使用一些神奇的工具,却不知道为什么需要它。
回答by Roddy
You should really use the standard List class. Unless, of course, this is a homework question, or you want to know how lists are implemented by STL.
你真的应该使用标准的 List 类。当然,除非这是一个作业问题,或者您想知道 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++。
回答by karx11erx
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. Next step would then be learning enough C++ to create a list class, and finally a list template class.
如果您想了解列表的工作原理,我建议您使用谷歌搜索一些 C/C++ 教程以了解该主题。下一步是学习足够的 C++ 来创建一个列表类,最后是一个列表模板类。
If you have more questions, ask back here.
如果您有更多问题,请在此处提问。
回答by mepcotterell
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
回答by brien
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,这是一个你真的应该深入了解的主题。
回答by plan9assembler
回答by Yasitha Bandara
Create list using C++ templates
使用C++ 模板创建列表
i.e
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 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 hear.
有关完整示例,请单击“听”。
For C++ templates tutorial click hear.
对于 C++ 模板教程,请单击听。
回答by Vinay
We are already in 21st century!! Don't try to implement the already existing data structures. Try to use the existing data structures.
我们已经在21世纪了!!不要试图实现已经存在的数据结构。尝试使用现有的数据结构。
Use STL or Boost library
使用 STL 或 Boost 库