gpt4 book ai didi

c - C中链表的动态数组

转载 作者:行者123 更新时间:2023-12-01 16:15:57 27 4
gpt4 key购买 nike

基本上我必须将单词存储在链表中,每个字符都有自己的节点。我真的对嵌套结构感到困惑。如何进入下一个节点?我知道我这样做是完全错误的,这就是我问的原因。

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

typedef struct node
{
char letter;
}NODE;


typedef struct handle
{
NODE **arr;
}HANDLE;

HANDLE * create();


void insert(handle **, char, int);

int main(int argc, char **argv)
{
FILE *myFile;
HANDLE *table = (HANDLE *)malloc(sizeof(HANDLE));
NODE *linked = (NODE *)malloc(sizeof(NODE));
int counter = 0;

linked = NULL;
table->arr[0] = linked;

char c;


myFile = fopen(argv[argc], "r");

while((c = fgetc(myFile)) != EOF)
{
if(c != '\n')
insert(&table, c, counter);

else
{
counter++;
continue;
}
}
}


void insert(HANDLE **table, char c, int x)
{
(*table)->arr[x]->letter = c; //confused on what to do after this or if this
//is even correct...
}

最佳答案

您有一个单词链接列表,每个单词都是一个字符链接列表。我对吗?如果是这样,最好使用它们的名称:

typedef struct char_list
{
char letter;
struct char_list * next;
} word_t;

typedef struct word_list
{
word_t * word;
struct word_list_t * next;
} word_list_t;

现在,您可以根据需要填充列表。

关于c - C中链表的动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16303677/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com