473,666 Members | 2,088 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 1462
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
5488
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
15120
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
4592
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 the frond-end mdb on the
12
3944
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 this. Could someone explain what I might be missing, or maybe point me in the direction of a good...
51
8616
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 arrays. Does anybody know if Mcilroys optimization is applicable to truly linked lists at all?
0
8624
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 if the end of the single-linked list is encountered. The single-linked list travels only one...
7
5764
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 classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
8449
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8876
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8556
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7387
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5666
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2774
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.