C语言 当新字符串输入时,所有链接列表值改变


#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] = {''};
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;
}

我想在链表中存储一些字符串,但是当我尝试输入任何字符串并添加到链表中时,链表的所有前一个值都被更改为我输入的最后一个字符串。

我得到什么->

名称:克里斯

克里斯名称:勒布朗

勒布朗勒布朗

我所期望的->名称:克里斯。

克里斯名称:勒布朗

克里斯勒布朗

如前所述,您的程序当前仅存储指向字符数组的链接。如果你可以将你的名字限制在一个特定的字符长度,下面是你可以在链表中存储名字的一种方法。

#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("nNamesn-------------------n");
while (node != NULL)
{
printf("%sn", node->visitor);
node = node->nextPtr;
}
}
char Name[31] = {''};
int main(void)
{
/* Start with the empty list */
struct ticket *head = NULL;
//int i = 0;
printf("Enter name(s) or type 'quit' to end entrynn");
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

您可能希望探索其他方法来分配可变数量的字符数据,但是如果您对要存储的名称的最大长度有一定的了解,您可能希望利用简单的固定长度方法在链表结构中存储字符串数据。

试一试,看看它是否符合你的项目精神。

相关内容

  • 没有找到相关文章

最新更新