队列的顺序存储实现

#include <stdio.h>
#include <stdlib.h>
#define ElementType int 
const int MAXSIZE = 10;

typedef struct QNode *Queue;
typedef struct QNode{
	ElementType Data[MAXSIZE];
	int rear;
	int front;
};
void InitQueue(Queue &Q){
	Q = (Queue)malloc(sizeof(struct QNode));
	Q->rear = Q->front = 0;
}

bool Is_Empty_Queue(Queue Q){
	return Q->front == Q->rear;
}
bool Is_Full_Queue(Queue Q){
	return ((Q->rear + 1)% MAXSIZE == Q->front);
}
bool AddQ(Queue &Q, ElementType X){
	if(Is_Full_Queue(Q)){
		printf("队列满
");
		return 0;
	}
	Q->rear =(Q->rear+1) % MAXSIZE;
	Q->Data[Q->rear] = X;
}
ElementType DelQ(Queue &Q){
	if(Is_Empty_Queue(Q)){
		printf("队列为空
");
		
	}else{
		Q->front = (Q->front+1)%MAXSIZE;
		return Q->Data[Q->front];
	}
}

int main(){
	Queue Q;
	InitQueue(Q);
	for(int i = 0;i<15;i++)
		AddQ(Q,i);
	
	for(int i = 0;i< 9;i++)
		printf("%d
",DelQ(Q));
	return 0;
}

  

原文地址:https://www.cnblogs.com/zangkuo/p/6143419.html