对象的多数组表示(不一样的链表-多数组表示链表)
生活随笔
收集整理的這篇文章主要介紹了
对象的多数组表示(不一样的链表-多数组表示链表)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
對象的多數組表示
(不一樣的鏈表-多數組表示鏈表)
// // 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]<<" ";}總結
以上是生活随笔為你收集整理的对象的多数组表示(不一样的链表-多数组表示链表)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Θ(n)反转单链表(算法导论第三版第十章
- 下一篇: 对象的单数组表示(用单数组实现链表-不一