写了个简单链表,竟然运行错误
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
typedef struct student
{
int number;
char name[100];
struct student *next;
}STU;
const int SIZE=sizeof(STU);
STU *buildList(int num)
{
STU *head;
STU *p1;
STU *p2;
head =(STU *)malloc(SIZE);
head->next=NULL;
if(head=NULL)
{
cout<<"malloc error"<<endl;
}
memset(head->name,0,100);
head->number=0;
p1=head;
head->next=NULL;
for(int i=0;i<num;i++)
{
p2=(STU *)malloc(SIZE);
p2->next=NULL;
cout<<"please input number :"<<endl;
cin>>p2->number;
cout<<"please input name: "<<endl;
cin>>p2->name;
p1=p2;
}
p1->next=NULL;
return head;
}
void destoryList(STU *src)
{
STU *p1,*p2;
p1=p2=src;
while(p1!=NULL)
{
p2=p1->next;
free(p1);
p1=p2;
}
return ;
}
int main()
{
int num=5;
STU *p=NULL;
p=buildList(num);
destoryList(p);
system("pause");
return 0;
}
晕了,麻烦大牛看一下,我是没看出来。。。。