模拟队列(一)-C描述


模拟队列(一)-C描述


#include<stdlib.h>
#include<stdio.h>

#define STATUS_OK 1
#define STATUS_ERROR 0

#define TRUE 1
#define FALSE 0

#define MAX_SIZE 20



typedef int Status;
typedef int QElementType;//队列元素


//0、队列声明
//1、初始化队列
//2、队列是否空
//3、队列是否满
//4、得到队列末尾元素
//5、加入队列
//6、离开队列
//7、清空队列

//0、队列声明
typedef struct {
	QElementType data[MAX_SIZE];
	int front;
	int rear;
}Queue;

//1、初始化队列
Status initQueue(Queue *	Q ) {
	Q->front = Q->rear = 0; return STATUS_OK;
}

//2、队列是否空
Status isQueueEmpty(Queue *	Q) {
return	Q->front == Q->rear ? TRUE : FALSE;
}
//3、队列是否满
Status isQueueFull(Queue *	Q) {
	return Q->rear ==   MAX_SIZE-1? TRUE : FALSE;
}



//4、得到队列末尾元素
Status getLastElement(Queue * Q, QElementType *e) {
	if (isQueueEmpty(Q))
		return STATUS_ERROR;
	*e = Q->data[Q->rear-1];
		return STATUS_OK;
}
//5、加入队列
Status enQueue(Queue * Q, QElementType e) {
	if (isQueueFull(	Q)	) return STATUS_ERROR;;
	for (size_t i = Q->rear; i >0 ; i--)
	{
		Q->data[i] = Q->data[i-1];
	}

	Q->data[0] = e;
	Q->rear++;
	return STATUS_OK;
}
//6、离开队列
Status deQueue(Queue * Q) {
	if (isQueueEmpty(Q))
		return STATUS_ERROR;
	Q->rear--;
	return STATUS_OK;
}
//7、清空队列
Status clearQueue(Queue * Q) {

	Q->front = Q->rear = 0;
	return STATUS_OK;
}

Status visitQE(QElementType e) {
	printf("%3d",e); 	return STATUS_OK;
}


void showQ(Queue * Q) {
	for (int  i = 0; i < Q->rear; i++)
	{
		visitQE(Q->data[i]);
	}
	printf("\n");
}

void main() {

	Queue queue;
	initQueue(&queue);

	QElementType e = 0;

	for (size_t i = 44; i < 50; i++)
	{
		enQueue(&queue, i);
	}
	showQ(&queue);
	getLastElement(&queue, &e);
	printf("取出队列最后一个元素 %d\n",e);
	deQueue(&queue);
	enQueue(&queue,333);

	showQ(&queue);

	clearQueue(&queue);//清空元素
	printf("清空元素—>");
	printf(isQueueEmpty(&queue) ? "没有元素了" : "还有元素");
	getchar();

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值