I really need help on my latest C++ assignment. The program was due a week ago and I'm still beating my head against a wall here. This is supposed to be a VERY SIMPLE linked list implementation. I have rewritten this dang thing countless times in as many different configurations as I can think of but I just can't get it. If someone could just shine a light for me I would be forever in debt.
That being said, after countless attempts I finally decided to use the code here: http://www.cprogramming.com/tutorial/lesson15.html
to start with. It works just fine without the modifications I made. The only thing I really did was move the bulk of the traversal into the ListNode::add() function. Now it goes through and places the values of the array into curNode->value, but once I move the pointer I can't access those values again. What I end up with is a 1 and a 9 printed in the terminal, it skips all the others. I guess my question is: How do I store the curNode->value values after the add() function handles them? I thought the linked list did this. Do I need another array to store the values? Sorry, I'm so confused...
I know I'm missing something very simple here, but my brain is just so burned out on this assignment I've got tunnel vision and I just can't see the forest through the trees.
The ultimate goal here is to assign doubles to the list, traverse & print the list, and, using the function isMember() (which I have not yet implemented), determine if a particular number is a member of said linked list. Oh, and to add a copy constructor, once I get this base portion complete.
Thanks in advance.:S
#include <iostream>
using namespace std;
struct ListNode {
double value;
ListNode *next;
void add( double, ListNode* );
bool isMember( double );
};
void ListNode::add( double x, ListNode *curNode ) {
curNode->next = new ListNode; // Creates a ListNode at the end of the list
curNode = curNode->next; // Points to that ListNode
curNode->next = 0; // Prevents it from going any further
curNode->value = x;
}
int main() {
const int SIZE = 5;
double testValue[SIZE] = { 1, 3, 5, 7, 9 };
double* tv = testValue;
ListNode *head;
ListNode *curNode;
head = new ListNode;
head->next = 0;
head->value = *tv;
curNode = head;
if ( curNode != 0 ) {
while ( curNode->next != 0 )
curNode = curNode->next;
}
cout << curNode->value << endl;
for( int i = 1; i < SIZE; i++ ) {
curNode->add( *( tv + i ), curNode );
}
//CURRENTLY I CANNOT MAKE THIS BLOCK WORK CORRECTLY. IT ONLY PRINTS THE FIRST AND LAST VALUES OF THE ARRAY
//ListNode::add() PERFORMS ALL OPERATIONS CORRECTLY AND I CAN PRINT AS I AM ASSIGNING THE NODE VALUES
//BUT ONCE I GET PAST THEM I CANT GET BACK THROUGHT THE LIST. IT ONLY LEAVES ME WITH THE MOST RECENT value ENTRY
//??????????????????????????????????????????????????????????????????????????????????????????????????????????????
//if ( curNode->value != 0 ) { //Makes sure there is a place to start
// while ( curNode->next != 0 ) {
// cout<< curNode->value << endl;
// curNode = curNode->next;
// }
// cout<< curNode->value << endl;;
//}
return 0;
}