鱼C论坛

 找回密码
 立即注册
查看: 471|回复: 2

[已解决][Error] expected ')' before 'struct' 链表求助

[复制链接]
发表于 2023-9-16 21:36:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <stdlib.h>

//定义链表结构
struct Node {
        int data;
        struct Node* next;
};

//查找链表中第i个元素的函数
void findElement(struct Node* head, int i){
        struct Node* current = head->next;
        int index = 1;
       
        while (current != NULL && index < i){
                current = current->next;
                index++;
        }
       
        if (current == NULL || index != i) {
                printf("不存在该元素\n");
     }else {
             printf("第 %d 个元素的值为 %d\n", i, current->data);
         }
}

int main() {
       
        struct Node* head = (struct Node*)malloc(sizeof(struct));                      //为什么这里报错[Error] expected ')' before 'struct'
        head->next = NULL;
       
        //在链表中插入一些元素,这里假设有5个元素
        for (int i = 1; i <= 5; i++){
                struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
                newNode->data = i*10; //假设元素值是10的倍数
                newNode->next = head->next;
                head->next = newNode;
        }
       
        //要查找的位置
        int i;
        printf("请输入要查找的位置(1 ≤ i ≤ 5):");
        scanf("%d", &i);
       
        //调用查找函数
        findElement(head, i);
       
        //释放链表节点的内存
        struct Node* current = head->next;
        while (current != NULL){
                struct Node* temp = current;
                current = current->next;
                free(temp);
        }
        free(head);
       
        return 0;
         
}

问题见于int main()下面第一个注释,求大佬解惑
最佳答案
2023-9-16 21:36:33
在main函数中,为了创建一个新的链表头节点,需要使用malloc函数来分配内存空间。但是在代码中,malloc函数的参数是struct,而不是struct Node。因此,在struct之前应该加上Node,即应该为sizeof(struct Node)。修改代码如下:

struct Node* head = (struct Node*)malloc(sizeof(struct Node));
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-9-16 21:36:33 | 显示全部楼层    本楼为最佳答案   
在main函数中,为了创建一个新的链表头节点,需要使用malloc函数来分配内存空间。但是在代码中,malloc函数的参数是struct,而不是struct Node。因此,在struct之前应该加上Node,即应该为sizeof(struct Node)。修改代码如下:

struct Node* head = (struct Node*)malloc(sizeof(struct Node));
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-16 21:36:40 | 显示全部楼层
struct Node* head = (struct Node*)malloc(sizeof(struct)); 这一行中,你忘记了指定要分配内存的结构体类型,应该写成 struct Node* head = (struct Node*)malloc(sizeof(struct Node));

也就是说,你需要将 sizeof(struct) 改为 sizeof(struct Node)。这样就能够成功分配足够的内存来存储一个 struct Node 类型的节点,并将其赋值给 head 指针。

修改后的代码如下:

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. // 定义链表结构
  4. struct Node {
  5.     int data;
  6.     struct Node* next;
  7. };

  8. // 查找链表中第 i 个元素的函数
  9. void findElement(struct Node* head, int i) {
  10.     struct Node* current = head->next;
  11.     int index = 1;

  12.     while (current != NULL && index < i) {
  13.         current = current->next;
  14.         index++;
  15.     }

  16.     if (current == NULL || index != i) {
  17.         printf("不存在该元素\n");
  18.     } else {
  19.         printf("第 %d 个元素的值为 %d\n", i, current->data);
  20.     }
  21. }

  22. int main() {
  23.     struct Node* head = (struct Node*)malloc(sizeof(struct Node));
  24.     head->next = NULL;

  25.     // 在链表中插入一些元素,这里假设有5个元素
  26.     for (int i = 1; i <= 5; i++) {
  27.         struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  28.         newNode->data = i*10; // 假设元素值是10的倍数
  29.         newNode->next = head->next;
  30.         head->next = newNode;
  31.     }

  32.     // 要查找的位置
  33.     int i;
  34.     printf("请输入要查找的位置(1 ≤ i ≤ 5):");
  35.     scanf("%d", &i);

  36.     // 调用查找函数
  37.     findElement(head, i);

  38.     // 释放链表节点的内存
  39.     struct Node* current = head->next;
  40.     while (current != NULL) {
  41.         struct Node* temp = current;
  42.         current = current->next;
  43.         free(temp);
  44.     }
  45.     free(head);

  46.     return 0;
  47. }
复制代码


希望能够解决你的问题!
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 06:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表