对象的单数组表示(用单数组实现链表-不一样的链表实现)

对象的单数组表示

(用单数组实现链表-不一样的链表实现)

#ifndef C11LEARN_SINGULARGROUPSREPRESENTOBJECTS_H
#define C11LEARN_SINGULARGROUPSREPRESENTOBJECTS_H
#include "cmath"
#include "../tools/tool.h"
template<typename T>
class SingularGroupsRepresentObjects
{
private:
    int head;
    int tail;
    char * buffer;
    int capacity;
    int unit;
    int size;
public:
    SingularGroupsRepresentObjects(int capacity = 50);
    virtual ~SingularGroupsRepresentObjects();
    SingularGroupsRepresentObjects(const SingularGroupsRepresentObjects<T> & l);
    const SingularGroupsRepresentObjects<T> & operator=(const SingularGroupsRepresentObjects<T> &l);
    T operator[](int index) const;
    T & operator[](int index);
    void add(T key);
    int search(const T &key);
    bool remove(const T &key);
    bool full();
    bool empty();
    int length() const;
};
template<typename T>
SingularGroupsRepresentObjects<T>::SingularGroupsRepresentObjects(int capacity):capacity(capacity){
    if(this->capacity<50) this->capacity = 50;
    unit = (sizeof (int))*2 + sizeof(T);
    buffer = new char [unit*capacity];
    memset(buffer,0,unit*capacity);
    head = tail = -1;
}
template<typename T>
SingularGroupsRepresentObjects<T>::~SingularGroupsRepresentObjects(){
    if(buffer!= nullptr)
    {
        delete[] buffer;
        buffer = nullptr;
    }
}
template<typename T>
SingularGroupsRepresentObjects<T>::SingularGroupsRepresentObjects(const SingularGroupsRepresentObjects<T> & l){
    capacity = l.capacity;
    unit = l.unit;
    buffer = new char [unit*capacity];
    head = l.head;
    tail = l.tail;
    size = l.size;
    memcpy(buffer,l.buffer,unit*capacity);
}
template<typename T>
const SingularGroupsRepresentObjects<T> & SingularGroupsRepresentObjects<T>::operator=(const SingularGroupsRepresentObjects<T> &l){
    if(this == &l)return *this;
    if(buffer!= nullptr)
    {
        delete[] buffer;
    }
    capacity = l.capacity;
    unit = l.unit;
    buffer = new char [unit*capacity];
    head = l.head;
    tail = l.tail;
    size = l.size;
    memcpy(buffer,l.buffer,unit*capacity);
    return *this;
}
template<typename T>
T SingularGroupsRepresentObjects<T>::operator[](int index) const{
    if(index<0 || index >= size)
        throw "out of bound";
    int current = index;
    while (current-->0)
    {
        current = *(int*)(buffer+current*unit+sizeof(int));
    }
    return *(T*)(buffer+current*unit+sizeof(int)*2);
}
template<typename T>
 T & SingularGroupsRepresentObjects<T>::operator[](int index){
    if(index<0 || index >= size)
        throw "out of bound";
    int current = head;
    while (index-->0)
    {
        current = *(int*)(buffer+current*unit+sizeof(int));
    }
    return *(T*)(buffer+current*unit+sizeof(int)*2);
}
template<typename T>
void SingularGroupsRepresentObjects<T>::add(T key){
    if(full())return;
    if(head == -1)
    {
        int *prev= new (buffer)int;
        *prev = -1;
        int *next = new(buffer+sizeof(int))int;
        *next = -1;
        T* t = new(buffer+sizeof(int)*2)T;
        *t=key;
        size = 1;
        head = tail = 0;
    }
    else
    {
        int index = 0;
        while (index<capacity)
        {
            if(*(int*)(buffer+index*unit) ==0
               && (*(int*)(buffer+index*unit+sizeof(int))) ==0)
                break;
            index++;
        }
        *(int*)(buffer+tail*unit+sizeof(int)) = index;
        int *prev = new (buffer+index*unit)int;
        *prev = tail;
        int *next = new (buffer+index*unit + sizeof(int))int;
        *next = -1;
        T * t = new (buffer+index*unit + sizeof(int)*2)T;
        *t = key;
        tail = index;
        size++;
    }
}
template<typename T>
int SingularGroupsRepresentObjects<T>::search(const T &key){
    if(empty()) return -1;
    int current =  head;
    while (current!=-1 && *(T*)(buffer + current * unit + sizeof (int)*2) != key)
    {
        current = *(int*)(buffer+current*unit+sizeof (int));
    }
    return current;
}
template<typename T>
bool SingularGroupsRepresentObjects<T>::remove(const T &key){
    int index = search(key);
    if(index == -1) return false;
    int prev = *(int*)(buffer+index*unit);
    int next = *(int*)(buffer + index * unit + sizeof(int));
    if(prev == -1)
    {
        head = next;
    }
    else
    {
        *(int*)(buffer + prev * unit + sizeof(int)) = next;
    }
    if(next == -1)
    {
        tail = prev;
    }
    else
    {
        *(int*)(buffer + next * unit) = prev;
    }
    memset(buffer+index*unit,0,unit);
    size--;
    return true;
}
template<typename T>
bool SingularGroupsRepresentObjects<T>::full(){
    return size == capacity;
}
template<typename T>
bool SingularGroupsRepresentObjects<T>::empty(){
    return size == 0;
}
template<typename T>
int SingularGroupsRepresentObjects<T>::length() const{
    return size;
}
#endif //C11LEARN_SINGULARGROUPSREPRESENTOBJECTS_H

