This commit is contained in:
e2hang
2025-07-19 21:34:14 +08:00
parent 91e6520894
commit ce3f273487
5 changed files with 20 additions and 0 deletions

View 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>;

View File

@@ -0,0 +1,28 @@
#ifndef ARRAY_LIST
#define ARRAY_LIST
#include <iostream>
#include "linearList.h"
template <typename T>
class arrayList : public linearList<T> {
public:
arrayList(int initialCapacity = 10);
arrayList(const arrayList<T>& x);
~arrayList();
int capacity();
bool empty() const;//If empty, return 0;
int size() const; //Return Array Size
virtual T& get(int theIndex) const override; // Return Arr[theIndex]
virtual int indexOf(const T& theElement) const override; //Return the index of "theElement" first appeared
virtual void erase(int theIndex) override; //Erase Arr[theIndex]
virtual T* changeLength(T* x, int oldlength, int newlength) override;
virtual void insert(int theIndex, const T& theElement) override; //Insert theElement to place Arr[theIndex]
virtual void output() const override; //cout
private:
T* element;
int arrayLength;
int listSize;
};
#endif

View File

@@ -0,0 +1,20 @@
#ifndef LINEAR_LIST
#define LINEAR_LIST
#include <iostream>
template <typename T>
class linearList {
public:
virtual ~linearList() {};
virtual bool empty() const = 0; //If empty, return 0;
virtual int size() const = 0; //Return Array Size
virtual T& get(int theIndex) const = 0; // Return Arr[theIndex]
virtual int indexOf(const T& theElement) const = 0; //Return the index of "theElement" first appeared
virtual void erase(int theIndex) = 0; //Erase Arr[theIndex]
virtual T* changeLength(T* x, int oldlength, int newlength) = 0;
virtual void insert(int theIndex, const T& theElement) = 0; //Insert theElement to place Arr[theIndex]
virtual void output() const = 0; //cout
};
#endif

View File

@@ -0,0 +1,49 @@
#include <iostream>
#include "linearList.h"
#include "arrayList.h"
/*
NEEDED TEST:
ok int capacity();
ok bool empty() const;//If empty, return 0;
ok int size() const; //Return Array Size
ok virtual T& get(int theIndex) const override; // Return Arr[theIndex]
virtual int indexOf(const T & theElement) const override; //Return the index of "theElement" first appeared
ok virtual void erase(int theIndex) override; //Erase Arr[theIndex]
ok virtual T* changeLength(T * x, int oldlength, int newlength) override;
ok virtual void insert(int theIndex, const T & theElement) override; //Insert theElement to place Arr[theIndex]
ok virtual void output(std::ostream & out) const override; //cout
*/
int main() {
arrayList<int> test(10);
std::cout << "ArrayCapacity: " << test.capacity() << std::endl;
std::cout << "Is Empty: " << test.empty() << std::endl;
for (int i = 0; i < 10; i++) {
test.insert(i, i + 1);
}
test.output();
std::cout << test.get(4) << std::endl ; //output 5
test.insert(10, 11); // 1 2 3 4 5 6 7 8 9 10 11
test.output();
std::cout << test.size() << std::endl;
test.erase(0); // 2 3 4 5 6 7 8 9 10 11
test.output();
test.insert(5, 3);
test.output();
std::cout << test.indexOf(3) << std::endl;
return 0;
}
/*
Output is down below
ArrayCapacity: 0
Is Empty: 1
1 2 3 4 5 6 7 8 9 10
5
1 2 3 4 5 6 7 8 9 10 11
11
2 3 4 5 6 7 8 9 10 11
2 3 4 5 6 3 7 8 9 10 11
1
*/