c++__ Loop Queue (practice)

Source: Internet
Author: User
Tags stub

Loop queue

Queue.h

#ifndef Queue_h_#defineQueue_h_#defineSIZE 10typedefintdata_type;enumQueue_op {queue_err= -1, QUEUE_OK, queue_empty};classQUEUE {Private: Data_type*data; intcount; intIfront; intirear; Public: QUEUE (); ~QUEUE (); intGetCount ()Const; Data_type getData (unsignedintIConst; Data_type*getdata ()Const; intGetfront ()Const; intGetrear ()Const; voidSetCount (intcount); voidSetData (data_type data, unsignedinti); voidSetData (Data_type *data); voidSetfront (intfront); voidSetrear (intrear);//QUEUE *create ();//void Destroy (QUEUE *pqueue);    intEnQueue (QUEUE *Pqueue, data_type tdata); intDeQueue (QUEUE *pqueue, data_type *pData); intIsEmpty (QUEUE *pqueue); void operator Delete(void*pqueue);};#endif/* Queue_h_ */

Queue.cpp

#include"QUEUE.h"#include<iostream>using namespacestd; Queue::queue () {//TODO auto-generated Constructor stubcout<<"Queue"<<Endl;  This->setcount (0);  This->setfront (0);  This->setrear (0); int*data =NewData_type[size]; if(!data) {cout<<"new Data[size] Error"<<Endl;  This-SetData (NULL); } Else {         This->setdata (NewData_type[size]); }}queue::~QUEUE () {//TODO auto-generated destructor stubcout<<"~queue"<<Endl;}intQueue::getcount ()Const {    returncount;} Data_type queue::getdata (unsignedintIConst {    returndata[i];} Data_type*queue::getdata ()Const {    returndata;}intQueue::getfront ()Const {    returnIfront;}intQueue::getrear ()Const {    returnirear;}voidQueue::setcount (intcount) {     This->count =count;}voidQueue::setdata (data_type data, unsignedinti) { This->data[i] =data;}voidQueue::setdata (Data_type *data) {     This->data =data;}voidQueue::setfront (intfront) {Ifront=Front;}voidQueue::setrear (intrear) {Irear=Rear;}intQueue::enqueue (QUEUE *Pqueue, Data_type tdata) {    if(!pqueue)returnQueue_err; if(SIZE = = pqueue->GetCount ()) {cout<<"queue en over"<<Endl; returnQueue_err; } pqueue->setdata (Tdata, pqueue->getrear ()); Pqueue->setrear (Pqueue->getrear () +1); if(SIZE = = pqueue->getrear ()) Pqueue->setrear (0); Pqueue->setcount (Pqueue->getcount () +1); returnQueue_ok;}intQueue::D equeue (Queue *pqueue, data_type *pData) {    if((!pqueue) | | (!pData)) returnQueue_err; if(0= = Pqueue->GetCount ()) {cout<<"Queue de over"<<Endl; returnQueue_err; }    *pdata = Pqueue->getdata (pqueue->Getfront ()); Pqueue->setfront (Pqueue->getfront () +1); if(SIZE = = pqueue->Getfront ()) Pqueue->setfront (0); Pqueue->setcount (Pqueue->getcount ()-1); returnQueue_ok;}intQueue::isempty (QUEUE *pqueue) {    if(!pqueue)returnQueue_err; if(0= = Pqueue->GetCount ())returnQueue_empty; returnQueue_ok;}voidQUEUE::operator Delete(void*pqueue) {    if(!pqueue)return; Data_type*tmp = ((QUEUE *) pqueue)GetData ();  Free(TMP);  Free(Pqueue); return;}

Main.cpp

#include"queue/queue.h"#include<iostream>using namespacestd;voidfunction () {QUEUE*pqueue =NewQUEUE; if(!pqueue) {cout<<"New Queue Error"<<Endl; return; }    inti =5; Data_type Data=0;  while(i--) {Pqueue-EnQueue (Pqueue, i); Pqueue->dequeue (Pqueue, &data); cout<< Data <<"  "; }    if(QUEUE_OK = = pqueue->IsEmpty (pqueue)) cout<<"Queue OK"<<Endl; /*i = 9;     while (i--) {pqueue->dequeue (pqueue, &data);     cout << data << "";     } cout << Endl;     if (Queue_empty = = Pqueue->isempty (pqueue)) cout << "QUEUE EMPTY" << Endl; */    DeletePqueue;}intMain () {function (); return 0;}

c++__ Loop Queue (practice)

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.