Sorry about a n00b question but I'm having trouble. I'm messing around with linked list to try to get a good handle on them for the program I have to write. The program below was intended to fill a link list with structures then print out each structure. Instead it is only printing out the last structures. If someone could tell me why this is clearly and concisely in words I understand that would be great =]

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream fin;
struct node
  {
     char name[20];    // Name of up to 20 letters
     int age;          // D.O.B. would be better
     float height;     // In metres
     node *nxt;        // Pointer to next node
  };

node *start_ptr = NULL;
node *temp;
temp = new node;

fin.open ("tfile.data");
while (fin >> temp -> name)
{
  fin >> temp -> age;
  fin >> temp -> height;
  temp -> nxt = NULL;
}

if (start_ptr == NULL)
    start_ptr = temp;
while (temp != NULL)
{
cout << temp -> name<<" ";
cout << temp -> age<<" ";
cout << temp -> height<<" ";
temp = temp->nxt;
cout << endl;
}
return 0;
}

Recommended Answers

All 5 Replies

Because you allocate only one node and you continue to overwrite it in each of your reads in your while loop. Therefore, when you set the start_ptr to the node, following the read loop, you are setting it to the end, which is actually (also) the beginning of your linked list because you have only one node in your linked list.

You need something like this:

///////
// ORIGINAL CODE
///////
node *start_ptr = NULL;
node *temp;
temp = new node;
 
fin.open ("tfile.data");
while (fin >> temp -> name)
{
  fin >> temp -> age;
  fin >> temp -> height;
  temp -> nxt = NULL;
}
 
if (start_ptr == NULL)
    start_ptr = temp;
while (temp != NULL)

//////////
// CHANGE TO SOMETHING LIKE:
/////////
node *temp;
node *start_ptr = temp = null;

string s; // you may want to use a char[] instead

fin.open ("tfile.data");
while (fin >> s)
{
  if (temp == null)
  {
    start_ptr = temp = new node();
  }
  else
  {
    temp = temp->nxt = new node();
  }
  temp->name = s; // won't work like this with a string: FYI
  fin >> temp -> age;
  fin >> temp -> height;
}

temp = start_ptr; 
while (temp != NULL)
   ....

I wrote that on the fly, but I think it is correct logic for using your definition. You will not be able to compile this, because I used a string and simplified it including leaving out a string copy you need to get the string into your char[]. I think if you look at this though, you will see what you are doing wrong in your original code.

Cheers.

Unfortunately I'm not really understanding your logic and I'm getting segmentation faults with your code

Post the new code with your changes and I will see what you've done.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream fin;
struct node
  {
     char name[20];    // Name of up to 20 letters
     int age;          // D.O.B. would be better
     float height;     // In metres
     node *nxt;        // Pointer to next node
  };

node *temp;
node *start_ptr = temp = NULL;

fin.open ("tfile.data");
while (fin >> temp -> name)
{
 if ( temp == NULL)
 {
   temp = new node();
 }
 else
 {
  temp = temp->nxt = new node();
 }
 fin >> temp -> age;
 fin >> temp -> height;
}
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.