C ++ cyclically ordered queues and cyclically ordered queues

Source: Internet
Author: User

C ++ cyclically ordered queues and cyclically ordered queues

As the name implies: a queue stored in an ordered structure is called an ordered queue.
Cyclic ordered queues can avoid false overflow of queues. As shown in the following special cases.

Note:
Front and rear only represent the index values in the base [I] sequence table, rather than the absolute address of the memory. This makes it easier to process in subsequent loops.
Key to queue Loop

front=(front+1)%queueSize;

The following is my cyclic queue operation project file:

//////////////////////////////////////// /// // SqQueue. h /////////////////////////////////////// /// // # ifndef MYHEAD_H # define MYHEAD_H # include "myhead. h "# endif # include <iomanip> /////////////////////////////// //////////////////////////////////////// //////// cyclically ordered queue data structure C ++ class declaration (base class) template <typename ElemType> class SqQueue {public: void clear (); // round the loop Status deQueue (ElemType & e); // output queue Status enQueue (ElemType & e); // enter queue Status getFront (ElemType & e ); // read the int getLength () element of the cyclic sequence queue header; // calculates the number of elements in the cyclic sequence queue bool isEmpty (); // determine whether the cyclic sequence team is empty bool isFull (); // determine whether the cyclic sequence team is full SqQueue <ElemType> operator = (SqQueue <ElemType> rightQ ); // The definition of the overload value assignment operator void display (); void randSqueue (); // ***************************** the system automatically calls the constructor and the Structure function declaration ************************* // SqQueue (int Size = 20); // constructor ~ SqQueue (); // destructor SqQueue (const SqQueue <ElemType> & otherQ); // copy the initialization constructor protected: int rear; int front; int queueSize; elemType * base ;}; //////////////////////////////////////// //////////////////////////////////////// ////////////// C ++ class implementation of the cyclic ordered queue data structure (base class) template <typename ElemType> void SqQueue <ElemType >:: clear () {front = rear;} template <typename ElemType> Status SqQueue <ElemType >:: deQueue (ElemType & e ){ If (isEmpty () return ERROR; e = base [front]; front = (front + 1) % queueSize; return OK ;} template <typename ElemType> Status SqQueue <ElemType>: enQueue (ElemType & e) {if (isFull () return ERROR; base [rear] = e; rear = (rear + 1) % queueSize; return OK;} template <typename ElemType> Status SqQueue <ElemType >:: getFront (ElemType & e) {if (isEmpty ()) return ERROR; e = base [front] return OK;} template <typename ElemType> int SqQ Ueue <ElemType >:: getLength () {return (rear-front + queueSize) % queueSize;} template <typename ElemType> bool SqQueue <ElemType >:: isEmpty () {return rear = front? True: false;} template <typename ElemType> bool SqQueue <ElemType >:: isFull () {return (rear + 1) % queueSize = front? True: false;} // system constructor and destructor implementation template <typename ElemType> SqQueue <ElemType>: SqQueue (int size) {base = new ElemType [size]; assert (base! = 0); front = rear = 0; queueSize = size;} template <typename ElemType> SqQueue <ElemType> ::~ SqQueue () {delete [] base;} template <typename ElemType> SqQueue <ElemType>: SqQueue (const SqQueue <ElemType> & otherQ) {base = new ElemType [otherQ. queueSize]; assert (base! = 0); queueSize = otherQ. queueSize; front = otherQ. front; rear = otherQ. rear; for (int I = front; I % queueSize! = Rear) {base [I] = otherQ. base [I]; I = (I + 1) % queueSize ;}} template <typename ElemType> void SqQueue <ElemType >:: display () {int n = getLength (); cout <endl; cout <"the current queue is:" <endl; for (int I = 0; I <n; I ++) {cout <setw (6) <base [I + front];} cout <endl; cout <setw (6) <"timeout "; for (int I = 0; I <n-1; I ++) {cout <setw (6) <";}cout <setw (6) <"handle" <endl; cout <setw (6) <"front"; for (int I = 0; I <n-1; I ++) {cout <setw (6) <";}cout <setw (6) <" rear "<endl ;} template <typename ElemType> void SqQueue <ElemType>: randSqueue () {ElemType Elem [11]; srand (unsigned (time (NULL); int n = rand () % 10 + 1; the random array generated by cout <"is:" <endl; for (int I = 0; I <n; I ++) {Elem [I] = rand () % 100 + 1; cout <setw (6) <Elem [I] ;}for (int I = 0; I <n; I ++) {base [I] = Elem [I]; rear ++;} display ();}
// SqQueueTest. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include" SqQueue. h "# include <iostream> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {SqQueue <int> SQ (10); SQ. randSqueue (); char YesOrNo = 'y'; int num, a; Status sta; while (YesOrNo = 'y' | YesOrNo = 'y ') {cout <"1. queue: "<endl; cout <" 2. output queue: "<endl; cin> num; switch (num) {case 1: cout <" Input Queue value: "<endl; cin> a; sta = SQ. enQueue (a); if (sta = ERROR) cout <"the queue is full !!! "<Endl; break; case 2: sta = SQ. deQueue (a); if (sta = ERROR) cout <" the queue is empty !!! "<Endl; else cout <" the pop-up element is: "<a <endl; break; default: break;} SQ. display (); cout <"do you want to continue? Yes, enter Y "<endl; cin> YesOrNo;} system (" pause "); return 0 ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.