The comments in the code represent what I am thinking. So please tell me what I am thinking wrong.

Basically as I see it, a linked list contains a head node, the place to start, and each node points to the next node. So I can read in item by item into an instance of my structure and then push back the amount of elements in the list. It isn't working how I wanted though.

cpp(30) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

Is the fist error that comes up. However this worked fine in an older program where I was using vectors and the same method to read data into the vector. Any insight into this problem?

Any help at all would be great. Even a small tutorial on making linked list of structures like I am. Couldn't find any on google. I am still learning everything about linked lists so please be patient with me.

Re: Question on reading text file into structure.

Is what I have come up with from the tutorial you showed me, but I have a few questions.

node *head = NULL

I create a new node, type structure, and call it *head set equal to NULL. So this is saying what exactly? I guess I am still confused about pointers. It is pointing to nothing?

Exactly, it is pointing to nothing.

Quote

temp = new node;

This was the line they gave in the tutorial, but the compiler wouldn't accept it. So I changed the temp to head which worked. In this line I am creating a node, right? And calling it head?

It's not that head is a magic word. When you wrote temp = new node you hadn't declared temp. If you had written

node *temp = new node;

or

node *temp;
// some other code ...
temp = new node;

that would have been fine.

Quote

head->next = NULL;

Finally, this causes the node I just created (head) and entered information for, and it sets the pointer in the structure equal to NULL? Basically saying end of list?

Correct. You can test, e.g., say you are working with a arbitrary node temp somewhere in a list:

if(temp->next!=NULL) {
// do something with the next node
}

That way, you can avoid trying to access a node that doesn't exist (which would cause your program to crash).

By the way, given what you are working on here there is no reason for #include <list> in this program. That <list> header is needed only if you want to use an STL list -- a whole different animal that you should learn about at some point (actually easier to use than creating your own linked list, but it is important for you to learn linked lists anyway).

Re: Question on reading text file into structure.

So instead of head = new node; you want to do something like node *temp = new node;

Here's the add_node_at_end() from the tutorial. Notice it declares 2 temp node pointers: one to hold the new node and it's information, and the second one to use when 'walking' the list of nodes to find the end of the list.

Re: Question on reading text file into structure.

Posted 11 March 2010 - 09:50 PM

Quote

By the way, given what you are working on here there is no reason for #include <list> in this program. That <list> header is needed only if you want to use an STL list -- a whole different animal that you should learn about at some point (actually easier to use than creating your own linked list, but it is important for you to learn linked lists anyway).

Originally I was thinking of doing something like I did in my vector program... and use push_back in the STL list container.

Re: Question on reading text file into structure.

Posted 11 March 2010 - 10:19 PM

Riskinit, on 11 March 2010 - 11:50 PM, said:

Some of the functions in the container look so useful...

Well, yeah...

But you know, since this C++, the usual thing to do, if you're writing something like a linked list, is not to just create a "head" pointer in your main function, but instead wrap that head pointer along with a constructor, destructor, and a set of useful functions to add nodes, remove nodes, search for nodes, etc, etc, just like those juicy functions you see for the STL list.

This is the structure that I make to hold information about an individual. I based this off of an example in my book but want to make sure I understand the how the pointers all work.
The line following makes a variable that holds the memory address of a studentRec.

studentRec *next_1

Next I make a constructor which contains a bunch of variables. This code

studentRec *next = NULL

Says I am making a variable called next of type studentRec that points to nothing. So when I use this constructor I enter everthing, which is automatically converted to my structure.

Where I am having a problem is with the linked list. I can read the file in correctly, but when I go to print the linked list everything is backwards. Reads the first record last and the last record first, which makes me think I am pointing everything in the wrong direction, except in my books example everything is backwards when they print their linked list. So could someone explain my errors?

I feel like I am looking at a drawing right now but not seeing what is important.

This is what I am thinking... I am creating a node with the necessary info, but the 5th entry is throwing me off. I pass studentList which points to nothing. And then I call this new node studentList. So something is out of place, but I am not sure. This dumb book does a poor job at teaching.

Re: Question on reading text file into structure.

Posted 12 March 2010 - 02:12 PM

It's called a linked list because each node is a link in a chain. The idea is that the first node in the chain points to the second which points to the third, and so on. So a list with 4 nodes looks something like this:

N0 -> N1 -> N2 -> N3 -> [NULL == 0]

Each node has a "next-pointer" that has the memory address of the next node. When you create a new node, that new node is going to be the last one in the chain (unless you're creating a sorted list, but that's a different story), so you automatically set its next-pointer to NULL to make it explicit that it doesn't point to a real memory address.

What you have been doing is first, creating a new node that initially points to NULL, but then setting the new node's next-pointer to the address in studentList, which is the "head" of the list, and finally setting studentList to the address of the new node, thus placing the new node at the head of the list.

To add a new node at the END of the chain, you first have to find the node that's currently the end of the chain and set its next-pointer to point to the new node, and leave the pointer that points to the head of the chain (in this case studentList) unchanged, except when you are creating the FIRST node. As you can see, it is a pain (and quite inefficient) to have to search through the entire list to find the end every time you want to add a node. Therefore, in addition to the pointer to the head of the list, a list class normally also has a pointer to the "tail" (the last node in the list), and every time you add a new node you would reassign the tail pointer and also the next-pointer of the node that used to be last so that BOTH of them point to the new last node.