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

  • Thread starter Hallingrad
  • Start date
  • Tags
    List
In summary, the problem is that whenever a new node is added to the linked list, the last line read from the text file gets placed into every node.
  • #1
Hallingrad
29
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
  • #2
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:
  • #3
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?
 
  • #4
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?
 
  • #5
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.
 
  • #6
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?
 
  • #7
> 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.
 

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

1. What is a linked list?

A linked list is a data structure used to store a sequence of data elements. It consists of nodes that are connected to each other through pointers, with each node containing a data element and a pointer to the next node in the list.

2. How is a linked list different from an array?

A linked list differs from an array in several ways. Firstly, a linked list is a dynamic data structure, meaning its size can change during program execution, while an array has a fixed size. Additionally, accessing elements in a linked list requires traversing through the list, while elements in an array can be accessed directly using their index.

3. What operations can be performed on a linked list?

Common operations on a linked list include insertion of a new node, deletion of a node, and searching for a specific element. Other operations, such as sorting or merging two linked lists, can also be performed.

4. How do you implement a linked list in C?

A linked list can be implemented in C using a struct to represent each node and pointers to connect the nodes. The struct would contain a data element and a pointer to the next node. Additionally, a pointer to the head node of the list would be used to keep track of the beginning of the list.

5. What are the advantages of using a linked list?

Linked lists have several advantages, including dynamic size, efficient insertion and deletion operations, and the ability to store data elements in a non-contiguous manner. They are also more memory-efficient compared to arrays, as they do not require a fixed amount of memory to be allocated.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
5K
  • Programming and Computer Science
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
14K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
6
Views
13K
Back
Top