Hello this is my first post.
I am making a linked list. The problem is that whenever I try to retrieve a value I get the value from the first node.
LinkedList.cpp
this is the file I used to test itCode:#include <iostream> using namespace std; /*constructor*/ List::List():head(NULL),length(0)//sets first heads pointer to null { //remeber that head is not a node and does not contain any data //it simply points to the current first node //thus after the constructor is called, there trully still is no link list } /*destructor*/ List::~List() { eraseAll(); } void List::eraseAll() { while(head != NULL) { NodePtr temp = head; head = head -> next;//the node pointed to by head, the value of next (which is an adress) delete temp; }//heads pointer is now null } bool List::isEmpty() const { return head == NULL;//if head is null then the link list was just made, or deleteAll was just called or the list was lost (memorey leek) } unsigned int List::size() const { int count = 0;//this means the first element (list) is at 0 NodePtr toEnd = head; while(toEnd->next != NULL) { toEnd = toEnd->next; count++; } return count; } /*returns true upon sucess, otherwise false*/ bool List::insert(DataType item, unsigned int position) { if(position > length) return false;//couldn't be inserted (would cause empty spaces in list) if(position == 0 && head == NULL) { cout<<"inisde insert"<<endl; head = new Node(item); cout<<"this is head "<<head<<endl; return true; } int count = 0; NodePtr temp = head; NodePtr previous; while(count != position) { previous = temp; temp = temp->next; count++; }//the new node should now be inserted after previous, and be followed by temp previous = new Node(item); previous->next->next = temp; length++; return true; } DataType List::get(unsigned int position) const { NodePtr temp = head; if(head->next == NULL)//head's next is always null for some reason cout<<"head's screwed up"<<endl; unsigned int count = 0; cout<<"position: "<<position<<endl; while(count < position && temp->next != NULL) { count++; temp = temp->next; } cout<<"element from temp "<<temp->element<<endl; return temp->element;//problem occures here }
tester.cpp
for some reason head is always pointing to null. This confuses me because the only place I assign a variable the value of NULL is in the constructor.Code:#include "LinkedList.h" #include <iostream> using namespace std; int main() { List aList = List(); aList.insert(4.2, 0); aList.insert(22, 1); aList.insert(5,3); cout<<aList.get(3)<<endl; return 0; }
actually I miss-spoke. Head is not pointing to null the first time I call the insert method, but it is pointing to null whenever I call the get method.
here it is again slimmed down:
tester.cppCode:#include <iostream> using namespace std; List::List():head(NULL),length(0) { } /*returns true upon sucess, otherwise false*/ bool List::insert(DataType item, unsigned int position) { if(position > length) return false; if(position == 0 && head == NULL)//it appears head is always true { head = new Node(item); return true; } int count = 0; NodePtr temp = head; NodePtr previous; while(count != position) { previous = temp; temp = temp->next; count++; } previous = new Node(item); previous->next->next = temp; length++; return true; } DataType List::get(unsigned int position) const { NodePtr temp = head; unsigned int count = 0; while(count < position && temp->next != NULL)//this condition is never true { count++; temp = temp->next; } return temp->element;//problem occures here }
LinkedList.hCode:#include "LinkedList.h" #include <iostream> using namespace std; int main() { List aList = List(); aList.insert(4.5, 0); aList.insert(22, 1); aList.insert(5,3); cout<<aList.get(3)<<endl; return 0; }
Code:#include <cstdlib> typedef double DataType; // our list is zero based, that is, the first element in the list is in position 0. class List { public: List(); List(const List & rhs); List& operator=(const List & rhs); bool insert (DataType item, unsigned int position); DataType get(unsigned int position) const; #ifndef NDEBUG #endif private: struct Node { DataType element; Node* next; Node(DataType item):element(item),next(NULL){} }; typedef List::Node * NodePtr; NodePtr head;//private field unsigned int length; }; #include "LinkedList.cpp"



LinkBack URL
About LinkBacks



