队列具有先进先出的特性,设计一个20字节(unsigned char)的队列,请用C语言实现队列的push和pop函数。

unsignedcharqueue_buf[20];... unsigned char queue_buf[20]; 展开
 我来答
lgao622
推荐于2016-07-19 · 知道合伙人软件行家
lgao622
知道合伙人软件行家
采纳数:1137 获赞数:6551
毕业于武汉工程大学邮电与信息工程学院通信专业,软件行业,4年工作经验。

向TA提问 私信TA
展开全部
#include<stdio.h>
#include <string.h>

#define byte unsigned char
byte queue_buf[20], idx = 0;

void push(byte n)
{
    if (idx < 20)
        queue_buf[idx++] = n;
}

byte pop()
{
    byte ret = 0;
    if (idx-- > 0)
    {
       ret = queue_buf[0];
       memcpy(queue_buf, &queue_buf[1], idx);
    }
    return ret;
}

byte size()
{
    return idx;
}

int main()
{
    int len;
    for (int i = 1; i <= 20; i++)
        push(i);
    printf("size = %d\n", len = size());
    for (int i = 1; i <= len; i++)
        printf("%d ", pop());
    printf("\n");
    return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式