#include "stdafx.h"
/*
Queue is a FIFO linear table
The core of the queue is the operation of the head and tail index
As shown, when the head index is moved to the front 6, the tail is no longer at the end of the 0 position, then if the stack is not recycled, the queue is full, for this use (f+1%maxsize) way
When the opponent index to 6 of the position of the result is exactly 0 and back to the enemy. This allows for recycling. (Note maxsize=6+1, which is determined by the C + + array attribute)
*/
Template <class t>
Class Basequeuq {
Return the head element to X
virtual bool Front (T &x) = 0;
Into the team
virtual bool EnQueue (T &x) = 0;
Out Team
virtual bool DeQueue () = 0;
Empty queue
virtual void clear () = 0;
};
Template <class t>
Class Queue:p ublic basequeuq<t> {
int _INDEXF, _indexr,_maxsize;
T *_queue;
Public
Queue (int maxSize) {
_maxsize = maxSize;
_queue = new T[_maxsize];
_INDEXF = _INDEXR = 0;
}
BOOL Front (T &x) {
if (IsEmpty ()) {
return false;
}
else {
x = _queue[_indexf];
return true;
}
}
BOOL EnQueue (T &x) {
I f (isfull ()) {
Over flow
return false;
}
else {
_indexf= (_indexf+1)%_maxsize;//forward
_QUEUE[_INDEXF] = x;
return true;
}
}
BOOL DeQueue () {
ENTER
if (Isfull ()) {
Over flow
return false;
}
else {
_INDEXR = (_indexr+1)% _maxsize;//forward to the tail pointer 1
return true;
}
}
void Clear () {
_INDEXF = _INDEXR = 0;
}
BOOL Isfull () {
if ((_INDEXR + 1)% _maxsize = = _INDEXF) {//Because the loop queue is going to leave a space that is to be distinguished from an empty queue
return true;
}
else {
return false;
}
}
BOOL IsEmpty () {
return _INDEXF = = _INDEXR;
}
};
int main ()
{
int i = 3,J;
queue<int> *test = new queue<int> (5);
Test->enqueue (i);
Test->front (j);
printf ("%d", j);
while (1);
return 0;
}
Data structure of the queue C + + version