I'm writing a program that will (eventually) read in a list of names from a file and store them in a linked list. I have the basics of the program done and it compiles with no errors, but I'm having an issue where after I add some nodes and try to print them, it seems like every node is filled with the same values which are the most recent ones added, thought he numger of nodes seems to be correct.
This is my print function.
Expand|Select|Wrap|Line Numbers
- void printList(struct node* head)
- {
- struct node* crnt = head;
- // Loop through each node in the list printing its value
- while(crnt != NULL)
- {
- printf("%s %s\n", crnt->fname, crnt->lname);
- crnt = crnt->next;
- }
My code for inserting is below:
Expand|Select|Wrap|Line Numbers
- void insert(struct node** head, char* fname, char* lname)
- {
- struct node* newnode;
- struct node* crnt;
- // Create the node that will be inserted
- newnode = (struct node*)malloc(sizeof(struct node));
- newnode->fname = fname;
- newnode->lname = lname;
- // Insert into empty list
- if(*head == NULL)
- {
- newnode->next = NULL;
- *head = newnode;
- return;
- }
- // Find the correct place to insert the node
- crnt = *head;
- while(crnt->next != NULL)
- {
- crnt = crnt->next;
- }
- newnode->next = crnt->next;
- crnt->next = newnode;
- printf("Name Added!");
- }
If you need to see more of the program let me know.
Any ideas where my troubles might be?