queue_include

循环队列的基本操作:创建,入对,出对,获取对头元素

sq->rear=(sq->rear+1)%MaxLen     //为了保证循环及不溢出

#include<stdio.h>
#define MaxLen 20
typedef char elemtype;
typedef struct
{
    elemtype data[MaxLen];
    int front,rear;
}queue;

void init(queue *sq)
{
    sq->front=0;
    sq->rear=0;
}

int enqueue(queue *sq,elemtype x)
{
    if((sq->rear+1)%MaxLen==0)
        return 0;
    else
    {
        sq->rear=(sq->rear+1)%MaxLen;
        sq->data[sq->rear]=x;
        return 1;
    }
}
int outqueue(queue *sq,elemtype *x)
{
    if(sq->front==sq->rear)
        return 0;
    else
    {
        sq->front=(sq->front+1)%MaxLen;
        *x=sq->data[sq->front];
        return 1;
    }
}

int empty(queue *sq)
{
    if(sq->rear==sq->front)
        return 1;
    else
        return 0;
}

int gethead(queue *sq,elemtype *x)
{
    if(empty(sq))
        return 0;
    else
    {
        *x=sq->data[(sq->front+1)%MaxLen];
        return 1;
    }
}
时间: 2024-12-11 18:56:47

queue_include的相关文章