鱼C论坛

 找回密码
 立即注册
查看: 632|回复: 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 指针。

修改后的代码如下:
#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 Node));
    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;
}

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-27 03:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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