books* creat() { books* h; books* head; books* p1, * p2; int n = 0; int w; p1=(books*)malloc(LEN); printf("\tt\t我们先创建图书表\n\n"); printf("请输入序列号:"); scanf("%d", &p1->id); printf("请输入年份:"); scanf("%d", &p1->year); printf("请输入作者:"); scanf("%s", &p1->author); printf("请输入书名:"); scanf("%s", &p1->name); head = p1; head->next = NULL; printf("\n\n请按下面数字选择:\n\n"); printf(" 1.继续 2.结束"); printf("\n请选择:"); scanf("%d", &w); p2 = head; while (w == 1) { p1 = (books*)malloc(LEN); printf("请输入序列号:"); scanf("%d", &p1->id); printf("请输入日期:"); scanf("%d", &p1->year); printf("请输入作者:"); scanf("%s", &p1->author); printf("请输入书名:"); scanf("%s", &p1->name); p2->next = p1; p1->next = NULL; p2 = p2->next; printf("\n\n请按下面数字选择:\n\n"); printf(" 1.继续 2.结束"); printf("\n请选择:\n"); scanf("%d", &w); } return head; }为什么删除不了头结点?
时间: 2024-03-24 09:37:17 浏览: 21
因为在这个函数中,头结点是通过直接赋值得到的,而不是通过动态分配内存得到的。因此,如果直接删除头结点,可能会导致内存泄漏或程序崩溃。一种可行的方法是,在删除头结点之前,先将头结点的指针指向下一个节点,然后释放头结点的内存。例如:
```c
books* delete(books* head)
{
if (head == NULL) {
printf("链表为空,无法删除!\n");
return NULL;
}
books* p = head->next;
free(head);
head = p;
return head;
}
```
相关问题
#include<stdio.h> #include<stdlib.h> struct l{ int a; struct l *next; }; struct l *creat() { struct l *head,*p1,*p2; int n,i; scanf("%d",&n); head=NULL; p1=(struct l *)malloc(sizeof(struct l)); scanf("%d",&p1->a); p1->next=NULL; head=p1; p2=p1; for(i=0;i<n-1;i++) { p1=(struct l *)malloc(sizeof(struct l)); scanf("%d",&p1->a);p1->next=NULL; p2->next=p1; p2=p1; } return head; } void printl(struct l *head) { struct l *p; p=head; while(p!=NULL); { printf("%d ",p->a); p=p->next; } } int main() { struct l *head; head=creat(); printl(head); return 0; },这个代码问题在哪
这段代码有两个问题:
1. 在 `printl` 函数中,while 循环的条件中多了一个分号,导致循环体中的语句无法执行,造成无法输出链表中的元素。
2. 在 `main` 函数中,调用 `creat` 函数后没有判断返回的链表头指针是否为 NULL,如果内存分配失败,会返回 NULL,此时调用 `printl` 函数会出现错误。
修改后的代码如下:
```c
#include<stdio.h>
#include<stdlib.h>
struct l{
int a;
struct l *next;
};
struct l *creat() {
struct l *head,*p1,*p2;
int n,i;
scanf("%d",&n);
head=NULL;
for(i=0;i<n;i++)
{
p1=(struct l *)malloc(sizeof(struct l));
if(p1 == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
scanf("%d",&p1->a);
p1->next=NULL;
if(head==NULL) {
head=p1;
p2=p1;
} else {
p2->next=p1;
p2=p1;
}
}
return head;
}
void printl(struct l *head) {
struct l *p;
p=head;
while(p!=NULL) // 去掉多余的分号
{
printf("%d ",p->a);
p=p->next;
}
}
int main() {
struct l *head;
head=creat();
if(head == NULL) {
exit(1);
}
printl(head);
return 0;
}
```
data *creatNew(data *head){}有什么错
这段代码虽然看起来像是一个函数定义,但是函数体中没有任何有效的代码,也没有返回任何值,因此这个函数是有问题的。同时,函数名中的`creatNew`应该是`createNew`才是正确的英文拼写。正确的函数定义应该包括函数名、参数列表、返回值类型和函数体,例如:
```c
data *createNew(data *head){
// 函数体中应该包含有效的代码
return head;
}
```
这个函数的作用是创建一个新的数据节点,并将它插入到链表的头部。`data`是节点的数据类型,`head`是链表头节点的指针。函数体中应该包括创建新节点、初始化节点数据、修改链表指针等操作。函数返回链表头节点的指针,以便在调用函数后可以获取新的链表头节点。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)