【问题标题】:How to add a string to linked list in c?如何在c中将字符串添加到链表?
【发布时间】:2019-06-15 00:44:45
【问题描述】:

我已经知道如何在 C 中将 int 添加到链表中,但我需要添加一个字符串,但它根本不起作用。

main函数从用户那里获取数据,加入链表后在show函数中打印出来。

列表和主要

struct nlista{
    char dado[10];
    struct nlista *prox;
}*Head;

int main(){
    int op;
    char data[10];
    Head = NULL;

    printf("type a value: ");
    scanf("%s",&data);
    inserir(data);
    printf("the element is : ");
    show();
}

inserir():在列表末尾添加元素

    void inserir(char data){

    nlista *novoelemento;
    novoelemento = (struct nlista *)malloc(sizeof(struct nlista));
    nlista *check;
    check = (struct nlista *)malloc(sizeof(struct nlista));

    novoelemento->dado = data;

    if(Head == NULL){
        Head = novoelemento;
        Head->prox = NULL;
    }
    else{
        check = Head;
        while(check->prox != NULL)
            check = check->prox;

        check->prox = novoelemento;
        novoelemento->prox = NULL;  
    }

show():显示链表

    void show()
{
    nlista *check;
    check = (struct nlista *)malloc(sizeof(struct nlista));

    check = Head;
    if (check == NULL){
        return;
    }

    while(check != NULL) {
        printf("%s", check->dado);
        check=check->prox;
    }
    printf("\n");
}

我错过了什么?编译器消息是:从 char* 到 char 的无效转换。在 inserir(data) 行中;

【问题讨论】:

  • 为什么要在这一行新建一个元素:check = (struct nlista *)malloc(sizeof(struct nlista));?新创建的元素有什么用途?如果inserir 应该添加一个字符串,为什么它需要一个字符参数?让 cmets 解释为什么要按原样编写代码会有所帮助。
  • data 衰减为指针 (char *) 但 void inserir(char data) 要求 char
  • @DavidSchwartz 检查将验证列表的实际元素是否为空。该应用程序是关于添加具有多种数据类型的链表,但我坚持添加字符串,所以我简化了代码。那么应该是什么样的参数呢?
  • @saulodev 可能是你想用什么来表示一个字符串。最常见的是char*
  • @davidschwartz 不能解决同样的问题

标签: c string data-structures linked-list


【解决方案1】:

抱歉,我在您的代码中发现了很多错误。我为您的问题编写了一个非常简单的解决方案。请参考并纠正您的错误。

值不过是字符串,即数据(在代码中)作为主函数中函数 inserir 中的参数传递。请记住,链表的每个节点都由一个字符串元素和指向下一个节点的指针组成。字符串可以有各种长度。第 1 步和第 2 步将解决这个问题(参见代码)。希望你现在清楚了。

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

typedef struct nlista{
    char *data;
    struct nlista *prox;
}Node;

Node * inserir(Node *, char *);
'''inserir function should return your head node'''
void show(Node *);
'''show function takes head node'''

int main()
{
    char data[10];
    Node * Head = NULL;

    printf("Type a value: ");
    scanf("%s",data);
    Head = inserir(Head,data);
    printf("the element is : ");
    show(Head);
}

Node* inserir(Node *Head, char *value)
{

    Node *novoelemento;
    novoelemento = (Node *)malloc(sizeof(Node));

    //step 1. allocate memory to hold word
     novoelemento->data = malloc(strlen(value)+1);

    //step 2. copy the current word
    strcpy(novoelemento->data,value);

    Node *check;
    check = (Node *)malloc(sizeof(Node));

    if(Head == NULL){
        Head = novoelemento;
        Head->prox = NULL;
    }
    else{   
        check = Head;
        while(check->prox != NULL)
            check = check->prox;

        check->prox = novoelemento;
        novoelemento->prox = NULL;  
    }
  return Head;    
}    
void show(Node *Head)
{
    //check is of Node type using which you traverse the list and print the values
    Node *check;
    check = (Node *)malloc(sizeof(Node));
    check = Head;
    if (check == NULL){
        return;
    }

    while(check != NULL) {
        printf("%s", check->data);
        check=check->prox;
    }
    printf("\n");
}    

希望这会对你有所帮助。

【讨论】:

  • 虽然这还不错,但我的解释是他故意想要固定长度的字符串。不过没关系。让我们看看他拿的是哪一个。
  • 我理解他的问题。我想给他一个更笼统的答案。感谢您指出那部分。
  • 非常感谢 Saurav,我有一个问题,inserir 中的“char *value”可以用 char *data 移动吗?
  • 是的,您可以在函数定义中更改inserir。 value 是我给的参数的名称,它只引用数据。希望你现在清楚了。
【解决方案2】:

我们有 char dado[10];novoelemento-&gt;dado = data; 如您所见,这不会编译。

您似乎想要strncpy(novoelemento-&gt;dado, data, 10)[9] = 0; 这将复制数据中的字符串并确保它正确地以空值终止。

如果你有strlcpy,你可以做得更好strlcpy(novoelemento-&gt;dado, data, 10);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 2021-12-30
    • 2022-06-12
    • 1970-01-01
    相关资源
    最近更新 更多