gpt4 book ai didi

c - 链表:读取C中的字符串

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

我需要从文件中读取字符串(逐行)并将它们存储到链接列表中。我可以从文件中读取并打印它们。但是,我有如何将它们存储到链接列表的问题。我尝试创建链接列表并保存它们,如下所示:

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


typedef struct node
{
char data[256];
struct node *next;

} node_t;

node_t *head = NULL;
node_t *current = NULL;

int main(int argc, char const *argv[])
{
char temp = 'Hello';
insertToHead(temp);
return 0;
}

void insertToHead(char *word)
{
node_t *link = (node_t *) malloc(sizeof(node_t));

link->data = strcpy(link->data , word);

link->next = head;
head = link;
}

最佳答案

问题很多。

我修复了这里,现在程序至少可以编译:

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


typedef struct node
{
char data[256];
struct node *next;

} node_t;

node_t *head = NULL;
node_t *current = NULL;

void insertToHead(char *word)
{
node_t *link = (node_t *) malloc(sizeof(node_t));

strcpy(link->data , word);

link->next = head;
head = link;
}

int main(int argc, char const *argv[])
{
char *temp = "Hello";
insertToHead(temp);
return 0;
}

您确实应该学习如何读取编译器的输出。

关于c - 链表:读取C中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39858338/

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