Queue everyone seen a lot, the form is also relatively simple, is a special list, its enqueue, dequeue operation equivalent to the list of addlast, Removefirst operation. For the implementation of the linked list, you can view my other blog post-"linkedlist--linked list". The following is only a slightly more complex scenario-the loop queue.
Loop queue is to use a loop array to implement the queue, there is a problem to solve: in the normal non-circular queue, rear = = Front the queue is empty queue, but in the loop queue, this may mean that the queue is empty, it may also mean that the team is full. Two strategies are commonly used to address this problem.
1. Set an identity bit to distinguish whether the queue is empty or full. It can be said that when initializing the front = rear = 0,flag = 0 means null. By enqueue the queue operation makes front = rear, which is considered to be full state, flag = 1; by dequeue the team operation makes front = rear, which is considered to be empty, flag = 0.
2. Use less than one element space, with the Convention "the queue header pointer at the next position in the tail pointer of the queue (the next position in the loop state)" As the identification of the decision queue full.
The policy 1 code looks like this:
#ifndef _cirqueue_h#define _cirqueue_h#include <iostream> #include <stdexcept> using namespace std;// MAXN space, all spaces can be const int MAXN = 5;template<typename T>class cirqueue{private:bool isEmpty; T array[maxn];int Front,rear;public:cirqueue (); Cirqueue (t *list,int SIZE); void Enque (T e); T deque (); int getsize (); void print ();}; Template<typename T>cirqueue<t>::cirqueue () {front = rear = 0;isempty = 1;} Template<typename T>cirqueue<t>::cirqueue (T *list, int SIZE) {front = rear = 0;isempty = 1;if (SIZE > MAXN || SIZE < 0) throw Runtime_error ("Index Out of Range"), for (int i = 0; i < SIZE; i + +) Enque (List[i]);} Template<typename t>//If the queue is not full, increase the tail element void Cirqueue<t>::enque (T e) {if (rear = = Front &&!isempty) {cout << "already full!\n"; return;} Array[rear] = E;rear = (rear+1)%maxn;if (rear = = front) IsEmpty = 0;} Template<typename t>//If the queue is not empty, delete the team header element T Cirqueue<t>::d eque () {if (rear = front && isEmpty) {throw Runtime_eRror ("Already empty!\n");} T ele = Array[front];front = (front+1)%maxn;if (rear = = Front) IsEmpty = 1;return ele;} Template<typename t>int cirqueue<t>::getsize () {if (rear = = Front) return (isEmpty? 0:maxn); Elsereturn (rear -FRONT+MAXN)% Maxn;} Template<typename t>void cirqueue<t>::p rint () {int cnt = GetSize (); if (cnt = = 0) {cout << "Empty Queue!\n "; return;} int I=0,inde = Front;while (i < cnt) {cout << Array[inde] << ""; Inde = (inde+1)%maxn;i + +;} cout << Endl;} #endif
The policy 2 code looks like this:
#ifndef _cirqueue_h#define _cirqueue_h#include <iostream> #include <stdexcept> using namespace std;// 101 spaces, there is a space unavailable const int MAXN = 101;template<typename t>class cirqueue{private:t array[maxn];int front,rear; Public:cirqueue (); Cirqueue (t *list,int SIZE); void Enque (T e); T deque (); int getsize ();}; Template<typename T>cirqueue<t>::cirqueue () {front = rear = 0;} Template<typename T>cirqueue<t>::cirqueue (T *list, int SIZE) {front = rear = 0;if (SIZE >= MAXN-1) throw run Time_error ("Index Out of Range"), for (int i = 0; i < SIZE; i + +) Enque (List[i]);} Template<typename t>//If the queue is not full, increase the tail element void Cirqueue<t>::enque (T e) {if (rear+1)%MAXN = front) {cout << Already full!\n "; return;} Array[rear] = E;rear = (rear+1)%maxn;} Template<typename t>//If the queue is not empty, delete the team header element T Cirqueue<t>::d eque () {if (rear = = Front) {Throw Runtime_error ("already Empty!\n ");} T ele = Array[front];front = (front+1)%maxn;return ele;} Template<typename T>int CirqueuE<t>::getsize () {return (REAR-FRONT+MAXN)% Maxn;} #endif
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Circular queue--Loop Queue