C/C++:双向队列的实现

/**
*
* Althor:Hacker Hao
* Create:2023.10.11
* 
*/

#include <bits/stdc++.h>
using namespace std;
#define MAXSIZE 200
typedef struct Deque
{
	int front;    //头
	int rear;     //尾
	int num;      //队列中的元素数量
	int arr[MAXSIZE];  //队列中存储的数字
}Deque;

Deque* Init()    //初始化队列
{
	Deque* q = (Deque*)malloc(sizeof(Deque));
	if (!q)
	{
		cout << "建立错误!" << endl;
		exit(0);
	}
	q->front = q->rear = -1;
	q->num = 0;
	return q;
}

void Clean(Deque* q)   //清空队列
{
	if (!q)
	{
		cout << "队列不存在! " << endl;
		return;
	}
	//由于队列只是用了一次动态内存分配,所以直接free
	q->front = q->rear = -1;
	q->num = 0;
}

void Destroy(Deque* q)  //销毁队列
{
	if (!q)
	{
		cout << "队列不存在!" << endl;
		return;
	}
	free(q);
	q = NULL;
}

bool IsEmpty(Deque* q)   //判断是否为空
{
	if (q == NULL)
	{
		cout << "队伍不存在!" << endl;
		return true;  //为了防止队列空时对空指针操作
	}
	return q->num == 0;
}

bool IsFull(Deque* q)   //判断是否队满
{
	if (q == NULL)
	{
		cout << "队伍不存在!" << endl;
		return true;  //为了防止在队不满的情况下对空指针操作
	}
	return q->num == MAXSIZE;
}

int GetHead(Deque* q)  //返回队首元素
{
	if (q == NULL)
	{
		cout << "队列不存在!" << endl;
		return -1;
	}
	if (IsEmpty(q))
	{
		cout << "队列为空,没有头元素" << endl;
		return -1;
	}
	return q->arr[q->front];
}

int GetTail(Deque* q)  //返回队尾元素
{
	if (q == NULL)
	{
		cout << "队列不存在" << endl;
		return -1;
	}
	if (IsEmpty(q))
	{
		cout << "队列为空!" << endl;
		return -1;
	}
	return q->arr[q->rear];
}


int GetHeadIndex(Deque* q, int size) {
	if (IsEmpty(q)) {
		q->rear = 0;//如果从对头入队的是第一个元素,则需要将队头队尾指针都设为0
		return 0;
	}
	if (q->front - 1 < 0) {
		return (q->front - 1) + size;
	}
	else {
		return q->front - 1;
	}
}


void PushFromHead(Deque* q, int k)
{
	if (q == NULL)
	{
		cout << "队列不存在" << endl;
		return;
	}
	if (IsFull(q))
	{
		cout << "队列已满,无法入队" << endl;
		return;
	}
	q->front = GetHeadIndex(q, MAXSIZE);
	q->num++;
	q->arr[q->front] = k;
}

//获得从队尾入队的下标
int GetTailIndex(Deque* q, int size)
{
	if (IsEmpty(q))
	{
		q->front = 0;
		return 0;
	}
	return (q->rear + 1) % size;
}

//从队尾入队
void PushFromTail(Deque* q, int k)
{
	if (!q)
	{
		cout << "队列不存在" << endl;
		return;
	}
	if (IsFull(q))
	{
		cout << "队列已满,无法入队" << endl;
		return;
	}
	q->rear = GetTailIndex(q, MAXSIZE);
	q->num++;
	q->arr[q->rear] = k;
}


//从队头出队
int PopFromHead(Deque* q) {
	if (q == NULL) {
		cout << "队列不存在" << endl;
		return -1;
	}
	if (IsEmpty(q)) {
		cout << "队列为空,无法出队" << endl;
		return -1;
	}
	q->num--;
	int ret = q->arr[q->front];
	q->front = (q->front + 1) % MAXSIZE;
	return ret;
}

//从队尾出队
int PopFromTail(Deque* q) 
{
	if (q == NULL) {
		cout << "队列不存在" << endl;
		return -1;
	}
	if (IsEmpty(q)) {
		cout << "队列为空,无法出队" << endl;
		return -1;
	}
	int ret = q->arr[q->rear];
	q->num--;
	if (q->rear - 1 < 0) {
		q->rear = q->rear - 1 + MAXSIZE;
	}
	else {
		q->rear -= 1;
	}
	return ret;
}

//从队头开始打印队列数据
void Print(Deque* q) 
{
	if (q == NULL) {
		cout << "队列不存在" << endl;
		return;
	}
	if (IsEmpty(q)) {
		cout << "队列为空,无法打印" << endl;
		return;
	}
	int count = q->num;
	int p = q->front;
	while (count--) {
		cout << q->arr[p] << " ";
		p = (p + 1) % MAXSIZE;
	}
	cout << endl;
}

int main()
{
	cin.tie(0), cout.tie(0);
	Deque* q = Init();

	cout << "从队首入队:" << endl;
	for (int i = 0; i < 10; i++)
	{
		PushFromHead(q, i);
	}
	Print(q);

	cout << "从队尾入队:" << endl;
	for (int i = 20; i < 35; i++) {
		PushFromTail(q, i);
	}
	Print(q);

	cout << "从队头删除的元素是:" << PopFromHead(q) << endl;
	Print(q);

	cout << "从队尾删除的元素是:" << PopFromTail(q) << endl;
	Print(q);

	cout << "队首元素是:" << GetHead(q) << endl;
	cout << "队尾元素是:" << GetTail(q) << endl;

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值