Change
This commit is contained in:
133
LinearList/arrayList/arrayList.cpp
Normal file
133
LinearList/arrayList/arrayList.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "linearList.h"
|
||||
#include "arrayList.h"
|
||||
|
||||
template <typename T>
|
||||
int search(const T& x, const T* arr, int n, int length) {
|
||||
if (n >= length)
|
||||
return -1;
|
||||
if (arr[n] == x)
|
||||
return n;
|
||||
return search(x, arr, n + 1 , length);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
arrayList<T>::arrayList(int initialCapacity) {
|
||||
if (initialCapacity < 1) {
|
||||
std::cout << "Initial Capacity Must Be > 0" << std::endl;
|
||||
//<2F><><EFBFBD><EFBFBD>throw exception
|
||||
}
|
||||
element = new T[initialCapacity];
|
||||
listSize = 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
arrayList<T>::arrayList(const arrayList<T>& theList) {
|
||||
//<2F><EFBFBD><EEBFBD>
|
||||
arrayLength = theList.arrayLength;
|
||||
listSize = theList.listSize;
|
||||
element = new T[arrayLength];
|
||||
for (int i = 0; i < listSize; i++) {
|
||||
element[i] = theList.element[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
arrayList<T>::~arrayList() {
|
||||
delete [] element;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int arrayList<T>::capacity() {
|
||||
return arrayLength;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool arrayList<T>::empty() const {
|
||||
//If empty, return true;
|
||||
if (listSize == 0) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int arrayList<T>::size() const {
|
||||
//Return Array Size
|
||||
return listSize;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T& arrayList<T>::get(int theIndex) const {
|
||||
// Return Arr[theIndex]
|
||||
if (theIndex < 0 || theIndex >= listSize) {
|
||||
throw std::out_of_range("Index out of bounds: " + std::to_string(theIndex));
|
||||
}
|
||||
return element[theIndex];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int arrayList<T>::indexOf(const T& theElement) const {
|
||||
//Return the index of "theElement" first appeared
|
||||
return search(theElement, element, 0, listSize);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T* arrayList<T>::changeLength(T* x, int oldlength, int newlength) {
|
||||
//Arr length altered
|
||||
if (newlength < 0) {
|
||||
throw std::invalid_argument("newlength must be non-negative: " + std::to_string(newlength));
|
||||
}
|
||||
T* tmp = new T[newlength];
|
||||
int min = std::min(newlength, oldlength);
|
||||
for (int i = 0; i < min; i++) {
|
||||
tmp[i] = x[i];
|
||||
}
|
||||
delete[] x;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
void arrayList<T>::erase(int theIndex) {
|
||||
//Erase Arr[theIndex] , remember to move the rest
|
||||
if (theIndex < 0 || theIndex >= listSize) {
|
||||
throw std::out_of_range("Index out of bounds: " + std::to_string(theIndex));
|
||||
}
|
||||
for (int i = theIndex; i < listSize - 1; i++) {
|
||||
element[i] = element[i + 1];
|
||||
}
|
||||
listSize--;
|
||||
element[listSize] = T();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void arrayList<T>::insert(int theIndex, const T& theElement) {
|
||||
//Insert theElement to place Arr[theIndex]
|
||||
if (theIndex < 0 || theIndex > listSize) {
|
||||
throw std::out_of_range("Index out of bounds: " + std::to_string(theIndex));
|
||||
}
|
||||
if (listSize == arrayLength) {
|
||||
element = changeLength(element, arrayLength, arrayLength + 1);
|
||||
|
||||
arrayLength++;
|
||||
}
|
||||
for (int i = listSize - 1; i >= theIndex; --i) {
|
||||
element[i + 1] = element[i];
|
||||
}
|
||||
element[theIndex] = theElement;
|
||||
listSize++;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void arrayList<T>::output() const {
|
||||
// output into ostream
|
||||
for (int i = 0; i < listSize; i++) {
|
||||
std::cout << element[i] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
template class arrayList<int>;
|
||||
template class arrayList<double>;
|
||||
template class arrayList<char>;
|
||||
|
Reference in New Issue
Block a user