问题描述
- c语言链表删除节点问题
- #include
#define NUll 0
#include
struct stu
{
char name[6];
int xuehao;
int grade;
struct stu next;
};
typedef struct stu STU;
main()
{
STU *p*p_start*p2*p_print*p_charu*p_charu2;
int ixuehao;
//输入
for (i=0;i {
p=(STU *)malloc(sizeof(STU));
printf(""请输入学生姓名:n"");
scanf(""%s""&p->name);
printf(""请输入学生的学号:n"");
scanf(""%d""&p->xuehao);
printf(""请输入学生的成绩:n"");
scanf(""%d""&p->grade);
if (i==0)
p2=p_start=p;
else
{
p2->next=p;
p2=p;
if (i==2)
p->next=NULL;
}
}
// printf(""%d""p_start->next->next->next);
// printf(""%d""p_start->next->next->xuehao);
//链表的插入
p_charu=p_start;
printf(""请输入要删除的学号n"");
scanf(""%d""&xuehao);
while (1)
{
p_charu2=p_charu->next;
if (p_charu->next->xuehao==xuehao)
{
// if (p_charu->next->next==NULL)
// p_charu->next=NULL;
else
p_charu->next=p_charu->next->next;
free(p_charu2);
break;
}
if (p_charu->xuehao==xuehao)
{
p_start=p_charu->next;
free(p_charu);
break;
}
p_charu=p->next;
}
//输出
p_print=p_start;
/
for (i=0;i {
printf(""学生姓名为:%sn""p_print->name);
printf(""学生的学号为:%dn""p_print->xuehao);
printf(""学生的分数为:%dn""p_print->grade);
if (p_print->next!=NULL)
p_print=p_print->next;
}
*/
}
这是一个删除结点并输出的程序,
加注释的输出部分没有问题,关键应该在删除结点的循环那里,但是找不见错误啊,
错误为:当删除最后一个链表也就是3时程序发生错误,停止运行。求解
解决方案
要贴到源代码的两个引号里面去
解决方案二:
不要这样贴代码,应该选择工具栏上的代码,然后再贴。
你这样贴的代码丢了很多字符,没法直接调试。
解决方案三:
贴了两次没发现贴的一样,include后面、for循环里面等等都丢字符了么
解决方案四:
已经在http://ask.csdn.net/questions/159597这个帖子详细回复了
解决方案五:
```#include
#define NUll 0
#include
struct stu
{
char name[6];
int xuehao;
int grade;
struct stu next;
};
typedef struct stu STU;
main()
{
STU *p*p_start*p2*p_print*p_charu*p_charu2;
int ixuehao;
//输入
for (i=0;i {
p=(STU *)malloc(sizeof(STU));
printf(""请输入学生姓名:n"");
scanf(""%s""&p->name);
printf(""请输入学生的学号:n"");
scanf(""%d""&p->xuehao);
printf(""请输入学生的成绩:n"");
scanf(""%d""&p->grade);
if (i==0)
p2=p_start=p;
else
{
p2->next=p;
p2=p;
if (i==2)
p->next=NULL;
}
}
// printf(""%d""p_start->next->next->next);
// printf(""%d""p_start->next->next->xuehao);
//链表的插入
p_charu=p_start;
printf(""请输入要删除的学号n"");
scanf(""%d""&xuehao);
while (1)
{
p_charu2=p_charu->next;
if (p_charu->next->xuehao==xuehao)
{
// if (p_charu->next->next==NULL)
// p_charu->next=NULL;
else
p_charu->next=p_charu->next->next;
free(p_charu2);
break;
}
if (p_charu->xuehao==xuehao)
{
p_start=p_charu->next;
free(p_charu);
break;
}
p_charu=p->next;
}
//输出
p_print=p_start;
/
for (i=0;i {
printf(""学生姓名为:%sn""p_print->name);
printf(""学生的学号为:%dn""p_print->xuehao);
printf(""学生的分数为:%dn""p_print->grade);
if (p_print->next!=NULL)
p_print=p_print->next;
}
*/
}
解决方案六:
@苏小喵
请移步
http://ask.csdn.net/questions/159593