#include<iostream>
#include<exception>
using namespace std;
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 "EEROR! BAD SIZE!.\n";
}
};
template<class T>
class List {
public:
virtual bool isEmpty()const = 0;
virtual bool isFull()const = 0;
virtual int size()const = 0;
virtual void clear() = 0;
virtual void insert(int index, const T& x) = 0;
virtual void remove(int index) = 0;
virtual T get(int index)const = 0;
virtual T search(const T& x)const = 0;
virtual ~List() = 0;
};
template<class T>
class seqList:public List {
private:
T* data;
int curLength;
int maxSize;
void resize();
public:
seqList(int initSize = 100);
~seqList() { delete[] data; }
bool isEmpty()const { return curLength == 0; }
bool isFull()const { return curLength == maxSize; }
int size()const { return curLength; }
void clear() { curLength = 0;}
void insert(int index, const T& x);
void remove(int index);
T get(int index)const { if (index < 0 || index >= curLength) throw OutOfRange; return data[index]; }
int search(const T& x);
};
template<class T>
seqList<T>::seqList(int initSize) {
if (initSize <= 0) throw BadSize();
data = new T[initSize];
maxSize = initSize;
curLength = 0;
}
template<class T>
void seqList<T>::insert(int index, const T& x) {
if (index < 0 || index > curLength) throw OutOfRange();
if (isFull()) resize();
for (int i = curLength++; i > index; --i) {
data[i] = data[i - 1];
}
data[i] = x;
}
template<class T>
void seqList<T>::remove(int index) {
if (index < 0 || index >= curLength) throw OutOfRange();
if (isEmpty()) throw OutOfRange();
T value = data[index];
for (int i = index; i < --curLength; ++i) {
data[i] = data[i + 1];
}
}
template<class T>
int seqList<T>::search(const T& x) {
for (int i = 0; i < curLength; ++i) {
if (data[i] == x) return i;
}
return -1;
}
template<class T>
void seqList<T>::resize() {
data* p = data;
data = new T[maxSize * 2];
for (int i = 0; i < maxSize; ++i) {
data[i] = p[i];
}
maxSize *= 2;
delete[] p;
}
template<class T>
class linkList:public List {
private:
struct LinkNode {
T val;
LinkNode* next;
LinkNode(const T& x, LinkNode* p = nullptr) :val(x), next(p) {}
}
LinkNode* head;
public:
linkList():head(nullptr) {}
~linkList() { clear(); }
bool isEmpty()const { return head == nullptr; }
int size()const;
void clear();
void insert(int index, const T& x);
void remove(int index);
T get(int index)const;
T search(const T& x)const;
};
template<class T>
int linkList<T>::size()const {
int cnt = 0;
LinkNode* p = head;
while (p) {
++cnt;
p = p->next;
}
return cnt;
}
template<class T>
void linkList<T>::clear() {
ListNode* del = nullptr;
while (head) {
del = head;
head = head->next;
delete del;
}
}
template<class T>
void likList<T>::insert(int index, const T& x) {
if (index < 0) throw OutOfRange();
ListNode* node = new T(x);
if (index == 0) {
node->next = head;
head = node;
return;
}
ListNode* p = head;
int i = 0;
while (p && i < index - 1) {
p = p->next;
++i;
}
if (!p) throw outOfRange();
node->next = p->next;
p->next = node;
}
template<class T>
void linkList<T>::remove(int index) {
if (index < 0 || head == nullptr) throw OutOfRange();
if (index == 0) {
linkNode* p = head;
head = head->next;
delete p;
}
int i = 0;
ListNode* p = head;
while (p && i < index - 1) {
p = p->next;
++i;
}
if(!p) return;
if (p->next) {
ListNode* q = p->next;
p->next = q->next;
delete q;
}
}
template<class T>
T linkList<T>::get(int index)const {
int i = 0;
ListNode* p = head;
while (p && i < index) {
p = p->next;
++i;
}
if (p) return p->val;
throw OutOfRange();
}
template<class T>
int linkList<T>::search(const T& x)const {
ListNode* p = head;
int i = 0;
while (p) {
if (p->val == x) return i;
++i;
p = p->next;
}
if (p) return i;
return -1;
}
【数据结构】顺序表
最新推荐文章于 2023-07-30 17:19:35 发布