队列之顺序队

 

#include<iostream>
#include<string>
using namespace std;
#define MAXSIZE 100
typedef struct
{
	int data[MAXSIZE];
	int front;
	int rear;
}Seqqueue, * PSeqqueue;
//初始化队
PSeqqueue Init_SeqQueue(void)
{
	PSeqqueue Q = (PSeqqueue)malloc(sizeof(Seqqueue));
	if (!Q)
		return 0;
	Q->front = 0;
	Q->rear = 0;
	return Q;
}
//入队

 //由于队头一直向后退,会造成假溢出,所以我们采用循环队列
 // //所以随着入队,出队的进行,导致队头和队尾转了好多圈,简单的说就是队头和队尾已经大于MAXSIZE(队的最大容量)
//所以要对MAXSIZE取余.头指针不放元素,从front的下一位置放元素。
int Push_SeqQueue(PSeqqueue Q, int e)
{
	if ((Q->rear + 1) % MAXSIZE == Q->front)//由于队头一直向后退,会造成假溢出,所以我们采用循环队列
		//所以随着入队,出队的进行,导致队头和队尾转了好多圈,简单的说就是队头和队尾已经大于MAXSIZE(队的最大容量)
		//所以要对MAXSIZE取余.头指针不放元素,从front的下一位置放元素。
	{
		cout << "队满";
		return 0;
	}
	Q->rear = (Q->rear + 1) % MAXSIZE;
	Q->data[Q->rear] = e;
	return 1;
}//出队
int Pop_SeqQueue(PSeqqueue Q, int* e)
{
	if (Q->rear == Q->front)//显然队头和对位肯定在同一周期,所以不需要对MAXSIZE取余
	{
		cout << "队空";
		return 0;
	}
	Q->front = (Q->front + 1) % MAXSIZE;
	*e = Q->data[Q->front];
	return 1;
}
//判空
int Empty_SeqQueue(PSeqqueue Q)
{
	if (Q->front == Q->rear)
	{
		return 1;
	}
	return 0;
}
//取队头元素
int Get_top_SeqQueue(PSeqqueue Q, int x)
{
	if (Q->front == Q->rear)
	{
		cout << "队空";
		return 0;
	}
	x = Q->data[(Q->front + 1) % MAXSIZE];
	return 1;

}
//销毁队
int Destroy_SeqQueue(PSeqqueue* Q)
{
	if (*Q)
	{
		free(*Q);
		*Q = NULL;
	}
	return 1;
}
int main()
{
	PSeqqueue Q;
	int e = 0;
	Q = Init_SeqQueue();
	Push_SeqQueue(Q, 1);
	Pop_SeqQueue(Q, &e);
	cout << '\n' << e << endl;
	Destroy_SeqQueue(&Q);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值