【数据结构】顺序表

#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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值