469,626 Members | 1,460 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

Linked List Troubles

2
Hello,

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
  1. void printList(struct node* head)
  2. {
  3.     struct node* crnt = head;
  4.  
  5.     // Loop through each node in the list printing its value
  6.     while(crnt != NULL)
  7.     {
  8.         printf("%s %s\n", crnt->fname, crnt->lname);
  9.         crnt = crnt->next;
  10.     }
  11.  
This basically looks like most examples I've seen of printing out linked lists. What I'm wondering is if somehow its putting the newest value into every single node rather than just being a printing error. If I add 3 names, it will print 3 names but all 3 show up as the last one I put in, such as if I entered "Dave Turner" "Bob Jones" and "Joe Smith", it would print out "Joe Smith" 3 times.

My code for inserting is below:

Expand|Select|Wrap|Line Numbers
  1. void insert(struct node** head, char* fname, char* lname)
  2. {
  3.     struct node* newnode;
  4.     struct node* crnt;
  5.  
  6.     // Create the node that will be inserted
  7.     newnode = (struct node*)malloc(sizeof(struct node));
  8.     newnode->fname = fname;
  9.     newnode->lname = lname;
  10.  
  11.     // Insert into empty list
  12.     if(*head == NULL)
  13.     {
  14.         newnode->next = NULL;
  15.         *head = newnode;
  16.  
  17.         return;
  18.     }
  19.  
  20.     // Find the correct place to insert the node
  21.     crnt = *head;
  22.     while(crnt->next != NULL)
  23.     {
  24.         crnt = crnt->next;
  25.  
  26.     }
  27.  
  28.         newnode->next = crnt->next;
  29.     crnt->next = newnode;
  30.     printf("Name Added!");
  31. }
  32.  
The fname and lname values that are passed to the function are just strings entered from the keyboard. Once I get this to work I'll be reading them in from a file. I basically want each new node to be inserted at the end of the list.


If you need to see more of the program let me know.

Any ideas where my troubles might be?
Feb 3 '08 #1
2 1261
JosAH
11,448 Expert 8TB
Any ideas where my troubles might be?
Yep, more than likely you're passing the addresses of the buffers in which you've
read those names to your list node function. Those are the same addresses over
and over again so all the nodes point to those same buffers. When you print your
list all nodes print the string in those same buffers that contain the strings last read.

You should copy the strings in the buffers to private buffers instead, new buffers
for every node that hold the strings.

kind regards,

Jos
Feb 3 '08 #2
Joe324
2
Yep, more than likely you're passing the addresses of the buffers in which you've
read those names to your list node function. Those are the same addresses over
and over again so all the nodes point to those same buffers. When you print your
list all nodes print the string in those same buffers that contain the strings last read.

You should copy the strings in the buffers to private buffers instead, new buffers
for every node that hold the strings.

kind regards,

Jos
Not sure I understand exactly what you mean. Are you talking about the way I'm passing the strings to the Insert function or the the way the Print function is going through them? Pointers drive me crazy!
Feb 3 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Dream Catcher | last post: by
6 posts views Thread by Steve Lambert | last post: by
1 post views Thread by Simon | last post: by
12 posts views Thread by joshd | last post: by
51 posts views Thread by Joerg Schoen | last post: by
reply views Thread by Atos | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.