All data structures, algorithms and applications curricular templates please click: https://blog.csdn.net/weixin_44077863/article/details/101691360
Chain into the queues and the queue queues, according to another cycle queue if the loop
(Actually, I think the ordinary into the queue, deque, priority queue only tricky thing QAQ)
Circular queue order books directly on the template as follows :( circular queue is a template, because it is not really a big problem if the cycle)
(As long as not out of the loop through the summing coexist with a maxSize no longer saved)
template<class T>
class ArrayQueue{
private:
int maxSize,front,rear;
T*queue;
public://多出一个空间,用以区分队列空与满
ArrayQueue(int size=0):maxSize(size+1),queue(new T[maxSize]),front(0),rear(0){}
~ArrayQueue(){delete[] queue;}
void Clear(){front=rear=0;}
bool IsEmpty(){if(front==rear) return 1;return 0;}
bool IsFull(){if((rear+1)%maxSize==front) return 1;return 0;}
bool EnQueue(const T item){
if((rear+1)%maxSize==front){
cout<<"队列已满!入队失败!"<<endl;
return 0;
}
queue[rear]=item;
rear=(rear+1)%maxSize;
return 1;
}
bool DeQueue(T &item){
if(front==rear){
cout<<"队列为空!"<<endl;
return 0;
}
item=queue[front];
front=(front+1)%maxSize;
return 1;
}
bool GetFront(T &item){
if(front==rear){
cout<<"队列为空!"<<endl;
return 0;
}
item=queue[front];
return 1;
}
};
Simple talk about it this template, the template materials are more wonderful, looking not that clear, say something good point
size elements, defined maxsize = size + 1, why do it
First, the template elements subscripts (0 ~ size-1), and not the tail rear element index, but a position (subscript may push position) after the tail element
Then, a first in terms of the general misconception
Order size = 5, then the assumption front = 0, then the rear = 0,1,2,3,4 can be inserted, not 5, so some people said it would judge whether == front rear% size
So the question is, subscript 0 can be inserted into it? You mistake directly to the subscript 0 not lost
Well, it was said, front, rear equality, equal modulo whether separate sentence, that is a rear = 0 can be inserted, is a rear = 5, rear% 5 = 0 is not inserted
Good, Front = 2, then, can be inserted 2,3,4,0,1, 0 element front = rear = 2,5 or front = rear element = 2, the modulo irrelevant.
Thus, the finished bar.
Therefore, methods of teaching will play, though looking very strange, but it is a completely accurate entirely correct approach
It makes maxsize = size + 1, if the full sentence (rear + 1)% maxsize == front, which is fairly well understood, the former space to open a lot of people see no need
How specific thing we have to carefully analyze it under
Order size = 5, maxsize = 6, corresponding to the marked 0,1,2,3,4,5
If the front = 0, can be inserted into the rear = 0,1,2,3,4, not 5, it is determined (5 + 1) == 0 6% above the box that said almost
Then the front = 2 it? Very clever. 2,3,4,5,0 be inserted, can not be inserted into a (1 + 1)% 2 == 6 before the box solves said problem
See here, perhaps you will feel very confused, what is this principle?
0,1,2,3,4 and sentenced to the full 5-bit, six positions even turning up is the essence, because the box is not sentenced to almost a full position it can be considered fixed
So once the cycle will be a problem, and teaching methods to solve very clever, really great
Then
Some people say, then I rear altogether record last element index, a post do not remember
This method is also a great problem, front = rear = 0 then you have an element of? ?
You might say, right rear initialization -1
So when front = 2, rear = 1, which it is full or empty? ?
Of course, if you record the current size, it is straightforward to empty full sentence, write ye think ye write, all problems are solved.
But there is no record of the current textbooks size, he gave solve, it can be considered really very much
worth learning