我目前正在学习 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; …Run Code Online (Sandbox Code Playgroud)