The "C + +" container adapter implements various functions of queue queues (Enqueue, team out, empty, size, access all elements, etc.)

Source: Internet
Author: User
Tags assert

Adapter:


convert a generic container to another container, The so-called container, refers to the storage of data equipment, as we know the order of the table and linked list are container container. For example, we have a voltage of 220v, and like a charging line, it acts as a switch to the right voltage. And here, our main character is to transform the general-purpose list structure into a data structure that implements queue queues, which means that the list can also be used to implement other structures.


In a linear table, we know the difference between a linked list and a sequential table:


Linked list: The node is flexible, making inserting and deleting elements convenient and flexible, but for single-linked list if the node pointer _head, _tail, find the element is more troublesome, need to traverse.


Sequential tables: Open up a contiguous memory space, find elements conveniently, and access them by pointers or subscripts. Inserting or deleting elements is more complex, and inserting the elements next to each other immediately after the element is removed, and then the elements need to be moved forward.


For queuing queue model, we know that it is the tail plug Delete, FIFO features. So I chose the linked list structure for the above reasons to implement queue queues. You can refer to:


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7D/74/wKiom1bo06nSvSW8AAAYVK-E-CQ155.png "title=" ly59{ 4LO ' @HW ~dtlv (lu62w.png "alt=" Wkiom1bo06nsvsw8aaayvk-e-cq155.png "/>


The code is as follows:

#include <iostream>using namespace std; #include <assert.h>template<class T> Struct listnode{    listnode (const t& x)     :_next ( NULL)     , _prev (NULL)     , _data (x)      {}    listnode<t>* _next;    listnode<t>* _prev;     T _data;}; Template<class t>class list{public:    list ()          :_head (NULL)         , _tail (NULL)      {}    list (const list<t>& l)      {        listnode<t>* cur = l._head;         while  (cur)          {            this->pushback (cur->_ Data);            cur = cur->_next;         }    }    list<t >& operator= (const list<t>& l)     {         //Delete the node first, then insert node         if  (&s  != this)         {             ListNode<T>* pcur = _head;             while  (pcur)              {                 listnode<t>* del = pcur;                 pcur = pcur->_next;                 delete del;                 del = NULL;             }            listnode<t >* cur = _head;            while   (cur)             {                 this->pushback (Cur->_data);                 cur =  cur->_next;            }        }         return *this;    }     ~list ()//node deletion of one node     {        listnode <T>* cur = _head;        while  (cur)          {             ListNode<T>* del = cur;             cur = cur->_next;             delete del;            del =  Null;        }    }    void  pushback (CONST&NBSp T&AMP;&NBSP;X);     void popback ();     void popfront ();     void printlist ();     void reverse ();     Size_t length ();    public:    listnode<t>* _head;     listnode<t>* _tail;};/ /tail plug template<class t>void list<t>::P ushback (const t& x) {     //Analysis: There are two kinds of cases: no node, node     if  (_head == null)     {         _head = _tail = new ListNode<T> (x);    }    else    {         ListNode<T>* cur = new ListNode<T> (x);         _tail->_next = cur;        cur->_prev = _tail;         _tail = cur;        _tail->_next =  null;    }}//template<class t>void list<t>::P opback () {     //Analysis: Divided into three cases: no node, one node, multiple nodes     if  (_head == _tail)      {        if  (_head == null)          {             Return;        }        else         {             delete _head;            _head  = _tail =&nbSp Null;        }    }    else     {        listnode<t>* prev =  _tail->_prev;        delete _tail;         _tail = null;        _tail  = prev;        _tail->_next = NULL;     }}//template<class t>void list<t>::P Opfront () {     if  (_head == _tail)     {         if  (_head == null)         {             return;        }         delete _head;        _head = _tail =  NULL;    }    else    {         ListNode<T>* cur = _head;         ListNode<T>* del = _head;             _head = cur->_next;         delete del;        del = null;                 _head->_prev = NULL;     }    }//reverse Template<class t>void list<t>:: Reverse () {    //Analysis: Starting at both ends, exchanging data (odd number of data and even number of data)     listnode<t>*  begin = _head;    ListNode<T>* end = _tail;    while  (!) ( (begin == end)  | |  end->_next == begin))     {         swap (begin->_data, end->_data);         begin =  begin->_next;        end = end->_prev;     }}//length template<class t>size_t list<t>::length () {     ListNode<T>* cur = _head;    int count = 0;     while  (cur)     {         ++count;        cur = cur->_next;     }    return count;} Print template<class t>void list<t>::P rintlist () {    listnode<t>* cur = _head;    while  ( cur)     {        cout << cur->_ data <<  "--";        cur = cur->_next;     }    cout <<  "NULL" &NBSP;&LT;&LT;&NBSP;ENDL;} template<class t, template<class> class container = list>     //default form class queue{public:    //queue     void  Push (const t& x)     {        _con. Pushback (x);     }    //team     void pop ()      {        _con. Popfront ();     }    //Size     size_t size ()     {         return _con. Length ();     }    //empty     bool empty ()      {        return size ()  == 0;     }    //Team Head     t& front ()      {        size_t size = size ();         assert (size > 0);         The end of the return _con._head->_data;    }    //team      T& back ()     {        size_t size  = size ();         assert (size > 0);  &nbsP;      return _con._tail->_data;    }protected:     container<t> _con;};/ /Queue Test function void testqueue () {    queue<int, list> q1;     //queue<int> q1;    q1. Push (1);     q1. Push (2);     q1. Push (3);     q1. Push (4);     q1. Push (5);     //access all elements of the queue (the queue is the end of the header)     while  (!q1. Empty ())     {        cout<< q1. Front ()  <<  "  ";         q1. Pop ();     }}int main () {    testqueue ();     System ("pause");     return 0;}


This article is from the "C language 100-200 Prime" blog, please be sure to keep this source http://10740184.blog.51cto.com/10730184/1751682

The "C + +" container adapter implements various functions of queue queues (Enqueue, team out, empty, size, access all elements, etc.)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.