Initialization of sequential queues and operation of queues are saved in queue.h
#ifndef Queue_h#define QUEUE_HTemplate<classT>classqueue{ Public: Queue (intqueuecapacity);BOOLIsempty ();voidFront ();voidRear ();voidPush (T item);voidPop ();PrivateTQueue;intFrontintRearintcapacity;};//using constructors to initialize sequential queuesTemplate<classT>queue<t>::queue (intqueuecapacity) {if(queuecapacity<1) {Throw "The capacity of the queue must be >0"; }Else{Queue=NewT[queuecapacity]; capacity=queuecapacity; Front=rear=0;//Sacrifice front This position, this position does not put elements}}//element into queueTemplate<classT>voidQueue<t>::P ush (T item) {if((rear+1) {%capacity==front) {Throw "The queue is full"; }Else{rear= (rear+1)%capacity;Queue[Rear]=item; }}//DequeueTemplate<classT>voidQueue<t>::P op () {if(Isempty ())Throw "The queue is empty"; Front= (front+1)%capacity;}//Determine if the queue is emptyTemplate<classT>inline BOOLQueue<t>::isempty () {returnFront==rear; }//Team first elementTemplate<classT>inline voidQueue<t>::front () {if(! Isempty ()) {cout<<"Team first element is"<<Queue[(front+1)%capacity]<<endl; }Else{cout<<"The queue is empty"<<endl; }}//Team tail elementsTemplate<classT>inline voidQueue<t>::rear () {if(! Isempty ()) {cout<<"Team tail element is"<<Queue[rear]<<endl; }Else{cout<<"The queue is empty"<<endl; }}#endif
Main function
#include "queue.h"#include <iostream>Using namespaceSTD;int main () {queue<int> Q (Ten);Q. Push(1);Q. Push(2);Q. Push(3);Q. Front();Q. Rear();Q. Pop();Q. Pop();Q. Pop();Q. Front();Q. Rear();Q. Push(4);Q. Front();Q. Rear();System"Pause");Return0;}
Result diagram
C + + implementation of sequential queues