自调整表(self-adjusting list)的实现
Posted lhb666aboluo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自调整表(self-adjusting list)的实现相关的知识,希望对你有一定的参考价值。
自调整表:所有的插入操作都发生在表的前端。
find操作:当一个元素由find访问的时候,该元素就被移到表的前端,而其他元素的相对顺序保持不变。
以下展示自调整表的数组实现和链表实现。
数组实现:
#include <iostream> template <typename Object> class salist { private: Object* elem; int theSize; public: salist():elem{nullptr},theSize{0}{} salist(int n) { elem=new Object[n]; theSize=n; } ~salist() { delete[] elem; } salist(const salist& rhs) { elem = new Object[rhs.theSize]; theSize=rhs.theSize; for(int i=0;i!=theSize;++i) elem[i]=rhs.elem[i]; } const salist& operator=(const salist& rhs) { if(this==&rhs) return *this; delete[] elem; *this(rhs); return *this; } void push(const Object& x) { Object* oldElem = elem; ++theSize; elem = new Object[theSize]; elem[0]=x; for(int i = 1;i!=theSize;++i) elem[i]=oldElem[i-1]; delete[] oldElem; } void print() { for(int i=0;i!=theSize;++i) std::cout << elem[i] << ‘ ‘; std::cout << ‘ ‘; } void find(const Object& x) { int pos=-1; for(int i=0;i!=theSize;++i) if(elem[i]==x) pos = i; if(pos==-1) std::cout << "no element with this value "; else { for(int i=pos;i!=0;--i) elem[i]=elem[i-1]; } elem[0]=x; } }; int main() { salist<int> sl; sl.push(1); sl.push(2); sl.push(3); sl.print(); sl.find(1); sl.print(); salist<int> sl2(sl); sl2.print(); return 0; }
链表实现:
#include <iostream> template <typename Object> class salist { private: struct Node { Object data; Node* prev; Node* next; Node(const Object& d=Object(),Node* p=nullptr,Node* n=nullptr):data(d),prev(p),next(n){} }; Node* head; Node* tail; init() { head = new Node; tail = new Node; head->next=tail; tail->prev=head; } public: salist() { init(); } ~salist() { clear(); delete head; delete tail; } salist(const salist& rhs) { init(); Node* ptr=rhs.head->next; while(ptr!=rhs.tail) { push_back(ptr->data); ptr=ptr->next; } } const salist& operator=(const salist& rhs) { if(this==&rhs) return *this; clear(); Node* ptr=rhs.head->next; while(ptr!=rhs.tail) { push_back(ptr->data); ptr=ptr->next; } return *this; } salist(const std::initializer_list<Object>& rhs) { init(); for(auto x : rhs) push_back(x); } void push(const Object& x) { head->next=head->next->prev=new Node(x,head,head->next); } void push_back(const Object& x) { tail->prev=tail->prev->next=new Node(x,tail->prev,tail); } void clear() { Node* ptr = head->next; while(ptr!=tail) { head->next=ptr->next; delete ptr; ptr=head->next; } tail->prev=head; } void find(const Object& x) { Node* ptr=head->next; while(ptr!=tail) { if(ptr->data==x) { ptr->prev->next=ptr->next; ptr->next->prev=ptr->prev; push(x); break; } ptr=ptr->next; } if(ptr==tail) std::cout << "no element with this value "; else delete ptr; } void print() { Node* ptr = head->next; while(ptr!=tail) { std::cout << ptr->data << ‘ ‘; ptr=ptr->next; } std::cout << ‘ ‘; } }; int main() { salist<int> sl; sl.push(2); sl.push(5); sl.push(6); sl.print(); salist<int> sl2=sl; sl2.find(2); sl2.print(); salist<int> sl3={1,2,3}; sl3.print(); return 0; }
以上是关于自调整表(self-adjusting list)的实现的主要内容,如果未能解决你的问题,请参考以下文章
去除List列表中反复值(稍作调整,也适合于List<T> 和 List<?>)
md-grid-list 响应属性在调整浏览器窗口大小时不起作用