写了个简单链表,竟然运行错误

beyonddream2008 2009-09-05 10:52:23
#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;
}
晕了,麻烦大牛看一下,我是没看出来。。。。
...全文
37 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
beyonddream2008 2009-09-05
  • 打赏
  • 举报
回复
晕了,哈哈,谢了,把分都给你吧[Quote=引用 1 楼 starcat 的回复:]
if(head=NULL)
没细看,这儿是不是应该==?
[/Quote]
starcat 2009-09-05
  • 打赏
  • 举报
回复
if(head=NULL)
没细看,这儿是不是应该==?

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