测试代码

 	int arr[] = {1, 4, 6, -9, 2, -5, 10, -3, -7,12};
    int length = sizeof(arr)/sizeof (int);
    SingularGroupsRepresentObjects<int> singularGroupsRepresentObjects;
    for (int i = 0; i < length; ++i) {
        singularGroupsRepresentObjects.add(arr[i]);
    }
    for (int i = 0; i < length; ++i) {
        cout<<singularGroupsRepresentObjects.search(arr[i])<<" ";
    }
    cout<<endl;
    singularGroupsRepresentObjects[9] = 110;
    for (int i = 0; i < singularGroupsRepresentObjects.length(); ++i) {
        cout<<singularGroupsRepresentObjects[i]<<" ";
    }
    cout<<endl;
    for (int i = 0; i < length/2; ++i) {
        cout<<singularGroupsRepresentObjects.remove(arr[i])<<" ";
    }
    cout<<endl;
    for (int i = 0; i < singularGroupsRepresentObjects.length(); ++i) {
        cout<<singularGroupsRepresentObjects[i]<<" ";
    }
    cout<<endl;
    SingularGroupsRepresentObjects<int> singularGroupsRepresentObjects_b = singularGroupsRepresentObjects;
    SingularGroupsRepresentObjects<int> singularGroupsRepresentObjects_c;
    singularGroupsRepresentObjects_c = singularGroupsRepresentObjects_b;
    for (int i = 0; i < singularGroupsRepresentObjects_b.length(); ++i) {
        cout<< singularGroupsRepresentObjects_b[i]<<" ";
    }
    cout<<endl;
    for (int i = 0; i < singularGroupsRepresentObjects_c.length(); ++i) {
        cout<< singularGroupsRepresentObjects_c[i]<<" ";
    }
    for (int i = 0; i < singularGroupsRepresentObjects_c.length(); ++i) {
        singularGroupsRepresentObjects_c[i] = singularGroupsRepresentObjects_c[i] + arr[i];
    }
    cout<<endl;
    for (int i = 0; i < singularGroupsRepresentObjects_c.length(); ++i) {
        cout<< singularGroupsRepresentObjects_c[i]<<" ";
    }
    cout<<endl;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值