2

Basically I have to store words in linked list with each character having its own node. I get really confused with nested structures. How do I go to the next node? I know i'm doing this completely wrong which is why I'm asking.

#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...
} 
4

2 回答 2

5

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

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;

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

于 2013-04-30T15:47:22.900 回答
4

对于链表,您通常具有指向节点结构本身中下一个节点的链接。

typedef struct node
{
    char letter;
    struct node *next;
}NODE;

然后从任何给定的节点NODE *n,下一个节点是n->next(如果不是 NULL)。

insert应该扫描列表,直到找到一个n->next为 NULL 的节点,并在最后分配一个新节点(确保将其设置next为 NULL)。

您可能需要一个函数来初始化一个给定表索引的新列表,以及一个单独的函数来初始化一个新节点。

于 2013-04-30T15:37:48.087 回答