#include <iostream>
#include <cassert>
usingnamespace std;
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template<class Type>
class arrayListType
{
public:
void print() const;
void insertAt(int location, const Type& insertItem);
arrayListType(int size = 100);
arrayListType(const arrayListType<Type>& otherList);
protected:
Type *list;
int length;
int maxSize;
nodeType<Type> *first; //pointer to the first node of the list
nodeType<Type> *last; //pointer to the last node of the list
};
template<class Type>
class orderedArrayListType: public arrayListType<Type>
{
public:
int binarySearch(const Type& item) const;
void selectionSort();
orderedArrayListType(int size = 100);
void mergeSort();
private:
void divideList(nodeType<Type>* first1, nodeType<Type>* &first2);
nodeType<Type>* mergeList(nodeType<Type>* first1, nodeType<Type>* first2);
void recMergeSort(nodeType<Type>* &head);
};
template <class Type>
void arrayListType<Type>::print() const
{
int i;
for (i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}
template <class Type>
void arrayListType<Type>::insertAt(int location, const Type& insertItem)
{
int i;
if (location < 0 || location >= maxSize)
cout << "The position of the item to be inserted "
<< "is out of range." << endl;
elseif (length >= maxSize) //list is full
cout << "Cannot insert in a full list" << endl;
else
{
for (i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem; //insert the item at the
//specified position
length++; //increment the length
}
} //end insertAt
template <class Type>
arrayListType<Type>::arrayListType(int size)
{
if (size <= 0)
{
cout << "The array size must be positive. Creating "
<< "an array of size 100. " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new Type[maxSize];
assert(list != NULL);
}
//*************************************************************//
template<class Type>
int orderedArrayListType<Type>::binarySearch(const Type& item) const
{
int first = 0;
int last = length - 1;
int mid;
bool found = false;
while (first <= last && !found)
{
mid = (first + last) / 2;
if (list[mid] == item)
found = true;
elseif (list[mid] > item)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
elsereturn -1;
}// end binarySearch
template<class Type>
void orderedArrayListType<Type>::selectionSort()
{
int loc, minIndex;
for (loc = 0; loc < length; loc++)
{
minIndex = minLocation(loc, length - 1);
swap(loc, minIndex);
}
}
template<class Type>
orderedArrayListType<Type>::orderedArrayListType(int size): arrayListType<Type>(size)
{
}
template<class Type>
void orderedArrayListType<Type>::mergeSort()
{
recMergeSort(first);
}
template<class Type>
void orderedArrayListType<Type>::divideList(nodeType<Type>* first1, nodeType<Type>* &first2)
{
nodeType<Type>* middle;
nodeType<Type>* current;
if (first1 == NULL) //list is empty
first2 = NULL;
elseif(first1->link == NULL) //list has only one node
first2 = NULL;
else
{
middle = first1;
current = first1->link;
if (current != NULL) //list has more that two nodes
current = current->link;
while (current != NULL)
{
middle = middle->link;
current = current->link;
if (current != NULL)
current = current->link;
} //end while
first2 = middle->link; //first2 points to the first
//node of the second sublist
middle->link = NULL; //set the link of the last node
//of the first sublist to NULL
} //end else
} //end divide
template<class Type>
nodeType<Type>* orderedArrayListType<Type>::mergeList (nodeType<Type>* first1, nodeType<Type>* first2)
{
nodeType<Type> *lastSmall; //pointer to the last node of
//the merged list
nodeType<Type> *newHead; //pointer to the merged list
if (first1 == NULL) //the first sublist is empty
return first2;
elseif (first2 == NULL) //the second sublist is empty
return first1;
else
{
if (first1->info < first2->info) //compare the first nodes
{
newHead = first1;
first1 = first1->link;
lastSmall = newHead;
}
else
{
newHead = first2;
first2 = first2->link;
lastSmall = newHead;
}
while (first1 != NULL && first2 != NULL)
{
if( first1->info < first2->info)
{
lastSmall->link = first1;
lastSmall = lastSmall->link;
first1 = first1->link;
}
else
{
lastSmall->link = first2;
lastSmall = lastSmall->link;
first2 = first2->link;
}
} //end while
if (first1 == NULL) //first sublist is exhausted first
lastSmall->link = first2;
else //second sublist is exhausted first
lastSmall->link = first1;
return newHead;
}
}//end mergeList
template<class Type>
void orderedArrayListType<Type>::recMergeSort(nodeType<Type>* &head)
{
nodeType<Type> *otherHead;
if (head != NULL) //if the list is not empty
if(head->link != NULL) //if the list has more than one node
{
divideList(head, otherHead);
recMergeSort(head);
recMergeSort(otherHead);
head = mergeList(head, otherHead);
}
} //end recMergeSort
My issue is when I go to compile it, I get an error code Unhandled exception at 0x00ec2680. 0xC0000005: Access violation reading location 0xccccccd0.. When I went to step through the program, the error pops up when I reach intList.mergeSort(); in the .cpp file and if(head->link) in void orderedArrayListType<Type>::recMergeSort(nodeType<Type>* &head). I feel like something is not putting a value into link which causes the error. Am I correct on this part? I feel like it's missing something like
1 2 3
newNode->info = newItem; //store the new item in the node
newNode->link = first; //insert newNode before first
first = newNode;