队列--循环--顺序存储

//顺需存储
#include<iostream>
using namespace std;
#define MaxSize 60
typedef int ElemType;
typedef struct {
	ElemType data[MaxSize];
	int front, rear;
}SqQueue;
bool InitSqQueue(SqQueue& Q)
{
	Q.front = Q.rear = 0;
	return true;
}
bool QueueEmpty(SqQueue Q)
{
	return (Q.rear == 0);
}
bool EnQueue(SqQueue& Q, ElemType e)
{
	if (Q.rear==MaxSize)
		return false;
	Q.data[Q.rear++] = e;
	return true;
}
bool DeQueue(SqQueue& Q, ElemType& e)
{
	if (QueueEmpty(Q)) return false;
	e = Q.data[Q.front];
	for (int i = 0; i < Q.rear; i++)
	{
		Q.data[i] = Q.data[i + 1];
	}
	Q.rear--;
	return true;
}
bool GetHead(SqQueue Q, ElemType& e)
{
	if (QueueEmpty(Q)) return false;
	e = Q.data[Q.front];
	return true;
}
void printQueue(SqQueue Q)
{
	for (int i = 0; i < Q.rear; i++)
	{
		cout << Q.data[i] <<' ';
	}
	cout << endl;
}
int main()
{
	SqQueue Q;
	ElemType e;
	InitSqQueue(Q);
	for (int i = 0; i < MaxSize; i++)
	{
		EnQueue(Q, i * 2);
	}
	DeQueue(Q, e);
	cout << "e=" << e << endl;
	DeQueue(Q, e);
	cout << "e=" << e << endl;
	DeQueue(Q, e);
	cout << "e=" << e << endl;
	GetHead(Q, e);
	cout << "e=" << e << endl;
	GetHead(Q, e);
	cout << "e=" << e << endl;
	GetHead(Q, e);
	cout << "e=" << e << endl;
	return 0;
}
//空一节点---循环队列
#include<iostream>
#define MaxSize 6
using namespace std;
typedef int ElemType;
class SqQueue {
public:
	bool InitQueue();
	bool QueueEmpty();
	bool EnQueue(ElemType e);
	bool DeQueue(ElemType& e);
	bool GetHead(ElemType& e);
	void Queuelength();
private:
	ElemType data[MaxSize];
	int front, rear;
};
bool SqQueue::InitQueue()
{
	front = rear = 0;
	return true;
}
bool SqQueue::QueueEmpty()
{
	return	rear== front;
}
bool SqQueue::EnQueue(ElemType e)
{
	if ((rear + 1) % MaxSize == front)
		return false;
	data[rear] = e;
	rear = (rear + 1) % MaxSize;
	return true;
}
bool SqQueue::DeQueue(ElemType& e)
{
	if (QueueEmpty())
		return false;
	e = data[front];
	front = (front + 1) % MaxSize;
	return true;
}
bool SqQueue::GetHead(ElemType& e)
{
	if (QueueEmpty()) return false;
	e = data[front];
	return true;
}
void SqQueue::Queuelength()
{
	cout << "Queuelength = " << (rear + MaxSize - front) % MaxSize << endl;
}
int main()
{
	SqQueue S;
	S.InitQueue();
	for (int i = 1; i < 6; i++)
	{
		S.EnQueue(i);
	}
	S.Queuelength();
	ElemType e;
	S.DeQueue(e);
	cout << "e=" << e << endl;
	S.DeQueue(e);
	cout << "e=" << e << endl;
	S.DeQueue(e);
	cout << "e=" << e << endl;
	S.Queuelength();
	for (int i = 1; i < 6; i++)
	{
		S.EnQueue(7+i);
	}
	S.Queuelength();
	cout << "----------------" << endl;
	S.DeQueue(e);
	cout << "e=" << e << endl;
	S.DeQueue(e);
	cout << "e=" << e << endl; 
	S.GetHead(e);
	cout << "e=" << e << endl;
	S.DeQueue(e);
	cout << "e=" << e << endl;
	S.DeQueue(e);
	cout << "e=" << e << endl;
	S.GetHead(e);
	cout << "e=" << e << endl;
	S.Queuelength();
	return 0;
}
//rear为-1,且计数长度
#include<iostream>
#define MaxSize 6
using namespace std;
typedef int ElemType;
class Queue {
public:
	bool InitQueue();
	bool QueueEmpty();
	bool EnQueue(ElemType e);
	bool DeQueue(ElemType& e);
	bool GetHead(ElemType& e);
	void QueueLength();
private:
	ElemType data[MaxSize];
	int front, rear, count;
};
bool Queue::InitQueue()
{
	front = 0;
	rear = -1;
	count = 0;
	return true;
}
bool Queue::QueueEmpty()
{
	if (count == 0) 
		return true;
	return false;
}
bool Queue::EnQueue(ElemType e)
{
	if (count == MaxSize)
		return false;
	rear = (rear + 1) % MaxSize;
	data[rear] = e;
	count++;
	return true;
}
bool Queue::DeQueue(ElemType& e)
{
	if (QueueEmpty())
		return false;
	e = data[front];
	count--;
	front = (front + 1) % MaxSize;
	return true;
}
bool Queue::GetHead(ElemType& e)
{
	if (QueueEmpty()) return false;
	e = data[front];
}
void Queue::QueueLength()
{
	cout << "QueueLength = " << count << endl;
}
int main()
{
	Queue S;
	S.InitQueue();
	for (int i = 1; i < 10; i++)
	{
		S.EnQueue(i);
	}
	S.QueueLength();
	ElemType e;
	S.DeQueue(e);
	cout << "e=" << e << endl;
	S.DeQueue(e);
	cout << "e=" << e << endl;
	S.DeQueue(e);
	cout << "e=" << e << endl;
	S.QueueLength();
	for (int i = 1; i < 6; i++)
	{
		S.EnQueue(7 + i);
	}
	S.QueueLength();
	cout << "----------------" << endl;
	S.DeQueue(e);
	cout << "e=" << e << endl;
	S.DeQueue(e);
	cout << "e=" << e << endl;
	S.GetHead(e);
	cout << "e=" << e << endl;
	S.DeQueue(e);
	cout << "e=" << e << endl;
	S.DeQueue(e);
	cout << "e=" << e << endl;
	S.GetHead(e);
	cout << "e=" << e << endl;
	S.QueueLength();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值