Why Does Every Node in My Linked List Have the Same Data from a Text File in C?

  • Thread starter Thread starter Hallingrad
  • Start date Start date
  • Tags Tags
    List
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to implementing a linked list in C, specifically addressing why all nodes in the linked list appear to contain the same data from a text file. The scope includes technical explanations and debugging strategies.

Discussion Character

  • Technical explanation
  • Debugging
  • Exploratory

Main Points Raised

  • One participant describes an issue where every node in the linked list ends up with the last line read from the text file, suggesting a problem with how nodes are created and data is assigned.
  • Another participant points out that the line setting newNode = NULL immediately after allocation may be problematic, as it discards the pointer obtained from malloc.
  • Several participants emphasize the need to maintain a pointer to the first node of the linked list for proper traversal and management.
  • There is a suggestion to initialize the variable tempNode to NULL and to ensure that the index variable i is reset before parsing new data.
  • One participant mentions that removing newNode = NULL did not resolve the issue, indicating that nodes created in the loop seem to inherit the data of the last node processed.
  • Another participant encourages using a debugger to step through the code and check if newNode is assigned a different address with each call to malloc.
  • A later reply reflects on the lack of emphasis on debugging tools in programming education, highlighting their importance in identifying mistakes.

Areas of Agreement / Disagreement

Participants generally agree on the potential issues with memory allocation and data assignment in the linked list implementation, but no consensus is reached on the exact cause of the problem or the best solution. Multiple competing views on debugging strategies and code structure remain.

Contextual Notes

There are unresolved issues regarding the initialization of variables and the handling of memory allocation, as well as the overall design of the linked list. Specific assumptions about the structure of the data being read from the text file are not fully clarified.

Hallingrad
Messages
29
Reaction score
0
Hey guys,

I'm trying to implement a linked list in C, with each node carrying fields from a text file. The problem is that whatever was the last line read from the text file gets placed into every single node. Even more strange is that when I add an additional node outside of the while loop it doesn't replace all the previous nodes. Here's the loop that's causing me problems. Any help would be much appreciated!

Code:
while (!feof(file_ptrDR)) {
		
		newNode = (lnode *)malloc(sizeof(lnode));
                newNode = NULL;
	
		c = fgetc(file_ptrDR);
		while (c != ',') {
			NAME[i] = c;
			i++;
			c = fgetc(file_ptrDR);
			
		}
		
		i = 0;
		c = fgetc(file_ptrDR);
		newNode->name = NAME;
		while ((c = fgetc(file_ptrDR)) != ',') {
			PASSWORD[i] = c;
			i++;
		}
		i = 0;
		c = fgetc(file_ptrDR); //get past the space
		(newNode->password) = PASSWORD;
		c = fgetc(file_ptrDR);
		while (c != '\n' && c != EOF)  {
			TYPE[i] = c;
			c = fgetc(file_ptrDR);
			i++;
		}
		i = 0;
		newNode->type = TYPE;
		newNode->next = tempNode;
		tempNode = newNode;
}
linkedlist = tempNode;
 
Technology news on Phys.org
Code:
    newNode = (lnode *)malloc(sizeof(lnode));
    newNode = NULL;
Why are you zeroing out newNode just after allocating it? Perhaps you wanted this?
Code:
    newNode = (lnode *)malloc(sizeof(lnode));
    newNode->next = NULL;
Also you need to save the first instance of newnode in another variable so you have a pointer to the start of the linked list.
Code:
static lnode * firstNode = NULL;
...
    newNode = (lnode *)malloc(sizeof(lnode));
    if(firstNode == NULL)
        firstNode = newNode;
 
Last edited:
Jeff Reid said:
Code:
    newNode = (lnode *)malloc(sizeof(lnode));
    newNode = NULL;
Why are you zeroing out newNode just after allocating it? Perhaps you wanted this?
Code:
    newNode = (lnode *)malloc(sizeof(lnode));
    newNode->next = NULL;
Also you need to save the first instance of newnode in another variable so you have a pointer to the start of the linked list.
Code:
static lnode * firstNode = NULL;
...
    newNode = (lnode *)malloc(sizeof(lnode));
    if(firstNode == NULL)
        firstNode = newNode;

Making it null was just one of the various solutions I tried to rectify the problem. It didn't change the output when traveling through the list, but you're right, I should probably remove it.

As for the firstNode, where would I place it? The way I designed it, I'm adding new nodes to the head, so why would I need to save the very first newNode?
 
I didn't realize you wanted to create a LIFO (last in first out) linked list. I don't see how setting newNode = NULL isn't causing problems, because it will always throw away the pointer from malloc and instead set it to zero. I'm assuming that tempNode is initialized to NULL? Otherwise I don't see the problem. You might want to move the last i=0 to before the first part of the loop so it's clearly initialized to zero before the first time you parse "NAME".

Have you tried stepping through this code for a few loops with a debugger to see what's going on?
 
Jeff Reid said:
I didn't realize you wanted to create a LIFO (last in first out) linked list. I don't see how setting newNode = NULL isn't causing problems, because it will always throw away the pointer from malloc and instead set it to zero. I'm assuming that tempNode is initialized to NULL? Otherwise I don't see the problem. You might want to move the last i=0 to before the first part of the loop so it's clearly initialized to zero before the first time you parse "NAME".

Have you tried stepping through this code for a few loops with a debugger to see what's going on?

Making it not null didn't help.

It seems that any nodes made in the while loop get turned into what the last node was before the while loop exits, but if I manually add another node after that, it doesn't affect the previous nodes. Frankly I'm at a loss after hours of staring at the code.
 
So you did remove
Code:
     newNode = NULL;

If you run with the debugger, did you check to make sure newNode is being assigned a different address each time call malloc?
 
> Have you tried stepping through this code for a few loops with a debugger to see what's going on?

Why don't they teach new programmers about the debugger? It is so much easier to spot mistakes and it also teaches users a lot. It is the number one tool to use and no one seems to know about it.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
14K
  • · Replies 1 ·
Replies
1
Views
11K
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K