队列的实现

http://blog.csdn.net/leichelle/article/details/7546775

1.定义 :

队列(Queue):也是运算受限的线性表。是一种先进先出(First In First Out ,简称FIFO)的线性表。

队首(front) :允许进行删除的一端称为队首。
队尾(rear) :允许进行插入的一端称为队尾。
例如:排队购物。操作系统中的作业排队。先进入队列的成员总是先离开队列。

2.实现

 

struct queue {
    unsigned int elem[KEY_BUF_SIZE];//定长数组
    int front;
    int rear;
};

//定义一个全局的队列,初始化
static struct queue buf = {
    .front = 0,
    .rear = 0
};

static int queue_isfull(struct kqueue *q)
{
    return q->front == (q->rear + 1) % KEY_BUF_SIZE;
}

static int queue_isempty(struct kqueue *q)
{
    return q->front == q->rear;
}

static int queue_add(struct kqueue *q, unsigned int elm)
{
    if (queue_isfull(q))
        return -1;

    q->elem[q->rear] = elm;
    q->rear = (q->rear + 1) % KEY_BUF_SIZE;

    return 0;
}

static int queue_get(struct kqueue *q, unsigned int *elm)
{
    if (queue_isempty(q))
        return -1; 

    *elm = q->elem[q->front];

    q->front = (q->front + 1) % KEY_BUF_SIZE;
    return (*elm);

}

 

转载于:https://www.cnblogs.com/chencesc/p/5090659.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值