@devonrevenge
Nice try but, as cire pointed out, making the assignments from
inside the while loop causes problems.
In the push_back function the only thing we are using the while loop for
is to locate the last node in the list. We can know we have reached the last node because it is the only node that has next = NULL (which signifies that there is no next node).
Hence:
1 2
|
while( conductor->link != NULL )
conductor = conductor->link;
|
Will do the job. The while loop ends when conductor->link = NULL.
conductor will be pointing to the last node following this while loop.
So, finish this function here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
node* push_back (node *conductor,int number,string words)
{
node* newnode = new list::node;
newnode->num = number;
newnode->word = words;
newnode->link = ?;// don't forget to assign this. What value belongs here?
while (conductor->link!=NULL)
{
conductor=conductor->link;
}
// assign newnode to something here
return conductor;
}
|
Then I will have another task for you.
I see that you have created a list class, defined the node structure in it, added a 2nd data member, etc...
While this
is where we are going, at this point I think it introduces unnecessary complication.
I'd prefer to keep our model of a link list simple, so we can focus on how it works.
@cire. The advice you give is good but I think that you are introducing new concepts too quickly.
Let's step back a moment, OK?
What is required at minimum to create and use a link list?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
struct node
{
int x;// the data payload
node* link;// pointer to another node
};
int main()
{
node* front = new node;
front->x = 3;
front->link = NULL;
// we have created a (1 node) list!
return 0;
}
|
We belong at baby step #4 away from this right now.
@devonrevenge. When you get that push_back() working right
then
we will look at a a way to make it more efficient. OK?
EDIT: When I look at the above code for baby step 1 I see that I missed a baby step!
I should have gone from this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
struct node
{
int x;// the data payload
node* link;// pointer to another node
};
int main()
{
node* front = new node;
front->x = 3;
front->link = NULL;
if( front != NULL )
cout << "x = " << front->x << endl;
return 0;
}
|
To this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
struct node
{
int x;// the data payload
node* link;// pointer to another node
node(): x(0), link(NULL) {}
node( int X, node* Link ): x(X), link(Link) {}
};
int main()
{
// create a one node list
node* front = new node(3, front);
// display its contents
if( front != NULL )
cout << "x = " << front->x << endl;
return 0;
}
|
To this:
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
|
struct node
{
int x;// the data payload
node* link;// pointer to another node
node(): x(0), link(NULL) {}
node( int X, node* Link ): x(X), link(Link) {}
};
int main()
{
// create a four node list
node* front = NULL;
front = new node(3, front);// we don't actually need a push_front() yet.
front = new node(4, front);// we're doing it here directly.
front = new node(5, front);
front = new node(6, front);
// we have created a (4 node) list!
// let's see it - write a displayList() if you don't want this cluttering up main()
node* iter = front;
while( iter != NULL )
{
cout << "x = " << iter->x << endl;
iter = iter->link;
}
return 0;
}
|
Actually, the next thing needed here is a cleanup() function for deleting the nodes when we are done with them.
Note: This global cleanup() function will become the basis for a list class destructor later.
EDIT2:
Notice that with the new node structure constructor, a push_front() may now be written as:
1 2 3 4
|
node* push_front( node* front, int X )
{
return new node( front, X );
}
|
It's hard to get much simpler than that!