链表的输出出错,求指点啊
大家看看就是这个链表,就是为什么数不出来啊?哪一点出了问题呢?#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;
}
}