OK. It seems like you are getting close to seeing what the link member is for.
You have it working in your 4th previous post right?
The one that starts with:
this made my compiler flip out... |
Your observation that all lastthings should be bases was spot on, but you only changed it where the assignments are made, not where the cout-ing is being done.
You have asked:
...how could i dynamicly create nodes and iterate through them...
|
Doing this would be the logical next step.
I'll try guiding you here.
Let's start with the simplest function I can think of for adding nodes. Lets call this function
push_front because we will be using it to push new nodes onto the front of the list. Each new node becomes the first in the list. The node which was first becomes second (ie newFirst->link = oldFirst) and so on.
Here's some code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include <string>
using namespace std;
struct node
{
string word;
node* link;
};
// returns a pointer to the new front of the list
node* push_front( node* front, string Word )
{
node* temp = new node;// this will become the new front node (1st in the list)
temp->word = Word;// copy the data over
temp->link = front;// the new node points to the old front node
return temp;// new front node
}
int main()
{
node* base = NULL;// this will ALWAYS point to the FIRST node in the list
// (or = NULL if there are none)
// add a few nodes to the list
base = push_front(base, "first word");
base = push_front(base, "second word");
base = push_front(base, "third word");
// display the list contents
cout << base->word << endl;
cout << base->link->word << endl;
cout << base->link->link->word << endl;
cout << endl;
return 0;
}
|
The only thing new to you here should be the push_front(). Play around with this code and ask if you don't see how the function works.
I'll save a function to iterate through the list and display the words for next time (ie, if we don't get stuck here - at push_front ).