【发布时间】:2014-02-19 22:13:09
【问题描述】:
我目前正在学习 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();
}
我想知道我做错了什么,请帮助我!
【问题讨论】:
-
'但它不能正常工作。' 和 '我想知道我做错了什么' 模糊地诊断你的问题,对不起!请编辑您的问题,而不是试图用 cmets 澄清...
标签: c++ function printing linked-list