顺序队的基本操作(C 完整源代码)

#include

typedef int QElemType;
#define MAXSIZE 10

typedef struct 
{
    QElemType data[MAXSIZE];
    int front;
    int rear;
}SqQueue;

void InitQueue(SqQueue *Q)      //初始化
{
    Q->front =0;
    Q->rear=0;
}

void EnQueue(SqQueue *Q,QElemType e)        //入队
{
    if((Q->rear+1)%MAXSIZE==Q->front)
        return;
    Q->data[Q->rear]=e;
    Q->rear =(Q->rear+1)%MAXSIZE;
}

void DeQueue(SqQueue *Q,QElemType *e)       //出队
{
    if(Q->front==Q->rear)
        return;
    *e=Q->data[Q->front];
    Q->front=(Q->front+1)%MAXSIZE;
}

int QueueLength(SqQueue *Q)     //计算当前队长
{
    return (Q->rear-Q->front+MAXSIZE)%MAXSIZE;
}   

int succ(int i) 
{
    if(i == MAXSIZE)
        i = 0;
    else
        i += 1;
    return i;
}

void Printf(SqQueue *Q)
{
    int i;
    for(i = Q->front; i != Q->rear; i = succ(i))
        printf("%d ", Q->data[i]);
}

void main()
{
    SqQueue Q;
    InitQueue(&Q);

    EnQueue(&Q,1);
    EnQueue(&Q,2);
    EnQueue(&Q,3);
    EnQueue(&Q,4);
    EnQueue(&Q,5);
    EnQueue(&Q,6);
    EnQueue(&Q,7);
    EnQueue(&Q,8);

    Printf(&Q);

}

你可能感兴趣的:(数据结构-C)