您的位置:首页 > 编程语言 > C语言/C++

c语言实现静态循环队列

2018-03-06 22:27 399 查看
#include<stdio.h>
#include<malloc.h>

typedef struct queue
{
    int * Base;
    int front;
    int rear;
} QUEUE,*pQUEUE;

bool init(pQUEUE);
bool enter_queue(pQUEUE,int val);
bool full_queue(pQUEUE);
bool empty_queue(pQUEUE);
bool out_queue(pQUEUE);
bool traverse(pQUEUE);

int main()
{
    QUEUE my;
    init(&my);
    enter_queue(&my,1);
    enter_queue(&my,2);
    enter_queue(&my,3);
    enter_queue(&my,4);
    enter_queue(&my,5);
    traverse(&my);
    out_queue(&my);
    out_queue(&my);
    traverse(&my);

    return 0;
}
bool init(pQUEUE T)
{
    T->Base=(int*)malloc(sizeof(int)*6);
    if(T->Base==NULL)
        return false;
    T->front=0;
    T->rear=0;
    return true;
}
bool enter_queue(pQUEUE T,int val)
{
    if(full_queue(T))
        return false;
    T->Base[T->rear%6]=val;
    T->rear++;
    return true;

}
bool full_queue(pQUEUE T)
{
    if((T->rear+1)%6==T->front)
        return true;
    return false;
}
bool empty_queue(pQUEUE T)
{
    if(T->front==T->rear)
        return true;
    return false;
}
bool traverse(pQUEUE T)
{
    if(empty_queue(T))
        return false;
    for(int i=T->front;i!=T->rear;i=(i+1)%6)
        printf("%d\n",T->Base[i]);
    return true;
}
bool out_queue(pQUEUE T)
{
    if(empty_queue(T))
        return false;
    T->front=(T->front+1)%6;
    return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: