打印函数链表 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21893674/
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
Print Function Linked Lists C++
提问by Tóth Attila
I'm currently learning Linked Lists in C++, and I can't write a print function which prints out the elements of the list; I mean I wrote the function but it doesn't work properly.
我目前正在学习 C++ 中的链表,我无法编写打印列表元素的打印函数;我的意思是我写了这个函数,但它不能正常工作。
#include <iostream>
using namespace std;
struct node
{
char name[20];
node* next;
};
node* addNewPerson(node* head)
{
node* person = new node;
cout << "Name: ";
cin >> person->name;
person->next = NULL;
if (head == NULL) //is empty
{
head = person;
}
else
{
person = person->next;
}
return head;
}
void printList(node* head)
{
node* temp = head;
cout << temp->name << endl;
}
int main()
{
node* head = NULL;
node* temp = head;
unsigned short people = 0;
cout << "How many people do you want to invite to the party?" << endl;
cout << "Answer: ";
cin >> people;
cout << endl;
for (unsigned short i = 1; i <= people; i++)
{
addNewPerson(head);
cout << endl;
}
cout << "LIST: " << endl;
cout << endl;
while (temp != NULL)
{
printList(temp);
temp = temp->next;
}
cin.get();
}
I am wondering what I'm doing wrong, please help me!
我想知道我做错了什么,请帮助我!
回答by Vlad from Moscow
It is obvious that function addNewPerson is wrong.
很明显,函数 addNewPerson 是错误的。
node* addNewPerson(node* head)
{
node* person = new node;
cout << "Name: ";
cin >> person->name;
person->next = NULL;
if (head == NULL) //is empty
{
head = person;
}
else
{
person = person->next;
}
return head;
}
You allocated new node person.
您分配了新的节点人员。
node* person = new node;
Set its field next to NULL
将其字段设置为 NULL
person->next = NULL;
Then if head is not equal to NULL you set person to person->next
然后,如果 head 不等于 NULL,则将 person 设置为 person->next
person = person->next;
As person->next was set to NULL it means that now also person will be equal to NULL.
由于 person->next 被设置为 NULL,这意味着现在 person 也将等于 NULL。
Moreover the function returns head that can be changed in the function. However you ignore returned value in main
此外,该函数返回可以在函数中更改的 head。但是你忽略了 main 中的返回值
addNewPerson(head);
At least there should be
至少应该有
head = addNewPerson(head);
The valid function addNewPerson could look the following way
有效函数 addNewPerson 可能如下所示
node* addNewPerson(node* head)
{
node* person = new node;
cout << "Name: ";
cin >> person->name;
person->next = head;
head = person;
return head;
}
And in main you have to write
主要你必须写
for (unsigned short i = 1; i <= people; i++)
{
head = addNewPerson(head);
cout << endl;
}
Function printList does not output the whole list. It outputs only data member name of the first node that is of head.
函数 printList 不输出整个列表。它只输出首个节点的数据成员名称。
void printList(node* head)
{
node* temp = head;
cout << temp->name << endl;
}
It should look the following way
它应该如下所示
void printList(node* head)
{
for ( ; head; head = head->next )
{
cout << head->name << endl;
}
}
And at last main should look as
最后主要应该看起来像
int main()
{
node* head = NULL;
unsigned short people = 0;
cout << "How many people do you want to invite to the party?" << endl;
cout << "Answer: ";
cin >> people;
cout << endl;
for ( unsigned short i = 0; i < people; i++ )
{
head = addNewPerson(head);
cout << endl;
}
cout << "LIST: " << endl;
cout << endl;
printList( head );
cin.get();
}
回答by user1052842
In your addNewPerson function person->next never gets set, because head is always NULL.
在您的 addNewPerson 函数中,person->next 永远不会被设置,因为 head 始终为 NULL。
回答by Alex Chen
When you are adding a new person, it is not actually put into the linked list, since you have never invoked something like head -> next = person
.
当您添加一个新人时,它实际上并未放入链表中,因为您从未调用过类似head -> next = person
.
The section in your addNewPerson
routine should be something like
你的addNewPerson
例程中的部分应该是这样的
if (head == NULL) {
head = person;
} else {
node * end = head;
while (end -> next != NULL) // find the end of the list to insert new person
end = end -> next;
end -> next = person;
}
And in the beginning you can't just pass head
into addNewPerson
, since it is a pointer with null value, the addNewPerson
method accepts a pointer by value (value of the memory address it is pointing at) rather than a reference to head
. So you need to use
而在开始的时候你不能只是通过head
进入addNewPerson
,因为它是空值的指针,该addNewPerson
方法接受由值指针(它在指向的内存地址的值),而不是一个参考head
。所以你需要使用
node* addNewPerson(node* &head) { ... }
instead. Finally, when you declare temp
, it first receives the value of head
as well, so later access to temp
would only give you NULL
again. You need to change it to
反而。最后,当您声明 时temp
,它首先接收到head
as的值,因此以后访问temp
只会NULL
再次给您。你需要把它改成
node* &temp = head;
回答by Jonny Henly
addNewPerson method
addNewPerson 方法
Your addNewPerson
method never added a new person to the list it just set the head to the new person.
您的addNewPerson
方法从未将新人添加到列表中,它只是将头部设置为新人。
node* addNewPerson(node* head)
{
node* person = new node;
cout << "Name: ";
cin >> person->name;
person->next = NULL;
if (head->next == NULL) //is empty
{
head->next = person;
}
else
{
person->next = head->next;
head->next = person;
}
return head;
}
printList method
打印列表方法
Your method printList
should do what it says and print the list, instead of just printing one person at a time.
你的方法printList
应该按照它说的做并打印列表,而不是一次只打印一个人。
void printList(node* head)
{
node* tmp = head;
while(tmp->next != NULL) {
tmp = tmp->next;
cout >> tmp->name >> endl;
}
}
main method
主要方法
Upadted your main method with the new printList
method.
使用新printList
方法更新了您的主要方法。
int main()
{
node* head = new node;
unsigned short people = 0;
cout << "How many people do you want to invite to the party?" << endl;
cout << "Answer: ";
cin >> people;
cout << endl;
for (unsigned short i = 1; i <= people; i++)
{
addNewPerson(head);
cout << endl;
}
cout << "LIST: " << endl;
cout << endl;
cout << printList(head);
cin.get();
}
Also why are you using unsigned short
and char
arrays? Do you have a limited amount of memory? Why not just use int
and string
?
另外你为什么使用unsigned short
和char
数组?你的内存有限吗?为什么不直接使用int
and string
?
回答by user3307862
I think the problem is in how the link list is being added to in addNewPerson.
我认为问题在于如何在 addNewPerson 中添加链接列表。
The input to addNewPerson is 'head' so each time, before adding a new node to the linked list, we need to traverse all the way down the linked list till the last node before appending 'person' to the linked list.
addNewPerson 的输入是'head',所以每次在向链表添加新节点之前,我们需要一直向下遍历链表直到最后一个节点,然后将'person'附加到链表。
回答by S421
struct node
{
char name[20];
node* next;
};
node* addNewPerson(node* head)
{
node* person = new node;
//cout << "Name: ";
cin >> person->name;
person->next = head;
head = person;
return head;
}
void PrintLL(node *head)
{
while (head!=NULL){
cout<< head->name <<endl;
head=head->next;
}
}
int main() {
node* head = NULL;
node* temp = head;
int num_ppl;
cout << "How many people do you want to invite to the party?" << endl;
cout << "Answer: ";
cin >> num_ppl;
for(int arr_i = 0; arr_i < num_ppl; arr_i++){
head = addNewPerson(head);
}
PrintLL(head);
return 0;
}