C语言 输入新字符串时所有链接列表值都已更改

mf98qq94  于 2022-12-11  发布在  其他
关注(0)|答案(1)|浏览(109)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 40

struct ticket
{
    char *visitor;
    struct ticket *nextPtr;
};

// insert a new value into ticket data list
void append(struct ticket **head_ref, char *visitor)
{
    // allocate node
    struct ticket *new_node = (struct ticket *)malloc(sizeof(struct ticket));

    struct ticket *last = *head_ref;

    // put in the data
    new_node->visitor = visitor;

    // This new node is the last node
    new_node->nextPtr = NULL;

    // If the Linked List is empty, then make the new node as head
    if (*head_ref == NULL)
    {
        *head_ref = new_node;

        return;
    }

    // Else traverse till the last node */
    while (last->nextPtr != NULL)
    {
        last = last->nextPtr;
    }

    // Change the next of last node
    last->nextPtr = new_node;

    return;
}

// This function prints contents of linked list starting from head
void printList(struct ticket *node)
{
    while (node != NULL)
    {
        printf("\n%s", node->visitor);

        node = node->nextPtr;
    }
}

char Name[31] = {'\0'};

int main(void)
{
    /* Start with the empty list */
    struct ticket *head = NULL;
    int i = 0;

    printf("Name: "); // instruction
    scanf("%[^\n]%*c", Name);
    append(&head, Name);

    printList(head);

    printf("Name: "); // instruction
    scanf("%[^\n]%*c", Name);
    append(&head, Name);

    printList(head);

    return 0;
}

我想在链表中存储一些字符串,但是当我试图输入任何字符串并添加到链表中时,链表中所有先前的值都被更改为我输入的最后一个字符串。
我得到的-〉
姓名:克里斯
我的姓名:
勒布朗
我的期望-〉名称:克里斯
我的姓名:
克里斯·勒布朗

huwehgph

huwehgph1#

如前所述,你的程序目前只存储了一个指向字符数组的链接。如果你能把名字限制在一个特定的字符长度内,下面是一种在链表中存储名字的方法。

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

#define MAX 40

struct ticket
{
    char visitor[MAX];           /* Utilized your maximum length */
    struct ticket *nextPtr;
};

// insert a new value into ticket data list
void append(struct ticket **head_ref, char *visitor)
{
    // allocate node
    struct ticket *new_node = (struct ticket *)malloc(sizeof(struct ticket));

    struct ticket *last = *head_ref;

    // put in the data
    // new_node->visitor = visitor;
    strcpy(new_node->visitor, visitor); /* Store the name in this fashion */

    // This new node is the last node
    new_node->nextPtr = NULL;

    // If the Linked List is empty, then make the new node as head
    if (*head_ref == NULL)
    {
        *head_ref = new_node;

        return;
    }

    // Else traverse till the last node */
    while (last->nextPtr != NULL)
    {
        last = last->nextPtr;
    }

    // Change the next of last node
    last->nextPtr = new_node;

    return;
}

// This function prints contents of linked list starting from head
void printList(struct ticket *node)
{
    printf("\nNames\n-------------------\n");
    while (node != NULL)
    {
        printf("%s\n", node->visitor);

        node = node->nextPtr;
    }
}

char Name[31] = {'\0'};

int main(void)
{
    /* Start with the empty list */
    struct ticket *head = NULL;
    //int i = 0;

    printf("Enter name(s) or type 'quit' to end entry\n\n");

    while (1)               /* Allow for variable number of vistors to be entered and stored */
    {
        printf("Name: ");   // instruction
        scanf("%[^\n]%*c", Name);

        if ((strcmp(Name, "quit") == 0))
        {
            break;
        }

        append(&head, Name);
    }

    printList(head);

    return 0;
}

利用代码中的“MAX”常量,修改了链表结构,为39个字符加上NULL终止符的字符串分配了足够的存储空间。在main函数中对名称的输入进行了轻微的调整,可以通过输入一个或多个名称来测试程序。
下面是一个示例输出,说明了链接列表数据的存储和检索。

@Dev:~/C_Programs/Console/LinkedList/bin/Release$ ./LinkedList 
Enter name(s) or type 'quit' to end entry

Name: Chris
Name: Lebron
Name: Milly
Name: Joe
Name: quit

Names
-------------------
Chris
Lebron
Milly
Joe

您可能希望探索其他方法来分配可变数量的字符数据,但是如果您对要存储的名称的最大长度有一定的了解,则可能希望使用简单的固定长度方法在链表结构中存储字符串数据。
给予看它是否符合你项目的精神。

相关问题