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. -
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;
-
}
-
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: -
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!");
-
}
-
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?
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
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!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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;
|
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
|
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
|
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
|
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
| |
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...
|
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?
|
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...
|
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 :...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |