#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;
}
I want to store some string the linked list, but when I try to input any string and add to the linked list, all the previous value of the linked list has been changed to the last string that I have enter.
What I get ->
Name: Chris
Chris Name: Lebron
Lebron Lebron
What I expect -> Name: Chris
Chris Name: Lebron
Chris Lebron
CodePudding user response:
As noted your program currently just stores a link to the character array. Following is one route you might go with storing a name in your linked list if you can afford to limit your names to a specific character length.
#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;
}
Utilizing the "MAX" constant in your code, the linked list structure was revised to allocate enough storage for a character string of 39 characters plus the NULL terminator. Along with a slight tweak to the input of names in the main function, the program can be tested with the entry of one or more names.
Following is a sample output illustrating the storage and retrieval of your linked list data.
@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
You might want to explore other approaches for allocating variable amounts of character data down the road, but you might want to utilize the simple fixed length approach for storing string data in your linked list structure if you have a feel for the maximum length of the names to be stored.
Give that a try to see if it meets the spirit of your project.