代码如下:
#include<iostream>
using namespace std;
struct student
{
char name[30];
struct student * pnext;
};
struct student * creat_list(struct student * h); //建立链表函数
void print_list(struct student * h); //输出链表函数
void main()
{
struct student * head;
head = NULL;
head = creat_list(head); //建立链表
print_list(head); //输出链表
}
struct student * creat_list(struct student * h) //建立链表函数定义
{
struct student * p1,* p2;
p1 = p2 = new student;
if(p2 != NULL)
{
cin >> p2 -> name;
p2 -> pnext = NULL;
}
while(p2 -> name[0] != '#')
{
if(h == NULL)
h = p2;
else
p1 = p2;
p1 = p2;
p2 = new student;
cin >> p2 -> name;
p2 -> pnext = NULL;
}
return h;
}
void print_list(struct student * h) //输出链表函数定义
{
struct student * temp;
temp = h;
while(temp != NULL)
{
cout << temp -> name << endl;
temp = temp -> pnext;
}
}