473,574 Members | 3,125 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linked List Troubles

2 New Member
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 1459
JosAH
11,448 Recognized Expert MVP
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 New Member
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
5471
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 following: struct Node { int integer_value;
5
859
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 LOCATE subroutine that returns a node???? Any help would be appreciated. Thanks
10
15111
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 *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
6
4589
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 potential pitfalls I'd be extremely grateful. Cheers Steve
1
1940
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 good. In case both mdb,s are stored on a server the response time of the frond-end mdb is faraway from acceptable. If I import the linked tables in...
12
3935
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 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do...
51
8601
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 for linked lists, I couldn't find one. I read something about Mcilroy's "Optimistic Merge Sort" and studied some implementation, but they were for...
0
8613
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 anything from a short integer value to a complex struct type, also has a pointer to the next node in the single-linked list. That pointer will be NULL...
7
5758
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 compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template...
0
7805
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8234
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7815
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8098
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6452
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5300
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3740
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2243
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.