C语言链表输入姓名删除结点,这个函数那里错了

来源:百度知道 编辑:UC知道 时间:2024/05/28 04:16:39
学生系统来的部分代码,这个函数编译没错.但运行时用到姓名删除就不能删了
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
struct xueshengbiao *shanchu(struct xueshengbiao *head) //删除函数//
{struct xueshengbiao *p1,*p2;
int num,n;
char xingming[10];
p1=head;
printf("按1使用学号删除,按2使用姓名删除:");
scanf("%d",&n);
if(n==1)
{printf("现有的学生(学号代表学生)如下:\n");
while(p1!=NULL)
{printf("%d,",p1->xuehao);
p1=p1->next;
}
printf("\n");
p1=head;
printf("请输入你要删除学生信息的学号:\n");
scanf("%d",&num);
while(p1->xuehao!=num&&p1->next!=NULL) //查找要删除链表//
{p2=p1;p1=p1->next;}
if(num==p1->xuehao)
{if(p1==head)head=p1->next; //删除第一个链表//
else p2->next=p1->next; //最后一个链表//
return(head);

#include<stdio.h>
void delet(struct stu_info *head)
{
int i;
struct stu_info *p,*q;
printf("请输入要删除学生的学号:\n");
q=search(head);
p=q->next;
if(p!=NULL)
{
printf("按“1”键确定删除该学生!\n");
scanf("%d",&i);
if(i==1)
{
q->next=p->next;
free(p);
printf("该学生已被删除!\n");
}
}
printf("按“1”键继续删除!\n");
}

LS正解,另外不要忘记包含头文件"string.h"
还有if(xingming==p1->xingming)
if(num==p1->xuehao)
这两个语句改成if(p1->next!=NULL)更严谨,因为while语句中
有两个判断条件 p1->xuehao!=num&&p1->next!=NULL
不满足第一个p1->xuehao!=num时即转到下一条语句,但此时系统不判断
第二个条件,故不能保证p1->next!=NULL,这样当链表中只有节点时会有问题.

void deletedata(STU *head,char numb[12])
{
STU *p,*q;
q=head;
p=head->next;
while(p!=NULL)
{
if(strcmp(numb,p->number)!=0)