C + + uses class encapsulation to implement queues

Source: Internet
Author: User

1#include <stdlib.h>2#include <iostream>3 usingstd::cout;4 usingStd::endl;5 6 #defineSUM 3//Set the size of the queue here, because the queue node is a space for malloc, and generally does not have to set the queue upper limit. But the limit is added to the test convenience.7 8typedefstructqnode{9     intdata;Ten     structQnode *Pnext; One}qnode,*Pqnode; A  - classqueue{ -     Private: the Pqnode M_front; - Pqnode m_rear; -         intM_sum;//In the queueNumber of nodes -      Public: + Queue (); -~Queue (); +         voidPushintx); A         voidpop (); at         intFront (); -         intBack (); -         BOOLempty (); -         BOOLFull (); - }; - Queue::queue () { inM_front = M_rear = (pqnode)malloc(sizeof(Qnode)); -M_front->pnext =NULL; toM_sum =0; + } -queue::~Queue () {//Empty queues the Pqnode p,q; *M_rear =M_front; $p = m_front->Pnext;Panax NotoginsengM_front->pnext =NULL; -      while(p) { theQ =p; +p = p->Pnext; A          Free(q); the     } +      Free(M_front); - } $ voidQueue::p Ush (intx) { $     if(!Full ()) { -Pqnode p = (pqnode)malloc(sizeof(Qnode)); -P->data =x; theP->pnext =NULL; -cout<<"the In queue element is"<< x <<Endl;WuyiM_rear->pnext =p; theM_rear =p; -m_sum++; Wu}Else{ -cout <<"queue is full"<<Endl; About     } $ } - voidQueue::p op () { -    -Pqnode tmp;//point to deleted node, easy to free up space & determine if the truncated point is on the tail node A     if(Empty ()) { +cout <<"The queue is empty and the team fails"<<Endl; the         return ; -     } $cout <<"the deleted queue headers are:"<< M_front->pnext->data <<Endl; theTMP = m_front->Pnext; theM_front->pnext = tmp->Pnext; the     if(M_rear = =tmp) { theM_rear =M_front; -     } in      Free(TMP); them_sum--; the } About intQueue::front () { the     if(Empty ()) { thecout <<"queue is empty"<<Endl; the         return-1;//queue is empty +}Else{ -         returnM_front->pnext->data; the     }Bayi } the intQueue::back () { the     if(Empty ()) { -cout <<"queue is empty"<<Endl; -         return-1;//queue is empty the}Else{ the         returnM_rear->data; the     } the } - BOOLQueue::empty () { the     if(M_front = =m_rear) { the         return true; the}Else94         return false; the } the BOOLQueue::full () { the     if(M_sum <SUM) {98         return false; About}Else -         return true;101 }102 103 intMain () {104 Queue que; the     intret;106 107 Que.pop ();108Que.push (1);109Que.push (2); the 111 Que.pop (); the     if(ret = Que.front ())! =-1){113cout <<"the queue header elements are:"<< ret <<Endl; the     } the     if(ret = Que.back ())! =-1){ thecout <<"the tail element of the queue is:"<< ret <<Endl;117     }118Que.push (3);119Que.push (4); -Que.push (5);121 122     if(ret = Que.front ())! =-1){123cout <<"the queue header elements are:"<< ret <<Endl;124     } the     if(ret = Que.back ())! =-1){126cout <<"the tail element of the queue is:"<< ret <<Endl;127     } - 129     return 0; the     131}

C + + uses class encapsulation to implement queues

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.