问题描述
我将要创建一个可以插入并显示直到现在的链接:
I am about to create a linked that can insert and display until now:
struct Node {
int x;
Node *next;
};
这是我的初始化函数,只会为第一个Node
调用:
This is my initialisation function which only will be called for the first Node
:
void initNode(struct Node *head, int n){
head->x = n;
head->next = NULL;
}
要添加Node
,我认为链接列表无法正常工作的原因在于此功能:
To add the Node
, and I think the reason why my linked list isn't working correct is in this function:
void addNode(struct Node *head, int n){
struct Node *NewNode = new Node;
NewNode-> x = n;
NewNode -> next = head;
head = NewNode;
}
我的main
函数:
int _tmain(int argc, _TCHAR* argv[])
{
struct Node *head = new Node;
initNode(head, 5);
addNode(head, 10);
addNode(head, 20);
return 0;
}
让我按我认为可以运行的程序运行.首先,将头Node
初始化为Node
,如下所示:
Let me run the program as I think it works. First I initialise the head Node
as a Node
like this:
head = [ 5 | NULL ]
然后我添加一个n = 10的新节点,并将head作为参数传递.
Then I add a new node with n = 10 and pass head as my argument.
NewNode = [x |下一个]指向下一个.然后我更改头部指向NewNode的位置,因为NewNode现在是LinkedList中的第一个Node.
NewNode = [ x | next ] where next points at head. And then I change the place where head is pointing to NewNode, since NewNode is the first Node in LinkedList now.
为什么这不起作用?我将不胜感激任何可能使我朝正确方向前进的提示.我认为LinkedList有点难以理解.
Why isn't this working? I would appreciate any hints that could make me move in the right direction. I think LinkedList is a bit hard to understand.
当我打印此内容时,它仅返回5:
When I'm printing this, it only returns 5:
推荐答案
在这种情况下,这是我能想到的最简单的示例,未经测试.请注意,这使用了一些不良做法,并且不像通常使用C ++时那样(初始化列表,声明和定义的分离,等等).但这是我在这里无法涵盖的主题.
This is the most simple example I can think of in this case and is not tested. Please consider that this uses some bad practices and does not go the way you normally would go with C++ (initialize lists, separation of declaration and definition, and so on). But that are topics I can't cover here.
#include <iostream>
using namespace std;
class LinkedList{
// Struct inside the class LinkedList
// This is one node which is not needed by the caller. It is just
// for internal work.
struct Node {
int x;
Node *next;
};
// public member
public:
// constructor
LinkedList(){
head = NULL; // set head to NULL
}
// destructor
~LinkedList(){
Node *next = head;
while(next) { // iterate over all elements
Node *deleteMe = next;
next = next->next; // save pointer to the next element
delete deleteMe; // delete the current entry
}
}
// This prepends a new value at the beginning of the list
void addValue(int val){
Node *n = new Node(); // create new Node
n->x = val; // set value
n->next = head; // make the node point to the next node.
// If the list is empty, this is NULL, so the end of the list --> OK
head = n; // last but not least, make the head point at the new node.
}
// returns the first element in the list and deletes the Node.
// caution, no error-checking here!
int popValue(){
Node *n = head;
int ret = n->x;
head = head->next;
delete n;
return ret;
}
// private member
private:
Node *head; // this is the private member variable. It is just a pointer to the first Node
};
int main() {
LinkedList list;
list.addValue(5);
list.addValue(10);
list.addValue(20);
cout << list.popValue() << endl;
cout << list.popValue() << endl;
cout << list.popValue() << endl;
// because there is no error checking in popValue(), the following
// is undefined behavior. Probably the program will crash, because
// there are no more values in the list.
// cout << list.popValue() << endl;
return 0;
}
我强烈建议您阅读有关C ++和面向对象编程的知识.一个很好的起点可能是: http://www.galileocomputing.de/1278?GPP=opoo
I would strongly suggest you to read a little bit about C++ and Object oriented programming. A good starting point could be this: http://www.galileocomputing.de/1278?GPP=opoo
添加了一个弹出功能和一些输出.如您所见,程序将3个值压入5、10、20,然后将其弹出.此后顺序相反,因为此列表在堆栈模式下工作(LIFO,后进先出)
added a pop function and some output. As you can see the program pushes 3 values 5, 10, 20 and afterwards pops them. The order is reversed afterwards because this list works in stack mode (LIFO, Last in First out)
这篇关于C ++中的简单链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!