【数据结构】静态循环队列的操作

静态循环队列的操作 详细请参考以下代码:

#include <stdio.h>

#define MAX_SIZE 100

typedef struct 
{
	int front;
	int rear;
	int data[MAX_SIZE];
}Queue;

// 循环队列
void initQueue(Queue * sq);
void DestoryQueue (Queue * sq); //销毁队列
void EnQueue (Queue * sq,int e); //进队列
void DeQueue (Queue * sq); //出队列
void GetHead (Queue * sq); //取队头元素值
bool QueueEmpty (Queue sq); //判队列空否
void DisQueue(Queue * sq);  // 遍历队列中元素
void main ()
{
	int size = 0;
	char choice = '\0';
	Queue sQueue = {0,};

	initQueue(&sQueue);

	while (choice != 'q')
	{
		printf("******************************\n");
		printf("---    1   元素进队       --- \n");
		printf("---    2   队头出队       --- \n");
		printf("---    3   遍历队中元素   --- \n");
		printf("---    4   判队列空否     --- \n");
		printf("---    5   取队头元素值   --- \n");
		printf("---    6   销毁队列       --- \n");
		printf("---    q   退出           --- \n");
		printf("******************************\n");

		scanf("\n%c",&choice);

		switch (choice)
		{
		case '1':
			printf("输入进栈元素个数:");
			scanf("%d",&size);

			for (int i = 0; i < size; i++)
				EnQueue (&sQueue, i+1) ; //进队列
			break;
		case '2':
			DeQueue(&sQueue);
			break;
		case '3':
			DisQueue(&sQueue);
			break;
		case '4':
			if (!QueueEmpty(sQueue))
				printf("队列不为空\n");
			break;
		case '5':
			GetHead(&sQueue);
			break;
		case '6':
			DestoryQueue(&sQueue);
			break;
		case 'q':
			return;

		}
	}
}

/*初始化队列*/
void initQueue(Queue * sq)
{
	sq->front = 0;
	sq->rear = 0;

	for (int i = 0; i < MAX_SIZE; i++)
		sq->data[i] = 0;
}

void DestoryQueue (Queue * sq) //销毁队列
{
	initQueue(sq);
	return;
}


void EnQueue (Queue * sq,int e) //进队列
{
	if ((sq->rear+1)% MAX_SIZE != sq->front) // 判断是否队满
	{
		sq->data[sq->rear] = e;
		sq->rear = (sq->rear+1)%MAX_SIZE;    // 队尾进队
	}
}


void DeQueue (Queue * sq)  //出队列
{
	if (!QueueEmpty(*sq)) // 队列不空时出队
		sq->front = (sq->front+1)%MAX_SIZE;
	else
		return;
}

void GetHead (Queue * sq) //取队头元素值
{
	if (!QueueEmpty(*sq))
		printf("队头元素为 %d\n",sq->data[sq->front]);
}

bool QueueEmpty (Queue sq) //判队列空否
{
	if (sq.front != sq.rear)
		return false;
	else
	{
		printf("队列为空,请先进队\n");
		return true;
	}
}

void DisQueue(Queue * sq)  // 遍历队列中元素
{
	if (QueueEmpty(*sq))
		return;

	printf("遍历队列中的元素\n");

	for (int i = sq->front; i < sq->rear; i++)
	{
		printf("%d\t",sq->data[i]);
	}
	printf("\n");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值