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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#ifndef ARRAYlIST_H_INCLUDED
#define ARRAYlIST_H_INCLUDED
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
template <class elemType>
class arrayListType
{
public:
bool isEmpty() const;
bool isFull() const;
int listSize()const;
int maxListSize()const;
int partition(int first, int last);
void print() const;
void insert(const elemType& insertItem);
void insertionSort();
void swap(int first, int second);
void recQuickSort(int first, int last);
void quickSort();
arrayListType(int size = 10000);
~arrayListType();
protected:
int length;
int maxSize;
elemType *list;
};
template <class elemType>
bool arrayListType<elemType>::isEmpty() const
{
return (length == 0);
}
template <class elemType>
bool arrayListType<elemType>::isFull() const
{
return (length == maxSize);
}
template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
if(size == 0)
{
cout<<"The array size must be positive"
"creating array of size 100."<<endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new elemType[maxSize];
assert(list!=NULL);
}
template <class elemType>
void arrayListType<elemType>::print() const
{
for (int i = 0; i<length; i++)
cout<<list[i]<<" ";
cout<<endl;
}
template <class elemType>
void arrayListType<elemType>::insert(const elemType& insertItem)
{
if (length==maxSize)
cout<<"Array is Full"<<endl;
else if (length == 0)
list[length++] = insertItem;
else
list[length++] = insertItem;
}
template <class elemType>
int arrayListType<elemType>::listSize()const
{
return length;
}
template <class elemType>
int arrayListType<elemType>::maxListSize()const
{
return maxSize;
}
template <class elemType>
int arrayListType<elemType>::partition(int first, int last)
{
elemType pivot;
int index, smallIndex;
swap(first, (first+last)/2);
pivot = list[first];
smallIndex = first;
for (index = first + 1; index <= last; index++)
if (list[index] < pivot)
{
smallIndex++;
swap(smallIndex, index);
}
swap(first, smallIndex);
return smallIndex;
}
template <class elemType>
arrayListType<elemType>::~arrayListType()
{
delete [] list;
}
template <class elemType>
void arrayListType<elemType>::insertionSort()
{
int firstOutOfOrder, location;
elemType temp;
for (firstOutOfOrder = 1; firstOutOfOrder < length;
firstOutOfOrder++)
if (list[firstOutOfOrder] < list[firstOutOfOrder-1])
{
temp = list[firstOutOfOrder];
location = firstOutOfOrder;
do
{
list[location] = list[location -1];
location--;
}
while (location > 0 && list[location - 1] > temp);
list[location]=temp;
}
}
template <class elemType>
void arrayListType<elemType>::swap(int first, int second)
{
elemType temp;
temp = list[first];
list[first] = list[second];
list[second] = temp;
}
template <class elemType>
void arrayListType<elemType>::recQuickSort(int first, int last)
{
int pivotLocation;
if (first<last)
{
pivotLocation = partition(first, last);
recQuickSort(first, pivotLocation - 1);
recQuickSort(pivotLocation + 1, last);
}
}
template <class elemType>
void arrayListType<elemType>::quickSort()
{
recQuickSort(0, length -1);
}
#endif
|