清单列表C.

时间:2013-12-02 23:12:00

标签: c linked-list

我想列表由列表组成,内部列表由项目组成。

数据类型联盟:

typedef union s_datatype {
    int t_int;
    char* t_char;
    double t_double;
    bool t_bool;
} t_datatype;

项目结构:

typedef struct s_token {
    int y;
    int type;
    t_datatype value;
    struct s_token *next;
} t_token;

内部列表的结构:

typedef struct s_line {
    int x;
    int depth;
    int type;
    int number_of_tokens;
    t_token*head;
    struct s_line *next;
} t_line;

最终清单的结构:

typedef struct s_tokenized_code {
    int number_of_lines;
    t_line*head;
} t_tokenized_code;

假设我想添加新的“line”并进入该行,我想插入“token”。但我不知道,如何把它放在一起。你能帮助我吗?我不知道如何分配这个以及如何使用这个列表列表。

编辑:结构修改

1 个答案:

答案 0 :(得分:0)

<强>解决。

如果您想知道,如何(我想将项目插入列表末尾,而不是开头):

t_token*init_token (int y, int type, t_datatype value) {
    t_token*token = malloc(sizeof(struct s_token));
    if (token == NULL) {
        return NULL;
    }
    token->y = y;
    token->type = type;
    token->value = value;
    token->next = NULL;
    return token;
}

t_line*init_line (int x, int depth, int type) {
    t_line*line = malloc(sizeof(struct s_line));
    if (line == NULL) {
        return NULL;
    }
    line->x = x;
    line->depth = depth;
    line->type = type;
    line->number_of_tokens = 0;
    line->head = NULL;
    line->next = NULL;
    return line;
}

t_tokenized_code*init_code (void) {
    t_tokenized_code*code = malloc(sizeof(struct s_tokenized_code));
    if (code == NULL) {
        return NULL;
    }
    code->number_of_lines = 0;
    code->head = NULL;
    return code;
}

void insert_token (t_line*line, t_token*token) {
    if (token != NULL) {
        if (line->head == NULL){
            line->head = token;
            line->number_of_tokens += 1;
            return;
        }
        t_token*tmp = line->head;
        while (tmp->next != NULL) {
            tmp = tmp->next;
        }
        tmp->next = token;
        line->number_of_tokens += 1;
    }
}

void insert_line (t_tokenized_code*code, t_line*line) {
    if (line != NULL) {
        if (code->head == NULL) {
            code->head = line;
            code->number_of_lines += 1;
            return;
        }
        t_line*tmp = code->head;
        while (tmp->next != NULL) {
            tmp = tmp->next;
        }
        tmp->next = line;
        code->number_of_lines += 1;
    }
}

void free_code (t_tokenized_code*source) {
    if (source == NULL) {
        return;
    }
    t_line*line;
    t_token*token;
    while ((line = source->head) != NULL) {
        while ((token = line->head) != NULL) {
            line->head = token->next;
            free(token);
        }
        source->head = line->next;
        free(line);
    }
    free(source);
}

让我们做一个简单的测试 - 在第一行添加2个令牌,在第二行添加3个令牌:

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

int main (void) {
    t_tokenized_code*source = init_code();
    t_line*line = init_line(0,0,3);
    if (line == NULL) {
        return 1;
    }
    insert_line(source, line);
    t_datatype tt;

    tt.t_char = "first";
    t_token*token = init_token(0,6, tt);
    if (token == NULL) {
        return 1;
    }
    insert_token(line, token);

    tt.t_char = "second";
    token = init_token(1,6, tt);
    insert_token(line, token);

    line = init_line(1,0,3);
    insert_line(source, line);

    tt.t_char = "third";
    token = init_token(0,6, tt);
    insert_token(line, token);

    tt.t_char = "fourth";
    token = init_token(1,6, tt);
    insert_token(line, token);

    tt.t_char = "fifth";
    token = init_token(2,6, tt);
    insert_token(line, token);

    line = source->head;
    while (line != NULL) {
        printf("****\n");
        token = line->head;
        while (token != NULL) {
            printf("%s\n", token->value.t_char);
            token = token->next;
        }
        line = line->next;
    }
    free_code (source);
    return 0;
}

输出将如下:

****
first
second
****
third
fourth
fifth