创建散列表后无法写入,显示表达式必须具有类类型

创建散列表时后,初始化写入时提示:E0153 表达式必须具有类类型,但它具有类型 "cells"
E0153 表达式必须具有类类型,但它具有类型 "cells"

使用“.”时(表达式必须具有类类型,但它具有类型 "cells")

#include
#include
typedef enum { empty, deleted, written }information;
typedef struct node* cells;
struct node {
    int data;
    information info;
};
int main() {
    cells *cell;
    cell = (cells*)malloc(sizeof(cells));
    int n, i, jude, key, data;
    scanf_s("%d", &n);
    for (i = 0; i < 11; i++) {
        cell[i].info = empty;
    }
}

使用“->”,运行时超时跳出循环,(提示正在从cell读取无效数据)

#include
#include
typedef enum { empty, deleted, written }information;
typedef struct node* cells;
struct node {
    int data;
    information info;
};
int main() {
    cells *cell;
    cell = (cells*)malloc(sizeof(cells));
    int n, i, jude, key, data;
    scanf_s("%d", &n);
    for (i = 0; i < 11; i++) {
        cell[i]->info = empty;
    }
}

完整代码

#include
#include
typedef enum { empty, deleted, written }information;
typedef struct node* cells;
struct node {
    int data;
    information info;
};
int main() {
    cells *cell;
    cell = (cells*)malloc(sizeof(cells));
    int n, i, jude, key, data;
    scanf_s("%d", &n);
    for (i = 0; i < 11; i++) {
        cell[i]->info = empty;
    }
    for (i = 0; i < n; i++) {
        scanf_s("%d", &jude);
        if (jude == 1) {
            scanf_s("%d", &key);
            data = key;
            while (1) {
                key = key % 11;
                if (cell[key]->info != written) {
                    cell[key]->data = data;
                    cell[key]->info = written;
                    break;
                }
                else {
                    key++;
                }
            }
        }if (jude == 2) {
            scanf_s("%d", &key);
            data = key;
            key = key % 11;
            while (data != cell[key]->data) {
                key++;
            }cell[key]->info = deleted;
        }if (jude == 3) {
            scanf_s("%d", &key);
            int count = 1;
            data = key;
            key = key % 11;
            while (data != cell[key]->data) {
                key++;
                count++;
            }printf("%d", count);
        }
    }return 0;
}

E0153 表达式必须具有类类型,但它具有类型 "cells"
尝试将“.”换成“->”,结果提示正在从cell读取无效数据,运行超时跳出循环,未成功写入。
希望有人能帮我解决这个问题(散列表的创建与查找)
#include<stdio.h>
#include<stdlib.h>
typedef enum { empty, deleted, written }information;
typedef struct node* cells;
struct node {
    int data;
    information info;
};
int main() {
    cells *cell;
    cell = (cells*)malloc(sizeof(cells));
    int n, i, jude, key, data;
    scanf_s("%d", &n);
    for (i = 0; i < 11; i++) {
        cell[i].info = empty;
    }
}
 

这个代码的错误在于第13行,程序试图使用malloc分配一个指向指针的指针,但是应该分配一个指向struct node类型的指针数组。

修改方法是:

修改第13行,使用malloc分配一个指向struct node类型的指针数组,而不是一个指向指针的指针。
cell = (cells*)malloc(sizeof(struct node)*11);

修改第14行,将cell[i].info 替换为 cell[i]->info, 因为cell是一个指针数组,应该使用“->”来访问。
for (i = 0; i < 11; i++) {
cell[i]->info = empty;
}

修改第3行,将 typedef struct node* cells; 替换为 typedef struct node cells[11];

修改第10行,将 scanf_s("%d", &n); 替换为 scanf("%d", &n);

这样就可以解决问题了。

因为你用指针形式来实现数组的功能
所以每一个cell结点都需要malloc申请空间
不然你用cells[i]就越界了
或者你把cells* 改成 cells试试

cells cell;
cell = (cells)malloc(sizeof(node));