#include<stdio.h> #include<string.h> #include<stdlib.h> #define LEN sizeof(struct nodelink) struct nodelink { long num; char name[20]; struct nodelink *next; }; struct nodelink *create() { struct nodelink *p,*q,*head=NULL; p=q=(struct nodelink*)malloc(LEN); for(int i=0;i<10;i++) { scanf("%ld",&p->num); getchar(); scanf("%s",p->name); if(i==0) head=p; else q->next=p; q=p; p=(struct nodelink*)malloc(LEN); } p->next=NULL; free(q); return head; } void check(struct nodelink *head,long num) { int flag=0; while(head->next!=NULL) { if(head->num==num) { printf("%c\n",head->name); flag=1; } } if(flag==0) printf("查无此人!\n"); } void deletes(struct nodelink *head,long num) { struct nodelink *p,*q; int i; for(p=head,q=head->next,i=0;i<10;i++,p=p->next,q=q->next) { if(p->num==num) { p->next=q->next; break; } } printf("修改之后的表格为:\n"); printf("num\tname\n"); while(head!=NULL) { printf("%ld\t%c\n",head->num,head->name); p=p->next; } } int main() { long num,num1; struct nodelink *head; head=create(); printf("请输入您所要查询学生的学号:\n"); scanf("%ld",&num); check(head,num); printf("请输入您所要删除学生的学号:\n"); scanf("%ld",&num1); deletes(head,num1); return 0; }代码改错
时间: 2023-12-07 07:05:18 浏览: 30
以下是修改后的代码:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN sizeof(struct nodelink)
struct nodelink {
long num;
char name[20];
struct nodelink *next;
};
struct nodelink *create() {
struct nodelink *p, *q, *head = NULL;
for (int i = 0; i < 10; i++) {
p = q = (struct nodelink*)malloc(LEN);
scanf("%ld", &p->num);
getchar();
scanf("%s", p->name);
if (i == 0)
head = p;
else
q->next = p;
p->next = NULL;
}
return head;
}
void check(struct nodelink *head, long num) {
int flag = 0;
while (head != NULL) {
if (head->num == num) {
printf("%s\n", head->name);
flag = 1;
break;
}
head = head->next;
}
if (flag == 0)
printf("查无此人!\n");
}
void deletes(struct nodelink *head, long num) {
struct nodelink *p, *q;
p = head;
q = head->next;
if (p->num == num) {
head = head->next;
free(p);
} else {
while (q != NULL) {
if (q->num == num) {
p->next = q->next;
free(q);
break;
}
p = q;
q = q->next;
}
}
printf("修改之后的表格为:\n");
printf("num\tname\n");
while (head != NULL) {
printf("%ld\t%s\n", head->num, head->name);
head = head->next;
}
}
int main() {
long num, num1;
struct nodelink *head;
head = create();
printf("请输入您所要查询学生的学号:\n");
scanf("%ld", &num);
check(head, num);
printf("请输入您所要删除学生的学号:\n");
scanf("%ld", &num1);
deletes(head, num1);
return 0;
}
```
修改说明:
1. 在 check 函数中,需要在遍历链表时判断 head 是否为 NULL,否则会出现访问空指针的问题。同时,在找到匹配的学号时应该直接输出对应的姓名并结束函数,因为后面的学生信息已经没有必要再遍历了。
2. 在 deletes 函数中,首先需要判断头节点是否是要删除的节点,如果是则直接让 head 指向下一个节点,并释放原来的头节点。否则需要遍历链表找到要删除的节点,并修改前一个节点的 next 指针,最后释放要删除的节点。注意,在遍历链表时需要同时记录前一个节点和当前节点,这样才能正确地修改前一个节点的 next 指针。
3. 在输出表格时,应该使用 %s 输出姓名,而不是 %c。同时,遍历链表时应该使用一个临时指针变量来遍历,因为遍历过程中不能改变头指针的值。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)