顺序队列的C语言实现

时间:2022-01-10 00:46:53
#include <stdio.h>

#define MAXSIZE 101
#define ELEMTYPE int
#define QUEUE_EMPTY -10000 typedef struct queue
{
ELEMTYPE data[MAXSIZE];
int head;
int tail;
} Queue; void initQueue(Queue *Q);
void enQueue(Queue *Q,ELEMTYPE e);
ELEMTYPE deQueue(Queue *Q);
int isQueueEmpty(Queue *Q);
int isQueueFull(Queue *Q);
void printQueue(Queue *Q); int main(void)
{
Queue SQ;
initQueue(&SQ);
int j;
for(j=0;j<4;++j)
{
push(&SQ,j);
}
printQueue(&SQ);
return 0;
} void initQueue(Queue *Q)
{ Q->head = 1;
Q->tail = 1;
} void enQueue(Queue *Q,ELEMTYPE e)
{
if(isQueueFull(Q)) return; Q->data[Q->tail] = e;
Q->tail ++;
} ELEMTYPE deQueue(Queue *Q)
{
if(isQueueEmpty(Q)==1) return QUEUE_EMPTY; return Q->data[Q->head ++]; }
int isQueueEmpty(Queue *Q)
{
return Q->head == Q->tail;
} int isQueueFull(Queue *Q)
{
return ( (Q->tail + 1)%MAXSIZE ) == Q->head ;
} void printQueue(Queue *Q)
{ while(!isQueueEmpty(Q))
{
printf("%d\t",pop(Q));
}
}