//利用容器适配器实现栈和队列 #pragma once #include<iostream> #include<string> #include<cassert> using namespace std; template<typename T> struct Node { public: Node(const T& d) :_next(NULL) , _prev(NULL) ,_data(d){} T _data; Node<T> *_next; Node<T> *_prev; }; template<typename T> class LinkList { public: LinkList() :_head(NULL) , _tail(NULL){} LinkList(const LinkList<T>& list); LinkList<T>& operator=(LinkList<T> list); ~LinkList(); void PushBack(const T& d); void PushFront(const T& d); void PopBack(); void PopFront(); void Insert(Node<T> *addr, const T& d); //在当前结点后面插入元素 Node<T>* 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<T> *_head; Node<T> *_tail; }; template<typename T> size_t LinkList<T>::Size() { Node<T> *cur = _head; size_t count = 0; while (cur) { count++; cur = cur->_next; } return count; } template<typename T> LinkList<T>::LinkList(const LinkList<T>& list) :_head(NULL) , _tail(NULL) { Node<T> *cur = list._head; while (cur) { PushBack(cur->_data); cur = cur->_next; } } template<typename T> LinkList<T>& LinkList<T>::operator=(LinkList<T> list) { std::swap(_head,list._head); std::swap(_tail,list._tail); return *this; } template<typename T> LinkList<T>::~LinkList() { Node<T> *cur = _head; while (cur) { _head = _head->_next; delete cur; cur = _head; } _tail = NULL; } template<typename T> void LinkList<T>::PushBack(const T& d) { Node<T> *NewHead = new Node<T>(d); if (NULL == _head) { _head = NewHead; _tail = NewHead; } else { _tail->_next = NewHead; NewHead->_prev = _tail; _tail = _tail->_next; } } template<typename T> void LinkList<T>::PushFront(const T& d) { Node<T> *NewHead = new Node<T>(d); if (NULL == _head) { _head = NewHead; _tail = NewHead; } else { NewHead->_next = _head; _head->_prev = NewHead; _head = NewHead; } } template<typename T> void LinkList<T>::PopBack() { if (NULL == _head) { return; } else if (NULL==_head->_next) { delete _tail; _head = NULL; _tail = NULL; } else { Node<T> *cur = _tail; _tail = _tail->_prev; _tail->_next = NULL; delete cur; } } template<typename T> void LinkList<T>::PopFront() { if (NULL == _head) { return; } else if (NULL == _head->_next) { delete _tail; _head = NULL; _tail = NULL; } else { Node<T> *cur = _head; _head = _head->_next; _head->_prev = NULL; delete cur; } } template<typename T> void LinkList<T>::Insert(Node<T> *addr, const T& d) //在当前结点后面插入元素 { Node<T> *NewNode = new Node<T>(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<typename T> Node<T>* LinkList<T>::Search(const T& d) { Node<T> *cur = _head; while (cur) { if (cur->_data == d) { return cur; } cur = cur->_next; } return NULL; } template<typename T> void LinkList<T>::Remove(const T& d) { Node<T> *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<typename T> void LinkList<T>::RemoveAll(const T& d) { while (Search(d) != NULL) { Remove(d); } } template<typename T> void LinkList<T>::Sort() { Node<T> *end = _tail; while (_head != end) { Node<T> *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<typename T> void LinkList<T>::Display() { Node<T> *cur = _head; while (cur) { cout << cur->_data << " "; cur = cur->_next; } cout << endl; }
#include"LinkList.h" template<typename T,template<class> class 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<T> _con; }; template<typename T,template<class> class Container> void Queue<T,Container>::Push(const T& d) { _con.PushBack(d); } template<typename T,template<class> class Container> void Queue<T,Container>::Pop() { _con.PopFront(); } template<typename T,template<class> class Container> T& Queue<T,Container>::Front() { return _con.GetFront(); } template<typename T,template<class> class Container> T& Queue<T,Container>::Back() { return _con.GetBack(); } template<typename T,template<class> class Container> size_t Queue<T,Container>::Size() { return _con.Size(); } template<typename T,template<class> class Container> bool Queue<T,Container>::Empty() { return _con.Size()== 0; }
#pragma once #include<iostream> #include<cstring> #include<string> #include<cassert> using namespace std; template<typename T> class Seqlist { public: Seqlist(); Seqlist(const Seqlist<T>& seq); Seqlist & operator=(Seqlist<T> 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<typename T> T& Seqlist<T>::operator[](int index) { assert(index >= 0); assert(index < _sz); return _pdata[index]; } template<typename T> int Seqlist<T>::GetSize() { return _sz; } template<typename T> Seqlist<T>::Seqlist() :_sz(0) , _capacity(0) , _pdata(NULL){} template<typename T> Seqlist<T>::Seqlist(const Seqlist<T>& 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<typename T> Seqlist<T>& Seqlist<T>::operator=(Seqlist<T> seq) { swap(_pdata,seq._pdata); _sz = seq._sz; _capacity = seq._capacity; return *this; } template<typename T> Seqlist<T>::~Seqlist() { if (_pdata != NULL) { delete[] _pdata; _pdata = NULL; _sz = 0; _capacity = 0; } } template<typename T> void Seqlist<T>::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<typename T> void Seqlist<T>::PushBack(const T& d) { CheckCapacity(); _pdata[_sz++] = d; } template<typename T> void Seqlist<T>::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<typename T> void Seqlist<T>::PopBack() { _sz--; } template<typename T> void Seqlist<T>::PopFront() { //memmove(_pdata,_pdata+1,sizeof(T)*(--_sz)); for (int i = 0; i<_sz-1; i++) { _pdata[i] = _pdata[i+1]; } _sz--; } template<typename T> void Seqlist<T>::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<typename T> int Seqlist<T>::Search(const T& d) { int i = 0; for (i = 0; i < _sz; i++) { if (_pdata[i] == d) { return i; } } return -1; //没找到返回-1 } template<typename T> void Seqlist<T>::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<typename T> void Seqlist<T>::RemoveAll(const T& d) { while (Search(d) != -1) { Remove(d); } } template<typename T> void Seqlist<T>::Reserve(int n) { CheckCapacity(n); } template<typename T> void Seqlist<T>::Sort() { int end = _sz - 1; for (int i = 0; i < _sz - 1; i++) { int flag = 1; int k = end; for (int j = 0; j <end;j++) { if (_pdata[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<typename T> void Seqlist<T>::Display() { for (int i = 0; i < _sz; i++) { cout << _pdata[i] << " "; } cout << endl; }
#include"Seqlist.h" template<typename T,template<class> class 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<T> _con; }; template<typename T, template<class> class Container> bool Stack<T,Container>::Empty() { return _con.GetSize()==0; } template<typename T, template<class> class Container> void Stack<T, Container>::Pop() { _con.PopBack(); } template<typename T, template<class> class Container> void Stack<T,Container>::Push(const T& d) { _con.PushBack(d); } template<typename T, template<class> class Container> int Stack<T,Container>::Size() { return _con.GetSize(); } template<typename T, template<class> class Container> T& Stack<T, Container>::Top() { int sz=_con.GetSize()-1; return _con[sz]; }
本文出自 “11132019” 博客,转载请与作者联系!
原文地址:http://11142019.blog.51cto.com/11132019/1846850