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;
    }
  • 相关阅读:
    获取请求IP
    Excel导入工具类兼容xls和xlsx
    Openshift 4.3环境的离线Operatorhub安装
    RHEL学习
    OpenShift Service Mesh 培训作业
    OpenId Connect认证配置
    Route Sharding in OpenShift 4.3
    OpenShift 4.3环境中创建基于Go的Operator
    Quay和Clair的集成
    Quay 基础版安装和部署
  • 原文地址:https://www.cnblogs.com/skyxu123/p/9543797.html
Copyright © 2011-2022 走看看