大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
//利用容器适配器实现栈和队列 #pragma once #include#include #include using namespace std; template struct Node { public: Node(const T& d) :_next(NULL) , _prev(NULL) ,_data(d){} T _data; Node *_next; Node *_prev; }; template class LinkList { public: LinkList() :_head(NULL) , _tail(NULL){} LinkList(const LinkList & list); LinkList & operator=(LinkList list); ~LinkList(); void PushBack(const T& d); void PushFront(const T& d); void PopBack(); void PopFront(); void Insert(Node *addr, const T& d); //在当前结点后面插入元素 Node * Search(const T& d); void Remove(const T& d); void RemoveAll(const T& d); void Sort(); void Display(); size_t Size(); T& GetFront() { assert(_head); return _head->_data; } T& GetBack() { assert(_tail); return _tail->_data; } private: Node *_head; Node *_tail; }; template size_t LinkList ::Size() { Node *cur = _head; size_t count = 0; while (cur) { count++; cur = cur->_next; } return count; } template LinkList ::LinkList(const LinkList & list) :_head(NULL) , _tail(NULL) { Node *cur = list._head; while (cur) { PushBack(cur->_data); cur = cur->_next; } } template LinkList & LinkList ::operator=(LinkList list) { std::swap(_head,list._head); std::swap(_tail,list._tail); return *this; } template LinkList ::~LinkList() { Node *cur = _head; while (cur) { _head = _head->_next; delete cur; cur = _head; } _tail = NULL; } template void LinkList ::PushBack(const T& d) { Node *NewHead = new Node (d); if (NULL == _head) { _head = NewHead; _tail = NewHead; } else { _tail->_next = NewHead; NewHead->_prev = _tail; _tail = _tail->_next; } } template void LinkList ::PushFront(const T& d) { Node *NewHead = new Node (d); if (NULL == _head) { _head = NewHead; _tail = NewHead; } else { NewHead->_next = _head; _head->_prev = NewHead; _head = NewHead; } } template void LinkList ::PopBack() { if (NULL == _head) { return; } else if (NULL==_head->_next) { delete _tail; _head = NULL; _tail = NULL; } else { Node *cur = _tail; _tail = _tail->_prev; _tail->_next = NULL; delete cur; } } template void LinkList ::PopFront() { if (NULL == _head) { return; } else if (NULL == _head->_next) { delete _tail; _head = NULL; _tail = NULL; } else { Node *cur = _head; _head = _head->_next; _head->_prev = NULL; delete cur; } } template void LinkList ::Insert(Node *addr, const T& d) //在当前结点后面插入元素 { Node *NewNode = new Node (d); if (_head == addr) { NewNode->_next = _head; _head->_prev = NewNode; _head = NewNode; } else if (_tail == addr) { PushBack(d); } else { NewNode->_next = addr->_next; addr->_next->_prev = NewNode; addr->_next = NewNode; NewNode->_prev = addr; } } template Node * LinkList ::Search(const T& d) { Node *cur = _head; while (cur) { if (cur->_data == d) { return cur; } cur = cur->_next; } return NULL; } template void LinkList ::Remove(const T& d) { Node *cur =Search(d); if (cur == _head) { PopFront(); } else if (cur == _tail) { PopBack(); } else { cur->_prev->_next = cur->_next; cur->_next->_prev = cur->_prev; delete cur; } } template void LinkList ::RemoveAll(const T& d) { while (Search(d) != NULL) { Remove(d); } } template void LinkList ::Sort() { Node *end = _tail; while (_head != end) { Node *cur = _head; int flag = 1; while (cur!=end) { if (cur->_data > cur->_next->_data) { T tmp = cur->_data; cur->_data = cur->_next->_data; cur->_next->_data=tmp; flag = 0; } cur = cur->_next; } if (flag) { return; } end = end->_prev; } } template void LinkList ::Display() { Node *cur = _head; while (cur) { cout << cur->_data << " "; cur = cur->_next; } cout << endl; }
创新互联是一家集网站建设,清远企业网站建设,清远品牌网站建设,网站定制,清远网站建设报价,网络营销,网络优化,清远网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
#include"LinkList.h" templateclass Container> class Queue { public: void Push(const T& d); void Pop(); T& Front(); T& Back(); size_t Size(); bool Empty(); void Display() { while (!Empty()) { cout << Front() << " "; Pop(); } cout << endl; } private: Container _con; }; template class Container> void Queue ::Push(const T& d) { _con.PushBack(d); } template class Container> void Queue ::Pop() { _con.PopFront(); } template class Container> T& Queue ::Front() { return _con.GetFront(); } template class Container> T& Queue ::Back() { return _con.GetBack(); } template class Container> size_t Queue ::Size() { return _con.Size(); } template class Container> bool Queue ::Empty() { return _con.Size()== 0; }
#pragma once #include#include #include #include using namespace std; template class Seqlist { public: Seqlist(); Seqlist(const Seqlist & seq); Seqlist & operator=(Seqlist seq); ~Seqlist(); void PushBack(const T& d); void PushFront(const T& d); void PopBack(); void PopFront(); void Insert(int index,const T& d); int Search(const T& d); void Remove(const T& d); void RemoveAll(const T& d); void Sort(); void Reserve(int n); void Display(); int GetSize(); T& operator[](int index); private: void CheckCapacity(int n=0); private: T *_pdata; int _sz; int _capacity; }; template T& Seqlist ::operator[](int index) { assert(index >= 0); assert(index < _sz); return _pdata[index]; } template int Seqlist ::GetSize() { return _sz; } template Seqlist ::Seqlist() :_sz(0) , _capacity(0) , _pdata(NULL){} template Seqlist ::Seqlist(const Seqlist & seq) { _pdata = new T[seq._capacity]; for (int i = 0; i < seq._sz; i++) { _pdata[i] = seq._pdata[i]; } _sz = seq._sz; _capacity = seq._capacity; } template Seqlist & Seqlist ::operator=(Seqlist seq) { swap(_pdata,seq._pdata); _sz = seq._sz; _capacity = seq._capacity; return *this; } template Seqlist ::~Seqlist() { if (_pdata != NULL) { delete[] _pdata; _pdata = NULL; _sz = 0; _capacity = 0; } } template void Seqlist ::CheckCapacity(int n=0) { if (_sz == _capacity||n>_capacity) { int NewCapacity =2*_capacity+1; if (n > _capacity) { NewCapacity = n; } T* tmp = new T[NewCapacity]; for (int i = 0; i < _sz; i++) { tmp[i] =_pdata[i]; } delete[] _pdata; _pdata = tmp; _capacity = NewCapacity; } } template void Seqlist ::PushBack(const T& d) { CheckCapacity(); _pdata[_sz++] = d; } template void Seqlist ::PushFront(const T& d) { CheckCapacity(); //memmove(_pdata + 1,_pdata,sizeof(T)*_sz); for (int i =_sz;i>0; i--) { _pdata[i] = _pdata[i-1]; } _pdata[0] = d; _sz++; } template void Seqlist ::PopBack() { _sz--; } template void Seqlist ::PopFront() { //memmove(_pdata,_pdata+1,sizeof(T)*(--_sz)); for (int i = 0; i<_sz-1; i++) { _pdata[i] = _pdata[i+1]; } _sz--; } template void Seqlist ::Insert(int index, const T& d) { assert(index >= 0); assert(index<_sz); CheckCapacity(); //memmove(_pdata+index+1,_pdata+index,sizeof(T)*(_sz-index)); for (int i = _sz; i>index; i--) { _pdata[i] = _pdata[i - 1]; } _sz++; _pdata[index] = d; } template int Seqlist ::Search(const T& d) { int i = 0; for (i = 0; i < _sz; i++) { if (_pdata[i] == d) { return i; } } return -1; //没找到返回-1 } template void Seqlist ::Remove(const T& d) { int index = Search(d); if (index == -1) { return; } else { //memmove(_pdata+index,_pdata+index+1,sizeof(T)*(_sz-index-1)); for (int i =index; i<_sz-1; i++) { _pdata[i] = _pdata[i+1]; } _sz--; } } template void Seqlist ::RemoveAll(const T& d) { while (Search(d) != -1) { Remove(d); } } template void Seqlist ::Reserve(int n) { CheckCapacity(n); } template void Seqlist ::Sort() { int end = _sz - 1; for (int i = 0; i < _sz - 1; i++) { int flag = 1; int k = end; for (int j = 0; j _pdata[j+1]) { T tmp = _pdata[j]; _pdata[j] = _pdata[j + 1]; _pdata[j + 1] = tmp; flag = 0; k= j; } } if (flag == 1) { return; } end = k; } } template void Seqlist ::Display() { for (int i = 0; i < _sz; i++) { cout << _pdata[i] << " "; } cout << endl; }
#include"Seqlist.h" templateclass Container> class Stack { public: bool Empty(); void Push(const T& d); void Pop(); T& Top(); int Size(); void Display() { while (!Empty()) { cout << Top() << " "; Pop(); } cout << endl; } private: Container _con; }; template class Container> bool Stack ::Empty() { return _con.GetSize()==0; } template class Container> void Stack ::Pop() { _con.PopBack(); } template class Container> void Stack ::Push(const T& d) { _con.PushBack(d); } template class Container> int Stack ::Size() { return _con.GetSize(); } template class Container> T& Stack ::Top() { int sz=_con.GetSize()-1; return _con[sz]; }