The idea of using two stacks to simulate a queue is "pour water thought", where we use a custom type to simulate the Line table, then use the linear table to do the container to implement the data structure of the stack, and finally use the stack to implement the queue, the code is as follows:
#include <iostream> #include <string> #include <cassert>struct __truetype//type extraction {bool Get () {return true;}}; Struct __falsetype{bool get () {return false;}}; template <class _tp>struct typetraits{typedef __falsetype __ Ispodtype;}; template <>struct typetraits< bool>{typedef __truetype __IsPODType;}; template <>struct typetraits< char>{typedef __truetype __IsPODType;}; Template <>struct typetraits< unsigned char >{typedef __truetype __IsPODType;}; template <>struct typetraits< short>{typedef __truetype __IsPODType;}; Template <>struct typetraits< unsigned short >{typedef __truetype __IsPODType;};template <>struct typetraits< int>{typedef __truetype __IsPODType;}; Template <>struct typetraits< unsigned int >{typedef __truetype __IsPODType;}; template <>struct typetraits< long>{typedef __truetype __IsPODType;}; Template <>struct typetraits< unsigned long >{typedef __truetype __IsPODType;}; template <>struct typetraits< long long >{typedef __truetype __IsPODType;}; Template <>struct typetraits< unsigned long long>{typedef __truetype __IsPODType;}; template <>struct typetraits< float>{typedef __truetype __IsPODType;}; template&Nbsp;<>struct typetraits< double>{typedef __truetype _ _ispodtype;}; template <>struct typetraits< long double >{typedef __truetype __IsPODType;}; template <class _tp>struct typetraits< _tp*>{typedef __truetype __IsPODType;}; template <class t>//custom type Implementation linear Table Class seqlist{public:seqlist (): _size (0), _capacity (10), _ Array (new t[_capacity]) {memset (_array, 0, sizeof (T) *_capacity);} Seqlist (const t &x): _size (1), _capacity (Ten), _array (New t[_capacity]) {_array[0] = x ;} Seqlist (const seqlist & x) {_array = new t[x._size];my_memcpy (_array, x. _array, sizeof (T) *x._size); _capacity = x._size;_size = _capacity;} Void pushback (const t & x) {_checkcapacity (); _array[_size++]&nbsP;= x;} Void pushfront (const t & x) {_checkcapacity ();for (size_t i = _size ; i > 1; i--) {_array[_size] = _array[_size - 1];} _size++;_array[0] = x;} Void popback () {_size--;} Void popfront () {assert (_size);for (size_t i = 0; i < _size - 1; i++) {_array[i] = _array[i + 1];} _size--;} Size_t size () {return _size;} seqlist & operator = (seqlist l) {swap (_array, l._array); Swap (_size, l._size); swap (_capacity, l._capacity); return *this;} ~seqlist () {if (_array) {Delete[] _array;}} t& operator [] (const size_t t) {return _array[t];} Private:void _checkcapacity () {if (_size >= _capacity) {_capacity *= 3; t * tmp = new t[_capacity];memcpy (tmp, _array, sizeof (T)*_capacity);d elete[] _array;_array = tmp;}} void my_memcpy (t* dst, const t* src, size_t size) {if (TypeTraits <t>::__ispodtype (). Get ()) {memcpy (dst, src, size*sizeof (T));} else{for (size_t i = 0; i < size; ++i) {dst[i] = src[i];}}} size_t _size;size_t _capacity; t *_array;}; template <class t,typename contianer = seqlist<t> >//Adapter Implementation Stack Class stack{public:void push (const t & x) {_con. Pushback (x);} Void pop () {_con. Popback ();} Size_t size () {Return _con. Size ();} Bool empty () {return size () == 0;} T&top () {return _con[size () - 1];} protected:contianer _con;}; template <class t,typename container = stack<t> >//the stack as the adapter, implementing the Queue class queue{public:bool empty () {return (_instack.empty () &nbsP;&& _outstack (). Empty ());} Size_t size () {return _instack.size () + _outstack.size ();} Void push (const t &x) {_instack.push (x);} Void pop () {size_t movecount = _instack.size () - 1;for (size_t icount = movecount; icount > 0; --icount) {t temp = _instack.top (); _ Outstack.push (temp); _instack.pop ();} _instack.pop ();while (False == _outstack.empty ()) {t temp = _outstack.top (); _ Instack.push (temp); _outstack.pop ();}} T& front () {return _instack.top ();} T& back () {size_t movecount = _instack.size () - 1;for (size_t Icount = movecount; icount > 0; --icount) {T temp = _ Instack.top (); _outstack.push (temp); _instack.pop ();} T ret = _instack.top ();while (False == _outstack.empty ()) {T temp = _outstack.toP (); _instack.push (temp); _outstack.pop ();} Return ret;} Void printqueue () {size_t movecount = _instack.size ();for (size_t iCount = movecount; icount > 0; --icount) {t temp = _instack.top (); _ Outstack.push (temp); _instack.pop ();} while (False == _outstack.empty ()) {t temp = _outstack.top (); _InStack.Push (temp); cout << "<-" << temp;_outstack.pop ();} Cout << endl;} private:container _instack;container _outstack;};
The test cases are as follows:
void Test () {queue<int> q1;q1. Push (1); Q1. Push (2); Q1. Push (3); Q1. Push (4); Q1. Push (5); Q1. Push (6); Q1. PrintQueue (); Q1. Pop (); Q1. PrintQueue ();}
If there is any shortage or doubt, I hope to advise
This article is from the "Pawnsir It Road" blog, so be sure to keep this source http://10743407.blog.51cto.com/10733407/1761921
"Dry" container adapter implements two stack emulation queues