src pen size 如果 元素 解决 urn code hid
- struct MyQueue { //循环队列需要少用一个
- int que[1000000], head, tail, MXSIZE;
- void init() {
- MXSIZE = 1000000; // 1e6
- head = tail = 0;
- }
- bool isEmpty() {
- return head == tail;
- }
- bool isFull() {
- return (tail + 1) % MXSIZE == head;
- }
- void add_back(int val) {
- if (isFull()) assert(false);
- que[tail] = val;
- tail = (tail + 1) % MXSIZE;
- }
- int del_front() {
- if (isEmpty()) assert(false);
- int cur = que[head];
- head = (head + 1) % MXSIZE;
- return cur;
- }
- } que;
做题的时候感觉需要循环队列,不然炸内存,也不用stl,慢
然后自己写了一个,发现忘记了。T_T
主要是判断full和empty那里,可以知道head == tail就是empty了,但是如果你的队列大小是6,你也想装6个元素,那么head == tail也是满的一个条件。
为了解决这个,只能是少用一个了,6个的大小,用5个。
这样,当(tail + 1) % 6 == head,就是满了的标志
循环队列的复习
来源: http://www.bubuko.com/infodetail-2335436.html