Data structure of the queue C + + version

Source: Internet
Author: User


#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

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.