对象的多数组表示
(不一样的链表-多数组表示链表)
//
// Created by 许加权 on 2021/7/12.
//#ifndef C11LEARN_MOSTGROUPSREPRESENTOBJECTS_H
#define C11LEARN_MOSTGROUPSREPRESENTOBJECTS_H
template<typename T>
class MostGroupsRepresentObjects
{
private:int capacity;int *next;int *prev;T* array;int head;int tail;int size;
private:void clear();void copy(const MostGroupsRepresentObjects<T> & l);
public:MostGroupsRepresentObjects(int capacity = 50);const MostGroupsRepresentObjects & operator=(const MostGroupsRepresentObjects<T> & l);MostGroupsRepresentObjects(const MostGroupsRepresentObjects<T> & l);void add(const T key);bool remove(const T key);int search(const T key);virtual ~MostGroupsRepresentObjects();int length() const;bool full();bool empty();T & operator[](int index);T operator[](int index) const;
};
template<typename T>
MostGroupsRepresentObjects<T>::MostGroupsRepresentObjects(int capacity):capacity(capacity){if(this->capacity<50)this->capacity = 50;array = new T[capacity];next = new int[capacity];prev = new int[capacity];head = -1;tail = -1;size = 0;
}
template<typename T>
const MostGroupsRepresentObjects<T> &MostGroupsRepresentObjects<T>::operator=(const MostGroupsRepresentObjects<T> & l){if(this == &l)return *this;clear();copy(l);return *this;
}
template<typename T>
MostGroupsRepresentObjects<T>::MostGroupsRepresentObjects(const MostGroupsRepresentObjects<T> & l){copy(l);
}
template<typename T>
void MostGroupsRepresentObjects<T>::clear(){delete [] array;delete [] next;delete[] prev;array = next = prev = nullptr;head = -1;tail = -1;size = 0;
}
template<typename T>
void MostGroupsRepresentObjects<T>::copy(const MostGroupsRepresentObjects<T> & l){capacity = l.capacity;array = new T[capacity];next = new int[capacity];prev = new int[capacity];head = l.head;tail = l.tail;size = l.size;for (int i = 0; i < capacity; ++i) {array[i] = l.array[i];next[i] = l.next[i];prev[i] = l.prev[i];}
}
template<typename T>
void MostGroupsRepresentObjects<T>::add(const T key){if(full()) return;if(head == - 1){head = tail = 0;array[0] = key;next[0] = -1;prev[0] = -1;size = 1;}else{int index = -1;while (++index<capacity){if(next[index] == 0 && prev[index] == 0){break;}}next[tail] = index;prev[index] = tail;array[index] = key;tail = index;next[index] = -1;size++;}
}
template<typename T>
bool MostGroupsRepresentObjects<T>::remove(const T key){if(empty()) return false;int result = search(key);if(result == -1){return false;}else{int temp_prev = prev[result];int temp_next = next[result];if(temp_prev == -1){head = temp_next;}else{next[prev[result]] = temp_next;}if(temp_next == -1){tail = temp_prev;}else{prev[next[result]] = prev[result];}prev[result] = next[result] = 0;size--;return true;}
}
template<typename T>
int MostGroupsRepresentObjects<T>::search(const T key){int current = head;while (current!=-1 && array[current]!=key){current = next[current];}return current;
}
template<typename T>
MostGroupsRepresentObjects<T>::~MostGroupsRepresentObjects(){clear();
}
template<typename T>
int MostGroupsRepresentObjects<T>::length() const{return size;
}
template<typename T>
bool MostGroupsRepresentObjects<T>::full(){return size == capacity;
}
template<typename T>
bool MostGroupsRepresentObjects<T>::empty(){return size == 0;
}
template<typename T>
T & MostGroupsRepresentObjects<T>::operator[](int index){if(index<0 && index>=size)throw "index out of bound";int current = head;while (--index>=0){current = next[current];}return array[current];
}
template<typename T>
T MostGroupsRepresentObjects<T>::operator[](int index) const{if(index<0 && index>=size)throw "index out of bound";int current = head;while (--index>=0){current = next[current];}return array[current];
}#endif //C11LEARN_MOSTGROUPSREPRESENTOBJECTS_H
测试代码
int arr[] = {1, 4, 6, -9, 2, -5, 10, -3, -7,12};int length = sizeof(arr)/sizeof (int);MostGroupsRepresentObjects<int> mostGroupsRepresentObjects(50);for (int i = 0; i < length; ++i) {mostGroupsRepresentObjects.add(arr[i]);}for (int i = 0; i < length; ++i) {cout<<mostGroupsRepresentObjects.search(arr[i])<<" ";}cout<<endl;for (int i = 0; i < mostGroupsRepresentObjects.length(); ++i) {cout<<mostGroupsRepresentObjects[i]<<" ";}cout<<endl;mostGroupsRepresentObjects.remove(-3);mostGroupsRepresentObjects.remove(-5);for (int i = 0; i < length/2; ++i) {cout<<mostGroupsRepresentObjects.remove(arr[i])<<" ";}cout<<endl;MostGroupsRepresentObjects<int> mostGroupsRepresentObjects_b(mostGroupsRepresentObjects);for (int i = 0; i < mostGroupsRepresentObjects_b.length(); ++i) {cout<<mostGroupsRepresentObjects_b[i]<<" ";}cout<<endl;MostGroupsRepresentObjects<int> mostGroupsRepresentObjects_c;mostGroupsRepresentObjects_c = mostGroupsRepresentObjects_b;mostGroupsRepresentObjects_c[9]=220;for (int i = 0; i < mostGroupsRepresentObjects_c.length(); ++i) {cout<<mostGroupsRepresentObjects_c[i]<<" ";}