zoukankan      html  css  js  c++  java
  • 动态数组C++实现

    回顾大二的数据结构知识。从数组开始。实现了一个可自动扩充容量的泛型数组。

    头文件:Array.h

    #ifndef Array_hpp
    #define Array_hpp
    
    template <class T>
    class Array{
    private:
        T *base;        //数组首地址
        int length;     //数组中元素
        int size;       //数组大小,以数组中元素的大小为单位
    public:
        //初始化数组,分配内存
        bool init();
        //检查内存是否够用,不够用就增加
        bool ensureCapcity();
        //添加元素到数组尾
        bool add(T item);
        //插入元素到数组的具体位置,位置从1开始
        bool insert(int index,T item);
        //删除指定位置的元素并返回,位置从1开始
        T del(int index);
        //返回指定位置的元素
        T objectAt(int index);
        //打印数组所有元素
        void display();
    };
    
    #endif /* Array_hpp */
    
    

    实现:Array.cpp

    #include "Array.hpp"
    #include <mm_malloc.h>
    #include <iostream>
    using namespace std;
    
    template<typename T> bool Array<T>::init(){    
        base = (T *)malloc(10*sizeof(T));
        if(!base){
            return false;
        }
        size = 10;
        length = 0;
        return true;
    }
    
    template<typename T> bool Array<T>::ensureCapcity(){
        if(length >= size){
            T *newBase = (T*)realloc(base,10 * sizeof(T) + size);
            if(!newBase){
                return false;
            }
            base = newBase;
            size += 10;
            newBase = nullptr;
        }
        return true;
    }
    
    template<typename T> bool Array<T>::add(T item){
        if(!ensureCapcity()){
            return false;
        }
        T *p = base + length;
        *p = item;
        length ++;
        return true;
    }
    
    template<typename T> bool Array<T>::insert(int index,const T item){
        if(!ensureCapcity()){
            return false;
        }
        if(index < 1 || index > length){
            return false;
        }
        T *q = base + index - 1;
        T *p = base + length - 1;
        while( p >= q){
            *(p+1) = *p;
            p--;
        }
        *q = item;
        q = nullptr;
        p = nullptr;
        length ++;
        return true;
    }
    
    template<typename T>T Array<T>::del(int index){
        if(index<1 || index > length){
            return NULL;
        }
        T *q = base + index - 1;
        T item = *q;
        ++q;
        T *p = base + length;
        while(q <= p){
            *(q-1)=*q;
            ++q;
        }
        length --;
        return item;
    }
    
    template<typename T>T Array<T>::objectAt(int index){
        if(index<1 || index > length){
            return NULL;
        }
        T *q = base;
        return *(q + index - 1);
    }
    
    template <typename T>void Array<T>::display(){
        T *q = base;
        T *p = base +length - 1;
        while (q<=p) {
            cout << *(q++)<<" ";
        }
        cout << endl;
    }
    
    

    使用:

    #include <iostream>
    #include "Array.cpp"
    using namespace std;
    
    int main(int argc, const char * argv[]) {
        Array<int> array = *new Array<int>;
        array.init();
        array.add(1);
        array.insert(1,2);
        array.objectAt(1);
        return 0;
    }
    
  • 相关阅读:
    1.在html中引入js文件和Jquery框架
    在html页面添加一个隐藏域,并渲染一个需要保存的数值,在js中需要再获取,而不影响页面结构
    Django REST framework 使用简记
    BlockingQueue-线程的阻塞队列
    ExecutorService生命周期
    Java transient关键字使用小记
    数组
    Git 常用命令
    Git 常见报错
    python 常见报错
  • 原文地址:https://www.cnblogs.com/jiy-for-you/p/6892358.html
Copyright © 2011-2022 走看看