void MergeSorter::sortTester() {
int testNumber; // number of items in the list
Node<int>* head = new Node<int>(); // the head of the list
cout << "Enter number of sorts to use ";
cin >> testNumber;
head = buildIntegerChain(head, testNumber); //call to method that builds the list
Node<int>* curr = head; //used to iterate through to display the items in the list
//loop prints out the items
for(int i = 1; i <= testNumber; i++) {
cout << "\n" << curr->getItem() << "\n";
curr = curr->getNext();
}
//method call to sort list
head = mergeSortLinkedList(head, testNumber);
}
Node<int>* MergeSorter::buildIntegerChain(Node<int>* chainHead, long nodeCount) {
if(chainHead == NULL) { //if there aren't any items yet
int num = rand(); //generate a random number
chainHead -> setItem(num); //set it in the head of the list
} else {
for(int i = 1; i <= nodeCount; i++) { //count from 1 to the amount of nodes
int num = rand(); //generate a random number
Node<int>* curr = new Node<int>(); //create new node
curr -> setItem(num); //put random number in it
curr -> setNext(chainHead); //add node to the front of the list
chainHead = curr; //move head reference
} //end for
} //end if
return chainHead;
}
//=========================================================================================
//=========================================================================================
Node<int>* MergeSorter::mergeSortLinkedList(Node<int>* listHead, int size) {
if(size <= 1) //if there is at most one item in the list
return listHead; //return it
Node<int>* result = new Node<int>(); //node to store the new list
Node<int>* current = listHead; //two nodes used to iterate
Node<int>* nextNode = listHead; //through the list
//loop iterates to the middle of the list
for(int i = 1; i <= size/2; i++) {
current = nextNode; //makes sure current stays
nextNode = nextNode -> getNext(); //one node behind nextNode
} //end for
current -> setNext(NULL); //set current's reference to NULL to split lists
listHead = mergeSortLinkedList(listHead, size/2); //recursively split
nextNode = mergeSortLinkedList(nextNode, (size - size/2)); //both lists
result = mergeTwoLists(listHead, nextNode); //merges two lists back together
return result; //return sorted list
}
//=========================================================================================
//=========================================================================================
Node<int>* MergeSorter::mergeTwoLists(Node<int>* listOne, Node<int>* listTwo) {
if(listOne == NULL) //if listOne has nothing, return listTwo
return listTwo;
if(listTwo == NULL) //if listTwo has nothing, return listOne
return listOne;
Node<int>* curr = listOne; //used to iterate through listOne and to be compared to listTwo
Node<int>* prev = listOne; //used to keep track of the Node before the one being compared
Node<int>* curr2 = listTwo; //used to keep references in listTwo when it needs to move
while(listOne != NULL && listTwo != NULL) { //while both lists contain items
if(curr -> getItem() < listTwo -> getItem()) { //if the item in listOne is less then the item in list Two
prev = curr; //move previous ahead
curr = curr -> getNext(); //move current ahead
} else { //otherwise
prev -> setNext(listTwo); //set listTwo between previous and current
curr2 = curr2 -> getNext(); //get a reference to the next node in list two
listTwo -> setNext(curr); //listTwo points to the node currently being compared
prev = listTwo; //move the reference of previous to the newly added node
listTwo = curr2; //move listTwo's reference back into place
} //end if
} //end while
return listOne; //return sorted listOne
}
When I run it, I get this error
First-chance exception at 0x011d35f6 in Lab06.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x011d35f6 in Lab06.exe: 0xC0000005: Access violation reading location 0x00000000.
The program '[8648] Lab06.exe: Native' has exited with code -1073741819 (0xc0000005).
and the compiler points me to the getItem() method of the node class. I need help figuring out why, my assignment is already late. All help is greatly appreciated!