Queuing (queue)

Source: Internet
Author: User

    • Definition of queue--queue

The queue is only allowed at the end of the table to be inserted, and the team header in the table is deleted. The queue has a first-in, FIFO feature that is out-of-state.

    • The queue provides the following actions
     Q.empty () returns               true if the queue is empty, otherwise returns false     Q.size ()                returns the number of elements in the queue     q.pop ()                 deletes the first element of the queue but does not return its value     Q.front () Returns the value of the               first element of the team, but does not delete the element     Q.push ()                at the end of the team press the new element     Q.back ()                returns the value of the tail element of the queue, but does not delete the element
    • Implementation of the queue

Below is the source of a queue structure implemented in C + + (linked list)

1 #pragmaOnce2#include <iostream>3#include <assert.h>4 using namespacestd;5Template<typename t>6 classQueue7 {8  Public:9 Queue ()Ten :p Head (NULL) One , Ptail (NULL) A     {} -Queue (Constqueuenode<t>&q) -     { theQueuenode<t>*node =Q.phead; -          while(Node) -         { -Push (node->data); +Node = node->Next; -         } +     } A~Queue () at     { -Queuenode<t> *node =Phead; -          while(Node) -         { -Queuenode<t>*del =Node; -Node = node->Next; in             DeleteDel; -         } toPhead =NULL; +Size =0; -     } thequeuenode<t>&operator= (Constqueuenode<t>&q) *     { $         if( This! = &q)Panax Notoginseng         { -Node<t> *pnode =Other._phead; the              while(NULL! =Pnode) +             { APush (pnode->_data); thePnode = pnode->_pnext; +             } -         } $  $         return* This; -     } -     voidPush (ConstT &data) the     { -         if(Phead = =NULL)Wuyi         { thePtail = Phead =NewQueuenode<t>(data); -         } Wu         Else -         { AboutPtail->next =NewQueuenode<t>(data); $Ptail = ptail->Next; -         } -     } -     voidPop () A     { +ASSERT (NULL! =phead); the         if(Phead = =ptail) -         { $             DeletePhead; thePhead = Ptail =NULL; the         } the         Else the         { -Queuenode<t>*del =Phead; inPhead = phead->Next; the             DeleteDel; the         } About     } the     BOOLEmpty () the     { the         returnPhead = =NULL; +     } -t&fornt () the     {Bayi assert (Phead); the  the         returnPhead->data; -     } -  thet&Back () the     { the assert (Ptail); the  -         returnPtail->data; the     } the size_t size () the     {94          while(Phead! =NULL) the         { thesize++; thePhead = phead->Next;98         } About         returnsize; -     }101     voidPrint ()102     {103Queuenode<t>*cur =Phead;104          while(cur) the         {106cout << Cur->data <<" -";107Cur = cur->Next;108         }109cout <<"NULL"; the     }111 protected: thequeuenode<t>* _buynode (Constt&data)113     { the         return NewQueuenode<t>(data); the     } the protected:117queuenode<t>*Phead;118queuenode<t>*Ptail;119 size_t size; - };121 voidMain ()122 {123queue<int>Q;124Q.push (6); theQ.push (6);126Q.push (9);127 Q.pop (); - q.print ();129cout<<q.size (); thecout<<q.fornt ();131cout <<Q.back (); theSystem"Pause");133 134}

Queuing (queue)

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.