head = (Node)malloc(sizeof(Node)); //创建头节点 你这个语句就错了你知道吗
时间: 2024-04-09 10:32:46 浏览: 14
是的,我知道这个语句有问题。在C语言中,`malloc`函数返回的是一个`void`指针,需要进行类型转换为所需类型。所以正确的写法应该是:
```c
head = (Node*)malloc(sizeof(Node)); //创建头节点
```
这样就将`malloc`返回的指针转换为`Node*`类型,然后赋值给`head`指针变量。这样就可以正确地创建头节点了。
相关问题
struct Stu* create(CONF conf, int mode) { struct Stu* head, * node, * end; //定义头节点,普通节点,尾节点 head = (struct Stu*)malloc(sizeof(struct Stu)); //给头节点申请内存 //若是空表,则头尾地址一致 FILE* fp3; if (mode == 1) { strcpy(conf.filesavepath, "C:\\Users\\C语言\\lab3-22281252new\\DataFile.txt"); } if ((fp3 = fopen(conf.filesavepath, "r")) == NULL) { printf("配置文件没有被打开\n"); exit(EXIT_FAILURE);//这条语句是退出程序的意思 } fscanf(fp3, "%d", &conf.number); head->elem1 = conf.number; end = head; for (int i = 0; i < conf.number; i++) { //利用for循环向链表中添加数据 node = (struct Stu*)malloc(sizeof(struct Stu));//给普通节点申请内存空间 fscanf(fp3, "%d%d%d", &(node->elem1), &(node->elem2), &(node->elem3)); //给数据域赋值 end->next = node; //让上一个节点的数据域指向当前节点 end = node; //end指向当前节点,最终end指向尾节点 } end->next = NULL; print(head); //在屏幕上显示数据 return head; //返回头节点的地址 }的作用
这段代码的作用是从文件中读取学生信息,并将学生信息存储在一个链表中。它首先定义了一个头节点head、一个普通节点node和一个尾节点end。然后它从配置文件中读取一个整数,存储在conf.number中,并将其设置为头节点的elem1成员。接下来,它利用for循环向链表中添加conf.number个普通节点,每个普通节点有三个整型成员elem1、elem2和elem3,这三个成员的值分别从文件中读取。在每次循环中,它将每个新节点添加到链表的尾部,并将end指向新节点。最后,它将链表头节点的地址返回,并在屏幕上显示链表中的数据。
#include<stdio.h> #include<stdlib.h> struct Node{ int data; struct Node* next; }; struct Node* head; void Insert(int data){ struct Node* temp1 = (struct Node*)malloc(sizeof(struct Node*));//创建一个新的节点 temp1->data = data; temp1->next = NULL; struct Node* temp2 = head; if (temp2==NULL){ //头节点为NULL // temp1->next = temp2; temp2 = temp1; temp1->next = NULL; return; } while(temp2->next!=NULL){ //temp2->next!=NULL即第一个节点的链接位置不为空 //就指向下一个位置 temp2=temp2->next; // return; } //即temp2指向temp1 temp2->next = temp1; }; void Print(){ struct Node* temp = head; while(temp!=NULL){ printf("%d ",temp->data); temp=temp->next; } printf("\n"); }; void Delete(int n); int main(){ head = NULL; Insert(2); Insert(4); Insert(6); Insert(5);//2 4 5 6 Print(); // int n; // printf("enter a position\n"); // scanf("%d",&n); // Delete(n); // Print(); }这段代码有什么问题
这段代码存在以下问题:
1. 在 `struct Node* temp1 = (struct Node*)malloc(sizeof(struct Node*));` 中,应该将 `sizeof(struct Node*)` 改为 `sizeof(struct Node)`。`malloc` 函数需要传入需要分配内存的字节数,而 `sizeof` 运算符返回的是类型的大小,所以应该使用 `sizeof(struct Node)`。
2. 在 `Insert` 函数中,当头节点为 `NULL` 时,插入新节点的逻辑有问题。将节点链接的语句应该写在 `temp1` 插入到链表中之后,而不是之前。应该修改为:
```c
if (temp2 == NULL) {
head = temp1; // 头节点为NULL时,将头节点指向temp1
return;
}
```
3. 在 `Delete` 函数中,未定义该函数的实现。如果要使用该函数,需要先定义该函数的实现。
4. 在 `main` 函数中,没有调用 `Delete` 函数。如果要测试 `Delete` 函数的实现,需要调用该函数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)