双向循环链表的C++ OOP实现

双向循环链表的C++ OOP实现

​
// 2020/02/16 Tealer.Guo
// 双向链表

# include <iostream>

template<typename T>
struct Node {
    T elements;

    Node<T>* left; // 左节点
    Node<T>* right; // 右节点

    Node() {}
    Node(const T& this_elem, Node<T>* this_left, Node<T>* this_right) {
        elements = this_elem;
        left = this_left;
        right = this_right;
    }
    Node(const T& this_elem) {
        elements = this_elem;
    }
};

template<typename T>
class linkerList {
    public:
        linkerList(); // 构造方法
        linkerList(linkerList<T>& this_node); // 复制构造方法
        int indexOf(const T& this_elem) const;
        T& get(int this_index) const;
        void insert(int this_index, const T& this_elem);
        void erase(int this_index);
        int size() const { return list_size; }
        bool empty() const { return list_size == 0; }
        void output(std::ostream& out) const;
    protected:
        void checkIndex(int this_index) const;
        Node<T>* first_node;
        Node<T>* last_node;
        int list_size;
};

template<typename T>
linkerList<T>::linkerList() {
    // 构造方法
    first_node = last_node =nullptr;
    list_size = 0;
}

template<typename T>
linkerList<T>::linkerList(linkerList<T>& this_node) {
    // 复制构造函数
    first_node = this_node.first_node;
    last_node = this_node.last_node;
    list_size = this_node.list_size;
}

template<typename T>
void linkerList<T>::checkIndex(int this_index) const {
    // 检查索引
    if(this_index < 0 || this_index > list_size) {
        std::cerr << "Out of Index!";
        exit(1);
    }
}

template<typename T>
int linkerList<T>::indexOf(const T& this_elem) const {
    // 返回索引
    Node<T>* this_node = first_node;
    int this_index = 0;
    while(this_node -> elements != this_elem) {
        this_node = this_node -> right;
        this_index ++;
    }
    return this_index;
}

template<typename T>
T& linkerList<T>::get(int this_index) const {
    // 取得元素
    checkIndex(this_index);
    if(this_index == 0) {
        return first_node -> elements;
    } else {
        // 左 -> 右
        Node<T>* this_node = first_node;
        for(int i = 0; i < this_index - 1; i++) {
            this_node = this_node -> right;
        }
        return this_node -> elements;
    }
}

template<typename T>
void linkerList<T>::insert(int this_index, const T& this_elem) {
    // 插入元素
    checkIndex(this_index);
    if(this_index == 0) {
        first_node = new Node<T>(this_elem, last_node, first_node);
        last_node = first_node;
    } else {
        // 左 -> 右
        Node<T>* this_node = first_node;
        for(int i = 0; i < list_size - 1; i ++) {
            this_node = this_node -> right;
        }
        this_node -> right = new Node<T>(this_elem, this_node, this_node -> right);
        if(this_node -> right -> right != nullptr)
            this_node -> right -> right -> left = this_node -> right;
        else
            last_node = this_node -> right;
    }
    list_size ++;
}

template<typename T>
void linkerList<T>::erase(int this_index) {
    // 删除元素
    checkIndex(this_index);
    Node<T>* this_node = first_node;
    if(this_index == 0) {
        this_node = this_node -> right;
    } else {
        // 从左向右
        for(int i = 0; i < this_index -1; i++) {
            this_node = this_node -> right;
        }
        this_node -> right = this_node -> right -> right;
    }
    list_size--;
}

template<typename T>
void linkerList<T>::output(std::ostream& out) const {
    // 输出
    Node<T>* this_node = first_node;
    for(int i = 0; i < list_size; i ++) {
        out << this_node -> elements << " ";
        this_node = this_node -> right;
    }
}

template<typename T>
std::ostream& operator<<(std::ostream& out, linkerList<T>& this_linker) {
    // 重载
    this_linker.output(out);
    return out;
}

​

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值