循环顺序队列的基本操作 (C语言源代码)

/********************************
 *  循环顺序队的基本操作 
 *  2021/8/18
 *************************************/
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE  10

typedef struct
{
	int* base;
	int front;
	int rear;
}SqQueue;

/*①初始化循环队列*/
int SqQueue_Init(SqQueue*q)
{
	q->base = (SqQueue*)malloc(MAXSIZE*sizeof(SqQueue));
	if (!q->base)
		return -1;
	else
	{
		q->front = q->rear = 0;
		return 1;
	}
}

/*②入队*/
int Enter_SqQueue(SqQueue* q, int e)
{
	if ((q->rear + 1) % MAXSIZE == q->front)
		return -1;
	else
	{   
		q->base[q->rear] = e;
		q->rear = (q->rear+1) % MAXSIZE;
		return 1;
	
	}
	
}
/*③取队列长度*/
int Length_SqQueue(SqQueue* q)
{
	return (q->rear - q->front + MAXSIZE) % MAXSIZE;
}

/*④取队头元素*/
int Get_Front(SqQueue* q)
{
	return q->base[q->front];
}


/*⑤出队*/
int Out_SqQueue(SqQueue* q,int *e)
{
	//int* e=NULL;
	if (q->rear == q->front)
		return -1;
	else
	{
		*e=q->base[q->front];
		q->front = (q->front + 1) % MAXSIZE;
		return *e;

	}

}




void main(void)
{
	/*①初始化循环队列*/
	SqQueue* q1 = (SqQueue*)malloc(sizeof(SqQueue));
	int a = 0;
	a=SqQueue_Init(q1);
	//printf("%d",a);
	
	/*②入队*/
	int i,b=0;
	for (i = 0; i < 5; i++)
	{
		b=Enter_SqQueue(q1,i+1);
		//printf("%d\n",b);
	}
	
	/*③取队列长度*/
	int c = 0;
	c = Length_SqQueue(q1);
	//printf("%d",c);

	/*④取队头元素*/
	int g = 0;
	g = Get_Front(q1);
	//printf("%d",g);

	/*⑤出队*/
	int j,f,d= 0;
	int *e1 = &d;
	for (j = 0; j < 5; j++)
	{
	   f=Out_SqQueue(q1,e1);
	   //printf("%d\n",f);
	}
	

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值