C++中实现java的方法(三)

C++中实现java的方法(三)-----List集合包

本阶段所有数据结构类的包已经开发完成,按照java中的特点
全部存在List.h类里面
整个包下载地址:https://download.csdn.net/download/u013441283/12186068
该文件下属内容如下

#ifndef CAVA_LIST_H
#define CAVA_LIST_H
#include "ArrayList.cpp"
#include "LinkedList.cpp"
#include<map>
#endif //CAVA_LIST_H

数据类型类里面,本次主要是更新了LinkedList,即双向链表类
相关解释如下:

LinkedList(链表)开发(基本完成,进入debug阶段):
在主方法里面加入
#include”LinkedList.cpp”
调用时
LinkedList <泛型名>  自己的命名;
已经实现的方法:
size()获取当前链表长度
getFirst()获取头节点
getLast()获取尾节点
removeFirst()移除头节点
removeLast()移除尾节点
addFirst()插入头节点
addLast()插入尾节点
add(Object o) 向链表插入1个元素于尾节点
remove(object o)删除一个链表中的内容
clear()清空链表
indexOf(object o)返回第一个检测到的o的位置,如果没有就返回-1
element()返回本链表
toArray()将链表变成数组,以Object*返回
clone()复制链表,以LinkedList返回
set(int index,Object element)改变在index位置的数据,返回原有的数据
还有个别方法,基本来源于java原生方法,可参考java原生手册

修改:删去了cava.h文件,由util.h代替
同时本次新增了扩充方法

Stringmethod.h

相关LinkedList源码如下:

/*
 *按照java原生LinkedList编写
 * 开心编写于2020年2月
 * 使用方法参考开发日志
 * this method is from java original method called LinkedList
 * made by happy on 2020
 * please read the CAVA development logs
 * version 1.0
 * @email :1013214271@qq.com
 */
#include<iostream>
#include<string>
#include <sstream>
using namespace std;

