gpt4 book ai didi

c - 我将 head 设置为某物,但它始终为空

转载 作者:行者123 更新时间:2023-11-30 20:18:41 25 4
gpt4 key购买 nike

我只是想打印出我的双向链表。然而,即使我明确地将头和尾设置为某些东西,当我在 main 中打印它时它总是 NULL。我不确定问题是什么。我已尽力简化代码。

在函数 grade_word_gen 中,您可以清楚地看到我将 head 设置为某物,将 tail 设置为某物。

测试.txt

*A*
Great
Fantastic
Lovely
*B*
Bad
Not Good
Terrible
<小时/>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct grade {
struct grade_word *A_head;
struct grade_word *A_tail;

struct grade_word *B_head;
struct grade_word *B_tail;

};

struct grade_word {
char *word;
struct grade_word *next;
struct grade_word *prev;
};

struct grade *create_grade() {

struct grade *new_grade = malloc(sizeof(struct grade));

// Check grade was allocated correctly
if (new_grade == NULL) {
fprintf(stderr, "ERROR: Could not allocate memory for grade\n");
exit(1);
}

// Initialise all variables
new_grade->A_head = NULL;
new_grade->A_tail = NULL;

new_grade->B_head = NULL;
new_grade->B_tail = NULL;

return new_grade;
}

struct grade_word *create_grade_word(char *word) {

struct grade_word *new = malloc(sizeof(struct grade_word));

if (new == NULL) {
fprintf(stderr, "ERROR: Unable to allocate memory for grade_words\n");
exit(1);
}

// Initialise vairables
int len = strlen(word);
new->word = malloc(sizeof(char) * (len + 1));
strcpy(new->word, word);
new->next = NULL;
new->prev = NULL;

return new;
}

void grade_word_gen(struct grade *grade_data) {

FILE *fp = fopen("test.txt", "r");
char grade;
char buf[100 + 1];

struct grade_word *new_node;
struct grade_word *head;
struct grade_word *tail;

while (fgets(buf, 100, fp) != NULL) {

if (buf[0] == '*' && buf[2] == '*') {
grade = buf[1];

} else {

new_node = create_grade_word(buf);

// Set next, prev, head, tail pointers
if (grade == 'A') {
head = grade_data->A_head;
tail = grade_data->A_tail;
} else {
head = grade_data->B_head;
tail = grade_data->B_tail;
}

// If first item set the head
if (head == NULL) {
head = new_node;
//printf("head: %s\n", head->word);
// Otherwise just add on to the list
} else {
new_node->prev = tail;
tail->next = new_node;
}

tail = new_node;
}
// Reset buffer
strcpy(buf, "\0");
}

}

void print_grade_list(struct grade_word *list, char grade) {

if (list == NULL) {
printf("Grade %c is empty, so not grade words can be printed\n", grade);
return;
}

printf("Grade: %c\n", grade);
while (list != NULL) {
printf("%s\n", list->word);
list = list->next;
}
}

int main(void) {

struct grade *new_grade = create_grade();

grade_word_gen(new_grade);

print_grade_list(new_grade->A_head, 'A');
print_grade_list(new_grade->B_head, 'B');
}

我的输出始终是Grade %c 为空,因此无法打印成绩字。我不明白为什么我的头总是空的,即使我确实设置了它。

最佳答案

除了在初始化部分分配 NULL 之外,您永远不会向 A_head 分配任何内容。因此 A_head 将保持为 NULL:

问题出在这里:

        if (grade == 'A') {
head = grade_data->A_head; // Here you make head equal A_head
tail = grade_data->A_tail;
} else {
head = grade_data->B_head;
tail = grade_data->B_tail;
}

// If first item set the head
if (head == NULL) {
head = new_node; // Here you try to save new_node.
// But you save it into head and that will not
// change A_head

您需要有如下代码:

grade_data->A_head = new_node;

这样你就可以真正改变A_head

可以在案例 A 和 B 之间共享代码的另一种方法是双指针。喜欢:

// Make a double pointer
struct grade_word **head;
. . .

// Make it point to either the A or B head pointer
head = &grade_data->A_head; // or head = &grade_data->B_head;
. . .

// Change A or B head pointer using head
*head = new_node;

关于c - 我将 head 设置为某物,但它始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53349481/

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