我目前正在学习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();
}
我想知道我做错了什么,请帮助我!
答案 0 :(得分:4)
很明显,函数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;
}
您分配了新节点人员。
node* person = new node;
将其字段设置为NULL
person->next = NULL;
然后,如果head不等于NULL,则将person设置为person-&gt; next
person = person->next;
当person-&gt; next设置为NULL时,意味着现在person也将等于NULL。
此外,该函数返回可在函数中更改的头部。但是,您忽略主
中的返回值addNewPerson(head);
至少应该有
head = addNewPerson(head);
有效函数addNewPerson可以采用以下方式
node* addNewPerson(node* head)
{
node* person = new node;
cout << "Name: ";
cin >> person->name;
person->next = head;
head = person;
return head;
}
主要是你必须写
for (unsigned short i = 1; i <= people; i++)
{
head = addNewPerson(head);
cout << endl;
}
函数printList不输出整个列表。它仅输出头部的第一个节点的数据成员名称。
void printList(node* head)
{
node* temp = head;
cout << temp->name << endl;
}
它应该看起来如下
void printList(node* head)
{
for ( ; head; head = head->next )
{
cout << tmp->name << endl;
}
}
最后主要看起来应该是
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();
}
答案 1 :(得分:0)
在你的addNewPerson函数person-&gt; next永远不会被设置,因为head总是为NULL。
答案 2 :(得分:0)
当您添加新人时,它实际上并未放入链接列表中,因为您从未调用head -> next = person
之类的内容。
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;
}
在开始时,您不能只将head
传递给addNewPerson
,因为它是一个带空值的指针,addNewPerson
方法接受一个指针值(值的值)它指向的内存地址而不是对head
的引用。所以你需要使用
node* addNewPerson(node* &head) { ... }
代替。最后,当您声明temp
时,它首先会收到head
的值,因此稍后访问temp
只会再次给您NULL
。您需要将其更改为
node* &temp = head;
答案 3 :(得分:0)
您的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
应该按照说明进行操作并打印列表,而不是一次只打印一个人。
void printList(node* head)
{
node* tmp = head;
while(tmp->next != NULL) {
tmp = tmp->next;
cout >> tmp->name >> endl;
}
}
使用新的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();
}
另外,为什么使用unsigned short
和char
数组?你的内存有限吗?为什么不使用int
和string
?
答案 4 :(得分:0)
我认为问题在于如何在addNewPerson中添加链接列表。
addNewPerson的输入是'head'所以每次在向链表添加新节点之前,我们需要一直向下遍历链表直到最后一个节点,然后将'person'附加到链表。
答案 5 :(得分:0)
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;
}