C linked list with an array inside

In summary, the conversation discusses an issue with a program that is only printing the last inputted number instead of all numbers. The problem is identified as reusing the same array for each node instead of allocating a new array for each node. There is also a suggestion to use a debugger to review the values of variables. The conversation then delves into the possibility of using the same array with different values, but it is ultimately determined that allocating a new array for each node is necessary for the program to function correctly.
  • #1
baddin
24
0
I have pasted my code here : http://pastebin.com/nMNarH9i
It is in language C.
Basically, I am reading in 5 numbers at a time.
Putting these numbers in array one_vote_array.
Then I need to put this array in a structure vote_node.
I also then change my next pointer in the structure to head and then make head point to this new structure.
I want to to keep on making structures while I keep reading in 5 numbers.
The problem is my program is only printing the last number i inputted in it.
 
Technology news on Phys.org
  • #2
Clearly a case of voter fraud.

so the way your program is written it will print the voting backward right? last voter to first voter.

but in your case it prints the last voter and stops so that implies that the temp->next is null.

Id go back to the first for loop and use your print test to see what voter->node is beiing set to as you iterate thru your voters.

Do you have a debugger that you can run your program thru?

it would allow you to review the values of variables step by step.
 
  • #3
You are reusing the same array of five integers for each node. You need to allocate a new array of five integers for each node.
 
  • Like
Likes 1 person
  • #4
D H said:
You are reusing the same array of five integers for each node. You need to allocate a new array of five integers for each node.

Good catch, but I thought we were supposed to lead the OP to the answer.
 
  • #5
But my function read_one_vote changes the contents of the array one_vote_array everytime it is called, so shouldn't that array be different for each node?
 
  • #6
How can it be different for each node? Your code makes exactly the opposite be true. It's the same array for each node.

If you need a different array for each node, you need to make a different array for each node.
 
  • Like
Likes 1 person
  • #7
Ok, I did what you said, and it works now. Thank you very much.
 
  • #8
I still am finding it confusing though, can't I use the same array with different value and set it to each node?
 
  • #9
Because the values of one_vote_array change each time the function read_one_vote gets called
 
  • #10
baddin said:
I still am finding it confusing though, can't I use the same array with different value and set it to each node?

Think about it at the hardware level: If you need to keep track of say, 9 different arrays, how many arrays would you need to allocate in memory? Each block of memory can only hold one value at a time, so in order for your computer to "remember" what the content of each array is, it needs to allocate a separate memory block for each one.

What your asking is possible though. If you want, you can have a buffer in which you construct each array you want. Then when it is ready, you can copy it to another newly allocated array, and reset the original array. However, this still requires you to allocate a new array each time.
 

1. What is a C linked list with an array inside?

A C linked list with an array inside is a data structure that combines the features of a linked list and an array. It stores data in a sequential manner like an array, but also maintains the flexibility of a linked list by allowing for dynamic insertion and deletion of elements.

2. How is a C linked list with an array inside different from a regular linked list?

A C linked list with an array inside differs from a regular linked list in that it uses an array to store the data elements, rather than individual nodes. This allows for faster access of elements, but also requires more memory since the array's size is fixed.

3. What are the advantages of using a C linked list with an array inside?

Some advantages of using a C linked list with an array inside include faster access of elements, efficient insertion and deletion of elements, and the ability to store large amounts of data. Additionally, this data structure is well-suited for applications that require both sequential and random access of elements.

4. How do you insert an element into a C linked list with an array inside?

To insert an element into a C linked list with an array inside, first allocate space for the new element in the array. Then, update the pointers in the linked list to include the new element. Finally, adjust the array's size and update the indices of the remaining elements to maintain the order.

5. Can you delete an element from the middle of a C linked list with an array inside?

Yes, an element can be deleted from the middle of a C linked list with an array inside. However, this operation requires adjusting the pointers in the linked list and shifting the remaining elements in the array to maintain the order. This can be a time-consuming process and may result in decreased performance if done frequently.

Similar threads

  • Programming and Computer Science
Replies
2
Views
933
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
6
Views
976
  • Programming and Computer Science
Replies
11
Views
995
  • Programming and Computer Science
Replies
20
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top