数组模拟队列

/**
 * q: 队列
 * hh: 对头指针
 * tt: 队尾指针
 * 当队列非空时,hh 和 tt都是指向的有效元素
 * 同样是数组模拟,栈的栈顶指针初始值是0,栈从1开始存储元素
 * 队列中队尾元素初始值为-1,从0开始存储元素
 * 其实这个东西无所谓怎么定都行,但是前后一定要对应好,尤其是当改变hh和tt的意义时,要注意判空那里的条件
 */
#include <iostream>

using namespace std;

const int N = 1e5 + 10;

int q[N], hh, tt = -1;

inline void push(int x) // 将x进队
{
    q[++ tt] = x;
}
inline void pop() // 将队头元素出队
{
    ++ hh;
}
inline bool empty() // 判断队列是否为空
{
    if (tt >= hh) return false;
    return true;
}
inline int top() // 返回队头元素
{
    return q[hh];
}
posted @ 2021-01-19 11:30  0x7F  阅读(67)  评论(0编辑  收藏  举报