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

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

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

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;
		exit(0);
	}
	free(q);
	q = NULL;
}

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

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;
		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) {
		printf("队列不存在\n");
		return -1;
	}
	if (IsEmpty(q)) {
		printf("队列为空,无法出队\n");
		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) {
		printf("队列不存在\n");
		return -1;
	}
	if (IsEmpty(q)) {
		printf("队列为空,无法出队\n");
		return -1;
	}
	int ret = q->arr[q->rear];
	q->num--;
	if (q->rear - 1 < 0) {
		q->rear = q->rear - 1 + MAXSIZE;
	}
	else {
		q->rear = q->rear - 1;
	}
	return ret;
}

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

int main()
{
	cin.tie(0), cout.tie(0);
	Deque* q = Init();
	for (int i = 0; i < 10; i++)
	{
		PushFromTail(q, i);
	}
	Print(q);

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

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

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

	return 0;
}

你可能感兴趣的:(算法,数据结构,c++,c语言)