快期末了 给自己做点题 也顺便记录一下自己的错误
给未来遇见时解题上一些启发
也欢迎访客的进一步指正
12.28 删除值为z的结点
//省略声明 创建。。。。
void Delete(struct Student *head,int z){//删除值为z的结点
struct Student *item,*temp;
while(item->next!=NULL){
while(head->ID==z){
head=head->next;
}
item=head,temp=item->next;
if(temp->ID==z){
item->next=temp->next;
temp=temp->next;
}
else{
item=item->next;
temp=item->next;
}
}
}
//主函数
int main(){
struct Student *head;
head=Create();
int z;
printf("请输入z\n");
scanf("%d",&z);
Delete(head,z);
Print(head);
return 0;
}
运行出来发现
如果头结点需要被删除 那么删不掉
而其他结点可以正常删除
经过和同学的探讨修改为
struct Student* Delete(struct Student *head,int z){
struct Student *item,*temp,*p;
while(item->next!=NULL){
while(head->ID==z){
head=head->next;
}
item=head,temp=item->next;
if(temp->ID==z){
item->next=temp->next;
temp=temp->next;
}
else{
item=item->next;
temp=item->next;
}
}
return head;
}
int main(){
struct Student *head,*t;
head=Create();
int z;
printf("请输入z\n");
scanf("%d",&z);
t=Delete(head,z);
Print(t);
return 0;
}
解决思路:
一开始 delete函数用的 void类型
导致 可能对于delete中 head最终没有改变
依然传递给了 主函数中的print函数