C + + cyclic sequential queue

Source: Internet
Author: User

As the name implies: Queues that are stored in a sequential structure are called sequential queues
The cyclic sequential queue avoids the occurrence of false overflow in the queue. As shown, there are several special cases of circular queues.

What you should be aware of when you finish your order Loop queue:
Front, rear only represents the index value in Base[i] in this sequential table, not the absolute address of the memory, so it is easier to deal with later loops.
Key to queue loops

front=(front+1)%queueSize;

The following is a personal circular queue operation engineering File:

/// /// /// /// ///  /// ///   /// ///   /// ///   /// ///   /// ///   /// ///   //////sqqueue.h  /// /// /// /// ///  /// ///   /// ///   /// ///   /// ///   /// ///   /// ///   /// #ifndef Myhead_h  #define myhead_h  #include "myhead.h"#endif #include <iomanip>////////////////////////////////////////////////////////////////////////////////Circular order queue data structure C + + class declaration (base class)Template <typename Elemtype>class sqqueue{ Public:void Clear();//Put the Loop order team emptyStatus deQueue (Elemtype & E);//OUT queueStatus enQueue (Elemtype & E);//Incoming queueStatus Getfront (Elemtype & E);//Read the elements of the queue head of the loop sequence    intGetLength ();//Find the number of elements in the Loop order team    BOOLIsEmpty ();//Determine if the Loop order team is empty    BOOLIsfull ();//Determine if the Loop order team is fullSqqueue<elemtype>operator= (sqqueue<elemtype> rightq);definition of the//overloaded assignment operator    voidDisplay ();voidRandsqueue ();//**************************** the constructor and destructor declarations are automatically called for the system below *************************//Sqqueue (intSize= -);//Constructors~sqqueue ();//destructorSqqueue (ConstSqqueue<elemtype> & OTHERQ);//Copy initialization constructorprotected:intRearintFrontintQueuesize; Elemtype *Base;};/////////////////////////////////////////////////////////////////////////////////////////////////cyclic sequential queue data structure C + + class implementation (base class)Template <typename elemtype>voidSqqueue<elemtype>::clear () {front=rear;} Template <typename elemtype>status sqqueue<elemtype>::d equeue (Elemtype & E) {if(IsEmpty ())returnERROR; E=Base[Front]; Front= (front+1)%queuesize;returnOK;} Template <typename elemtype>status sqqueue<elemtype>::enqueue (elemtype &e) {if(Isfull ())returnERROR;Base[Rear]=e; Rear= (rear+1)%queuesize;returnOK;} Template <typename elemtype>status Sqqueue<elemtype>::getfront (Elemtype & E) {if(IsEmpty ())returnERROR; E=Base[Front]returnOK;} Template <typename elemtype>intSqqueue<elemtype>::getlength () {return(rear-front+queuesize)%queuesize;} Template <typename elemtype>BOOLSqqueue<elemtype>::isempty () {returnRear==front?true:false;} Template <typename elemtype>BOOLSqqueue<elemtype>::isfull () {return(rear+1)%queuesize==front?true:false;} ///////////system constructor and destructor implementationTemplate <typename Elemtype>sqqueue<elemtype>::sqqueue (intSize) {Base=NewElemtype[size]; AssertBase!=0); Front=rear=0; Queuesize=size;} Template<typename Elemtype>sqqueue<elemtype>::~sqqueue () {delete []Base;} Template<typename Elemtype>sqqueue<elemtype>::sqqueue (Constsqqueue<elemtype>& Otherq) {Base=NewElemtype[otherq.queuesize]; AssertBase!=0);    Queuesize=otherq.queuesize;    Front=otherq.front; Rear=otherq.rear; for(inti = front;i%queuesize!=rear) {Base[I]=otherq.Base[i]; I= (i+1)%queuesize; }}template<typename elemtype>voidSqqueue<elemtype>::d isplay () {intN=getlength ();    cout<<endl; cout<<"The current queue is:"<<endl; for(inti =0; I < n; i++) {COUT&LT;&LT;SETW (6) <<Base[I+front];    } cout<<endl; COUT&LT;&LT;SETW (6) <<"↑"; for(inti =0; I < n1; i++) {COUT&LT;&LT;SETW (6) <<" "; } COUT&LT;&LT;SETW (6) <<"↑"<<endl; COUT&LT;&LT;SETW (6) <<"Front"; for(inti =0; I < n1; i++) {COUT&LT;&LT;SETW (6) <<" "; } COUT&LT;&LT;SETW (6) <<"Rear"<<endl;} Template<typename elemtype>voidSqqueue<elemtype>::randsqueue () {elemtype elem[ One]; Srand (Unsigned (Time (NULL)));intN=rand ()%Ten+1; cout<<"The resulting random array is:"<<endl; for(inti =0; I < n; i++) {Elem[i]=rand ()% -+1; COUT&LT;&LT;SETW (6) <<Elem[i]; } for(inti =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 (intARGC, _tchar* argv[]) {sqqueue<int> SQ (Ten); Sq.randsqueue ();Charyesorno=' Y ';intNum,a; Status STA; while(yesorno==' Y '|| yesorno==' y ')    {cout<<"1. Into the queue: "<<endl;cout<<"2. Out queue: "<<endl;Cin>>num;Switch(num) { Case 1:cout<<"Enter the value of the queue:"<<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<<"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 Please enter Y "<<endl;Cin>>YesOrNo; } System ("Pause");return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + cyclic sequential 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.