zoukankan      html  css  js  c++  java
  • C++循环链表的实现

    // .hpp文件
    #include <stdio.h>
    class NodeCList{
    public:
        NodeCList();
        static NodeCList* create(int element);
    
        int element;
        NodeCList* next;
    };
    
    class CircleList {
    public:
        CircleList();
        static CircleList* create();
        void append(NodeCList* node);
        void insertAtFirst(NodeCList* node);
        void insert(NodeCList* node, int index);
        NodeCList* find(int index);
        int find(NodeCList* node);
        void remove(int index);
        void clear();
        void printContent();
    
    public:
        NodeCList* rear;
        int length;
    };
    
    //.cpp文件
    NodeCList:: NodeCList():next(nullptr){
    }
    NodeCList* NodeCList::create(int element){
        auto node = new (std::nothrow) NodeCList();
        node->element = element;
        return node;
    }
    
    CircleList::CircleList():rear(nullptr),length(0){
    
    }
    
    CircleList* CircleList::create(){
        return new(std::nothrow) CircleList();
    }
    void CircleList::append(NodeCList *node){
        if (!node) return;
        if (!this->rear){
            this->rear = node;
            node->next = this->rear;
        } else{
            node->next = this->rear->next;
            this->rear->next = node;
            this->rear = node;
        }
    
        this->length ++;
    }
    void CircleList::insertAtFirst(NodeCList *node){
        if (this->length == 0){
            this->rear = node;
            node->next = this->rear;
        } else{
            node->next = this->rear->next;
            this->rear->next = node;
        }
        this->length ++;
    }
    
    void CircleList::insert(NodeCList *node, int index){
        if (index < 0 || index > this->length){
            CCLOG("index error.");
            return;
        }
        if (index == 0){
            this->insertAtFirst(node);
        } else if (index == this->length){
            this->append(node);
        } else{
            auto tmp = this->find(index - 1);
            node->next = tmp->next;
            tmp->next = node;
            this->length ++;
        }
    }
    
    NodeCList* CircleList::find(int index){
        if (index < 0 || index > this->length - 1){
            CCLOG("index error.");
            return nullptr;
        }
        auto temp = this->rear->next;
        for (int i = 0; i < index; i++){
            temp = temp->next;
        }
        return temp;
    }
    
    int CircleList::find(NodeCList *node){
        auto tmp = this->rear->next;
        for (int i = 0; i < this->length; i++){
            if (node->element == tmp->element){
                return i;
            }
            tmp = tmp->next;
        }
        return -1;
    }
    
    void CircleList::remove(int index){
        if (index < 0 || index > this->length - 1 || this->length == 0) {
            CCLOG("index error.");
            return;
        }
        if (this->length == 1){
            this->clear();
        }
    
        if (index == 0){
            this->rear->next = this->find(0)->next;
            this->length --;
        } else{
            this->find(index - 1)->next = this->find(index)->next;
            this->length --;
        }
    }
    
    void CircleList::printContent(){
        std::string str = "";
        auto temp = this->rear->next;
        for (int i = 0; i < this->length; i++){
            printf("第%d个为: %d", i, temp->element);
            printf("
    ");
            temp = temp->next;
        }
    }
    
    void CircleList::clear(){
        this->length = 0;
        this->rear = nullptr;
    }
  • 相关阅读:
    039、Data Volume 之 bind mount (2019-02-28 周四)
    038、Docker 的两类存储资源(2019-02-27 周三)
    037、外部网络如何访问容器 (2019-02-26 周二)
    036、容器如何访问外部世界 (2019-02-25 周一)
    035、容器间通信的三种方式(2019-02-22 周五)
    034、理解容器之间的连通性(2019-02-21 周四)
    033、如何自定义容器网络(2019-02-20 周三)
    032、学容器必须懂bridge网络(2019-02-19 周二)
    031、none和host网络的适用场景(2019-02-18 周一)
    030、实现容器的底层技术(2019-01-25 周五)
  • 原文地址:https://www.cnblogs.com/skyxu123/p/9543797.html
Copyright © 2011-2022 走看看