数据结构-头歌-循环队列的基本操作

省流版:

#include <stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;

 // 函数结果状态代码
 #define TRUE 1
 #define FALSE 0
  #define OK 1
 #define ERROR 0
 #define OVERFLOW -1
 
 typedef int Status; 
 typedef int QElemType;

#define MAX_QSIZE 5 // 最大队列长度+1

struct SqQueue
{
   QElemType *base; // 初始化的动态分配存储空间
   int front; // 头指针,若队列不空,指向队列头元素
   int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置
};

Status InitQueue(SqQueue &Q); // 构造一个空队列Q
Status DestroyQueue(SqQueue &Q); // 销毁队列Q,Q不再存在
Status ClearQueue(SqQueue &Q); // 将Q清为空队列
Status QueueEmpty(SqQueue Q); // 若队列Q为空队列,则返回TRUE;否则返回FALSE
int QueueLength(SqQueue Q); // 返回Q的元素个数,即队列的长度
Status GetHead(SqQueue Q,QElemType &e); // 若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR
Status EnQueue(SqQueue &Q,QElemType e); // 插入元素e为Q的新的队尾元素
Status DeQueue(SqQueue &Q,QElemType &e); // 若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
Status QueueTraverse(SqQueue Q); // 从队头到队尾依次输出队列Q中每个元素
void vi(QElemType& e);    //打印元素

int main()
{
   int j;
   int i=0,l;
   QElemType d;
   SqQueue Q;
   InitQueue(Q);
   for(i=0;i<MAX_QSIZE;i++)
   {
     scanf("%d",&d);     
     EnQueue(Q,d);
   };
   printf("队列长度为: %d\n",QueueLength(Q));
   printf("现在队列中元素:\n");
   QueueTraverse(Q);
   DeQueue(Q,d);
   printf("删除的元素是%d\n",d);
   DeQueue(Q,d);
   printf("删除的元素是%d\n",d);
   scanf("%d",&d);
   EnQueue(Q,d);
    printf("队列长度为: %d\n",QueueLength(Q));
   printf("现在队列中元素:\n");
   QueueTraverse(Q);
   j=GetHead(Q,d);
   if(j)
     printf("现在队头元素为:%d\n",d);
   ClearQueue(Q);
   DestroyQueue(Q);
}
// 循环队列的基本操作(9个)
Status InitQueue(SqQueue &Q)
{ // 构造一个空队列Q
   /********** Begin **********/ 
	Q.base = (int*)malloc(sizeof(int) * MAX_QSIZE);
	if (!Q.base)
		return ERROR;
	Q.front = Q.rear = 0;
	return OK;
	/********** End **********/  
}

Status DestroyQueue(SqQueue &Q)
{ // 销毁队列Q,Q不再存在
   /********** Begin **********/ 
	if (Q.base)
		free(Q.base);
	Q.base = 0;
	Q.front = Q.rear = 0;
	return OK;
 
	/********** End **********/  
}

Status ClearQueue(SqQueue &Q)
{ // 将Q清为空队列
    /********** Begin **********/ 
	Q.front = Q.rear = 0;
	return OK;
	/********** End **********/  
}

Status QueueEmpty(SqQueue Q)
{ // 若队列Q为空队列,则返回TRUE;否则返回FALSE
   /********** Begin **********/ 
	if (Q.rear == Q.front)
		return TRUE;
	else
		return FALSE;
	/********** End **********/  
 }

int QueueLength(SqQueue Q)
{ // 返回Q的元素个数,即队列的长度
   /********** Begin **********/ 
	return (Q.rear - Q.front + MAX_QSIZE) % MAX_QSIZE;

	/********** End **********/  
 }

Status GetHead(SqQueue Q,QElemType &e)
 { // 若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR
   /********** Begin **********/ 
	if (Q.rear == Q.front)
		return ERROR;
	e = Q.base[Q.front];
	return OK;
 /********** End **********/  
}

Status EnQueue(SqQueue &Q,QElemType e)
{ // 插入元素e为Q的新的队尾元素
   /********** Begin **********/ 
	if ((Q.rear + 1) % MAX_QSIZE == Q.front)
		return ERROR;
	Q.base[Q.rear] = e;
	Q.rear = (Q.rear + 1) % MAX_QSIZE;
	return OK;
	/********** End **********/  
 }

Status DeQueue(SqQueue &Q,QElemType &e)
{ // 若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
   /********** Begin **********/ 
	if (QueueEmpty(Q))
		return ERROR;
	e = Q.base[Q.front];
	Q.front = (Q.front + 1) % MAX_QSIZE;
	return OK;
	

	/********** End **********/  
}

Status QueueTraverse(SqQueue Q)
{ // 从队头到队尾依次对队列Q中每个元素调用函数vi()
   /********** Begin **********/ 
	int i = Q.front;
	while (i != Q.rear)
	{
		vi(Q.base[i]);
		i = (i + 1) % MAX_QSIZE;
	}
	printf("\n");
	return OK;
	/********** End **********/  
}

void vi(QElemType& e)
{
	printf(" %d", e);
}

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值