template <class Object>
class LinkedList{
private:
    void IndexOutOfBoundsException(){
        cout << "IndexOutOfBoundsException";
        exit(1);
    }
    void NoSuchElementException(){
        cout << "NoSuchElementException";
        exit(1);
    }
    struct Node{
    public:
        Object element=NULL;
        Node* previous= nullptr;
        Node* next=nullptr;
        void set(Node* previous,Object value,Node* next){
            this->element = value;
            this->previous = previous;
            this->next = next;
        }
    };
    int length=0;
    Node *first= nullptr;
    Node *last=nullptr;
    template<typename T> string TtoString(const T& t){
        ostringstream oss;
        oss<<t;
        return oss.str();
    }
    void linkLast(Object o){
        Node* l=this->last;
        Node* newNode=new Node();
        newNode->set(l,o,new Node());
        this->last=newNode;
        if(l==nullptr){
            this->first=newNode;
        }
        else{
            l->next=newNode;
        }
        length++;
    }
    void linkBefore(Object e,Node* succ){
            Node* pred=succ->previous;
            Node* newNode=new Node();
            newNode->set(pred,e,succ);
            succ->previous=newNode;
            if(pred==nullptr){
                this->first=newNode;
            }
            else{
                pred->next=newNode;
            }
            length++;
    }
    Node* node(int index){
        Node* x;
        int i;
        if(index<this->length>>1){
            x=this->first;
        for(i=0;i<index;++i){
            x=x->next;
        }
        return x;
        }
        else{
            x=this->last;
            for(i=this->length-1;i>index;--i){
                x=x->previous;
            }
            return x;
        }
    }
    bool isElementIndex(int index){
        return index >= 0 && index < this->length;
    }
    void chackElementIndex(int index){
        if(!this->isElementIndex(index)){
            IndexOutOfBoundsException();
        }
    }
    void linkFirst(Object o){
        Node* f=first;
        Node* newNode=new Node();
        newNode->set(NULL,o,f);
        first=newNode;
        if(f==nullptr){
            this->last=newNode;
        }
        else{
            f->previous=newNode;
        }
        length++;
    }
   Object unlinkFirst(Node* f){
       Object element=f->element;
       Node* next=f->next;
       f->next=nullptr;
       f->element=NULL;
       first=next;
       if(next==nullptr){
           last=nullptr;
       }
       else{
           next->previous=nullptr;
       }
       length--;
       return element;
   }
    Object unlinkLast(Node* l){
        Object element=l->element;
        Node* prev=l->previous;
        l->previous=nullptr;
        l->element=NULL;
        last=prev;
        if(prev==nullptr){
            first=nullptr;
        }
        else{
            prev->next=nullptr;
        }
        length--;
        return element;
    }
    Object unlink(Node* x){
        Object element=x->element;
        Node* next=x->next;
        Node* prev=x->previous;
        if(next==nullptr){
            last=prev;
        }else{
            next->previous=prev;
            x->next=nullptr;
        }
        if (prev==nullptr){
            first=next;
        }else{
            prev->next=next;
            x->previous=nullptr;
        }
        x->element=NULL;
        length--;
        return element;
    }
public:
    int indexOf(Object o) {
        int index = 0;
        Node* x;
        if (o == NULL) {
            for(x = this->first; x != nullptr; x = x->next) {
                if (x->element == NULL) {
                    return index;
                }
                ++index;
            }
        } else {
            for(x = this->first; x != nullptr; x = x->next) {
                if (o==x->element) {
                    return index;
                }

                ++index;
            }
        }

        return -1;
    }
    Object element(){
        return this->getFirst();
    }
    Object getFirst(){
        const Node* f=first;
        if (f==nullptr){NoSuchElementException();}
        return f->element;
    }
    Object getLast(){
        const Node* l=last;
        if(l==nullptr){NoSuchElementException();}
        return l->element;
    }
    Object removeFirst(){
        const Node* f=first;
        if(f==nullptr){NoSuchElementException();}
        return unlinkFirst(f);
    }
    Object removeLast(){
        const Node* l=last;
        if(l==nullptr){NoSuchElementException();}
        return unlinkLast(l);
    }
    void addFirst(Object e){
        linkFirst(e);
    }
    void addLast(Object e){
        linkLast(e);
    }
    bool contains(Object o){
        return indexOf(o)!=-1;
    }
    void add(int index,Object o){
        if(index>length||index<0){
            IndexOutOfBoundsException();
        }
        else if(index==length){
            this->linkLast(o);
        }
        else{
            this->linkBefore(o,this->node(index));
        }
    }
    Object remove(){
        return this->removeFirst();
    }
    Object remove(int index){
        this->chackElementIndex(index);
        return this->unlink(this->node(index));
    }
    template<class T>
    bool remove(T o){
        Node* x;
        if(o==NULL){
            for(x = this->first; x != nullptr; x = x->next) {
                if (x->element==NULL){
                    this->unlink(x);
                    return true;
                }
            }
        }else{
            for(x = this->first; x != nullptr; x = x->next) {
                if(o==x->element){
                    this->unlink(x);
                    return true;
                }
            }
        }
        return false;
    }
    void add(Object o){
        this->linkLast(o);
    }
    Object* toArray(){
        Object* result=new Object[length];
        int i=0;
        for(Node* x=this->first;x!=nullptr;x=x->next){
            result[i++]=x->element;
        }
        return result;
    }
    LinkedList<Object> clone(){
        LinkedList<Object> clone;
        clone.first=clone.last=nullptr;
        clone.length=0;
        for(Node* x=this->first;x!=nullptr;x=x->next){
            clone.add(x->element);
        }
        return clone;
    }
    void clear(){
        Node* next;
        for(Node* x=this->first;x!=nullptr;x=next){
            next=x->next;
            x->element=NULL;
            x->next=nullptr;
            x->previous=nullptr;
        }
        this->first=this->last=nullptr;
        this->length=0;
    }
    int size() {return this->length;}
    Object get(int index){
        this->chackElementIndex(index);
        return this->node(index)->element;
    }
    Object set(int index,Object element){
        this->chackElementIndex(index);
        Node* x=this->node(index);
        Object oldElement=x->element;
        x->element=element;
        return oldElement;
    }
    string toString(){
        if (length==0)
        {
            string s2;
            s2="[]";
            return s2;
        }
        else
        {
            string s;
            s.append("[");
            int i=0;
            while(true){
                s.append(TtoString(get(i)));
                i++;
                if(i==length){
                    s.append("]");
                    return s;
                }
                s.append(", ");
            }
        }
    }
    Object pop(){
        return this->removeFirst();
    }
    void push(Object e){
        this->addFirst(e);
    }
    bool removeFirstOccurrence(Object o) {
        return this->remove(o);
    }
    bool removeLastOccurrence(Object o) {
        Node* x;
        if (o == NULL) {
            for(x = this->last; x != nullptr; x = x->prev) {
                if (x->element == NULL) {
                    this->unlink(x);
                    return true;
                }
            }
        } else {
            for(x = this->last; x != nullptr; x = x->prev) {
                if (o==x->element) {
                    this->unlink(x);
                    return true;
                }
            }
        }

        return false;
    }

};

由于HashMap在C++中有Map方法完全可以代替,所以在List类中以原生Map方法代替
目前来说整个CAVA中的开发数据结构部分已经完成
由于我已经开学,所以接下来更新速度将会放缓
按照现在进度,即将更新更多的方法

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值