#include
#include
#define len sizeof(struct student)
struct student
{int num;
float score;
struct student *next;
};
struct student *creat(void) // 建立
{struct student *p1,*p2,*head;
int n=0;
p1=p2=(struct student *)malloc(len);
scanf("%d %f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(len);
scanf("%d %f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void print(struct student *head) // 输出
{struct student *p;
p=head;
if(head!=NULL)
for(;p!=NULL;)
{printf("%d %f\n",p->num,p->score);
p=p->next;
}}
struct student *del(struct student *head,int num) // 删除
{struct student *p1,*p2=NULL;
if(head=NULL)
printf("空链表,无法操作");
else
{p1=head;
while((num!=p1->num)&&(p1->next!=NULL))
{p2=p1;
p1=p1->next;
}
if(num==p1->num)
{if(p1==head)
head=p1->next;
else
p2->next=p1->next;
free(p1);
printf("此链表已删除");
}
else
printf("无此数据,错误");
}
return head;
}
main()
{struct student *head,*p;
int num;
head=creat();
printf("输入要查找的数据并删除:\n");
scanf("%d",&num);
p=del(head,num);
printf("删除后数据为:");
print(p);
}
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
简单看一眼代码,没有运行的情况下.
你的删除方法中,条件判断式写错了if(head=NULL)
应该写成if (head == NULL)
,
如果写成head = NULL
,表示把head 的值赋为NULL指针,这是一个赋值,但是它本身是一个TRUE,所以你永远只会执行printf("空链表,无法操作");
写成head == NULL
是正确的,这才是判断head指针是否为NULL指针
,如果是返回TRUE,否返回FALSE
= 是赋值,==是判断