简体   繁体   English

如何在C中的链表中添加字符串?

[英]How to add a string to linked list in c?

I already get how to add an int to a linked list in C but I need to add a string and it simply doesn't work. 我已经知道了如何在C语言的链表中添加int,但是我需要添加一个字符串,但这根本行不通。

The main function gets the data from the user and prints it in the show function after adding it to the linked list. main函数从用户那里获取数据,然后将其添加到链接列表中,然后在show函数中进行打印。

list and main 清单和主要

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(): add the element in the end of list 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(): display the linked list 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");
}

What am I missing? 我想念什么? The compiler message is: invalid conversion from char* to char. 编译器消息是:从char *到char的无效转换。 in the line of inserir(data); 在inserir(data)行中;

We have char dado[10]; 我们有char dado[10]; but novoelemento->dado = data; 但是novoelemento->dado = data; As you discovered, this does not compile. 如您所知,它不会编译。

You seem to want strncpy(novoelemento->dado, data, 10)[9] = 0; 您似乎想要strncpy(novoelemento->dado, data, 10)[9] = 0; This copies the string within data over and ensures it's properly null terminated. 这将复制数据中的字符串,并确保将其正确终止为null。

If you have strlcpy , you can do it better as strlcpy(novoelemento->dado, data, 10); 如果您有strlcpy ,则可以像strlcpy(novoelemento->dado, data, 10);一样strlcpy(novoelemento->dado, data, 10);

Sorry, but I found many mistakes in your code. 抱歉,但是我在您的代码中发现许多错误。 I have written a very simple solution for your question. 我为您的问题写了一个非常简单的解决方案。 Kindly refer and correct your mistakes. 请参考并纠正您的错误。

Value is nothing but, the string ie data(in the code) passed as an argument in function inserir in the main function. 值不过是字符串,即数据(在代码中)作为参数传递给主函数inserir中的参数。 Remember each node of the linked list consists of a string element and the pointer to the next node. 请记住,链表的每个节点都由一个字符串元素和指向下一个节点的指针组成。 The string can be of various length. 字符串的长度可以不同。 step 1 and step 2 will take care of that(see the code). 步骤1和步骤2将解决此问题(请参见代码)。 Hope you are clear now. 希望你现在明白了。

#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");
}    

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM