循环队列全部操作实现代码(C语言)

栈和队列不愧为亲兄弟,在栈的实现基础上简单改改即可实现队列全部操作,加上循环二字,也就是多了个%取余(模)运算,放上成果,立码为证:

/*
实现循环队列的全体操作
						*/
#include "stdio.h"
#define MAXSIZE    50
#define TRUE       1
#define FALSE      0
 
typedef int bool;       //Cpp中这一行要取消掉,他内置bool型 
typedef int Status;    //函数类型,其值是函数结果状态码
typedef int ElemType;  //数据类型 
 
 
typedef struct queue
{
	ElemType Data[MAXSIZE];
	int front,rear;
}SqQueue;
 
 
void InitQueue(SqQueue *q)//初始化队列
{
	 q->rear=q->front=0; //一定要赋值,否则会出野指针 
}
 
bool QueueEmpty(SqQueue *q)//判队空 
{
	if(q->rear==q->front)
		return TRUE;
	else
		return FALSE;
}
 
bool EnQueue(SqQueue *q,ElemType e)//入队 
{
	if((q->rear+1)%MAXSIZE==q->front)  //队满 
	    {
    	printf("Queue is Full\n");   	
		return FALSE;
	    }
	    q->Data[q->rear]=e;
		q->rear=(q->rear+1)%MAXSIZE;//队尾指针加1取模 
		printf("EnQueue data %d into Queue \n",e);
	    return TRUE;
}
bool  DeQueue(SqQueue *q,ElemType *e)//出队 
{
	if(q->rear==q->front)
	    {
    	printf("Queue is Empty\n");   	
		return FALSE;
	    }
		*e=q->Data[q->front];//先取数再移指针 
		q->front=(q->front+1)%MAXSIZE;  //队头指针加1取模  
		printf("DeQueue data is %d\n",*e);
		return TRUE;
}
 
bool GetFront(SqQueue *q,ElemType *e)//取队头元素 
{
	if(q->rear==q->front)
		return FALSE;
	*e=q->Data[q->front];
		printf("GetFront data is %d\n",*e);
	return TRUE;
}
 
int main()
{
	SqQueue FS,*FSPtr;
	FSPtr=&FS;
	ElemType a=1,b=2,c=3;
	ElemType *x=&a;			//用来返回弹出或取出的元素 ,一定要赋初值,否则野指针 
	int i;
 
 
 
    InitQueue(FSPtr);
    printf("%s\n",QueueEmpty(FSPtr)==TRUE?"Queue is Empty":"Queue is not Empty");
    EnQueue(FSPtr,a);
    printf("%s\n",QueueEmpty(FSPtr)==TRUE?"Queue is Empty":"Queue is not Empty");
    EnQueue(FSPtr,b);
    EnQueue(FSPtr,c);
    for(i=0;i<50;i++)  //顺序压0-49数字入队 
    {
    	    EnQueue(FSPtr,i);
   	     					 }
	DeQueue(FSPtr,x);
	printf("DeQueue data is %d\n",*x);
    GetFront(FSPtr,x);

   for(i=0;i<20;i++)  //顺序出队 20次 
    {
    	    DeQueue(FSPtr,x);
    	    	    }
	
	printf("The Queue is:\n");
	
	for(i=(FSPtr->front)%MAXSIZE;i<(FSPtr->rear)%MAXSIZE;i++) //输出全队 
{
	printf("%d	",FSPtr->Data[i]);
}	
getchar();
return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

G00dChina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值