473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 1448
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

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

Similar topics

10
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the...
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
1
by: Simon | last post by:
Dear reader, Working with linked tables gives me some troubles. Both mdb's, frond-end and back-end are in deferent folders but on a stand alone pc delivers an reasonable response time say...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.