循环队列

#include
#include
#include

#define ARRAYSIZE 6

typedef struct Queue {
int *pBase;
int front;
int rear;
}QUEUE,*pQueue;

void init(pQueue q)
{
q->pBase = (int*)malloc(sizeof(int)*ARRAYSIZE);
q->front = 0;
q->rear = 0;
}

int FullQueue(pQueue q)
{
if(((q->rear)+1) % ARRAYSIZE == q->front) {
printf(“full queue\n”);
return 1;

} else {
	printf("no full queue\n");
	return 0;
}

}

int EmptyQueue(pQueue q)
{
if(q->rear == q->front) {
printf(“queue is null\n”);
return 1;
} else {
printf(“queue is not null\n”);
return 0;
}

}

int RunQueue(pQueue q, int num)
{
if(!FullQueue(q)) {
q->pBase[q->rear] = num;
q->rear = (q->rear + 1)%ARRAYSIZE;
}
return;
}

int OutQueue(pQueue q)
{
if(!EmptyQueue(q)) {
q->pBase[q->front] = 0;
q->front = (q->front + 1)%ARRAYSIZE;
}
return;
}

int ShowQueue(pQueue q)
{
int i = q->front;
while(i != q->rear) {
printf("%d",q->pBase[i]);
i = (i + 1) % ARRAYSIZE;
}
printf("\n");

}

void main(void)
{
QUEUE Q;//Q变量有三个成员
init(&Q);
RunQueue(&Q, 1);
RunQueue(&Q, 2);
RunQueue(&Q, 3);
RunQueue(&Q, 4);
RunQueue(&Q, 5);
RunQueue(&Q, 6);
printf("%d\n",Q.pBase[0]);
ShowQueue(&Q);
OutQueue(&Q);
ShowQueue(&Q);
}

你可能感兴趣的:(C语言,c语言,数据结构)