#include<iostream>
using namespace std;
template <typename T>
class List {
public:
virtual void clear() = 0;
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual void insert(int i, const T& value) = 0;
virtual void remove(int i) = 0;
virtual int search(const T& value) const = 0;
virtual void traverse() const = 0;
virtual void inverse() = 0;
virtual ~List() {};
};
class outOfRange :public exception {
public:
const char* what() const throw() {
return "ERROR! OUT OF RANGE.\n";
}
};
class badSize :public exception {
public:
const char* what() const throw() {
return "ERROR! BAD SIZE.\n";
}
};
template <typename elemType>
class seqList : public List<elemType> {
private:
elemType* data;
int curLength;
int maxSize;
void resize();
public:
seqList(int initSize = 10);
seqList(seqList& s1);
~seqList() { delete[] data; }
void clear() { curLength = 0; }
bool empty() const { return curLength == 0; }
int size() const { return curLength; }
void traverse() const;
void inverse();
void insert(int i, const elemType& value);
void remove(int i);
int search(const elemType& value) const;
bool Union(seqList<elemType> & B);
};
template <typename elemType>
seqList<elemType>::seqList(int initSize) {
if (initSize <= 0) throw badSize();
maxSize = initSize;
data = new elemType[maxSize];
curLength = 0;
}
template <typename elemType>
seqList<elemType>::seqList(seqList & s1) {
maxSize = s1.maxSize;
curLength = s1.curLength;
data = new elemType[maxSize];
for (int i = 0; i < curLength; i++) {
data[i] = s1.data[i];
}
}
template <typename elemType>
void seqList<elemType>::traverse() const{
if (empty()) cout << "is empty!" << endl;
else {
for (int i = 0; i < curLength; i++) {
cout << data[i] << " ";
}
cout << endl;
}
}
template <typename elemType>
int seqList<elemType>::search(const elemType& value) const {
for (int i = 0; i < curLength; i++) {
if (value == data[i]) return i;
}
return -1;
}
template <class elemType>
void seqList<elemType>::insert(int i, const elemType& value) {
if (i<0 || i>curLength) throw outOfRange();
if (curLength == maxSize) resize();
for (int j = curLength; j > i; j--) {
data[j] = data[j - 1];
}
data[i] = value;
++curLength;
}
template <typename elemType>
void seqList<elemType>::remove(int i) {
if (i<0 || i>curLength) throw outOfRange();
for (int j = i; j < curLength-1; j++) {
data[j] = data[j+1];
}
--curLength;
}
template <typename elemType>
void seqList<elemType>::inverse() {
elemType temp;
for (int i = 0; i < curLength / 2; i++) {
temp = data[i];
data[i] = data[curLength - i - 1];
data[curLength - i - 1] = temp;
}
}
template <typename elemType>
void seqList<elemType>::resize() {
elemType* p = data;
maxSize *= 2;
data = new elemType[maxSize];
for (int i = 0; i < curLength; i++) {
data[i] = p[i];
}
delete[] p;
}
template <typename elemType>
bool seqList<elemType>::Union(seqList<elemType> &B) {
int m, n, k, i, j;
m = this->curLength;
n = B.curLength;
k = m + n - 1;
i = m - 1;
j = n - 1;
if (m + n > this->maxSize) {
resize();
}
while (i >= 0 && j >= 0)
if (data[i] >= B.data[j]) {
data[k--] = data[i--];
}
else {
data[k--] = B.data[j--];
}
while (j >= 0)
data[k--] = B.data[j--];
curLength = m + n;
return true;
}
int main() {
seqList<int> *sq = new seqList<int>(10);
for (int i = 0; i < 5; i++) {
sq->insert(i, i + 10);
}
cout << "线性表遍历: ";
sq->traverse();
cout << "线性表长度: ";
cout << sq->size();
cout << "\n查询指定元素值的位序: ";
int num=sq->search(10);
cout << num;
cout << "\n移除指定位序处的值: ";
sq->remove(2);
sq->traverse();
cout << "合并两n个线性表: ";
seqList<int> *B = new seqList<int>(10);
for (int i = 0; i < 5;i++) {
B->insert(i, 10 + i);
}
sq->Union(*B);
sq->traverse();
}