数据结构->双端队列(c/c++)

文章介绍了如何使用C++定义和实现了一个简单的队列结构,包括队列的初始化、元素的入队(尾插和头插)、出队(头部和尾部)、查看队列头尾元素以及相关操作的测试函数。
摘要由CSDN通过智能技术生成
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
//#define MAXSIZE 5
typedef int date_t;

typedef struct listqueue {//节点结构体
	date_t name_id;
	listqueue* next;
}listqueue,*list;
typedef struct queue {//队列结构体
	listqueue* front;
	listqueue* rear;
	int size;
}queue;
void initqueue(queue* node)
{
	listqueue* head=new listqueue;
	node->front = node->rear = head;
	head->next = nullptr;
	node->size = 0;
}
bool empty(queue* qulist)
{
	return qulist->size == 0;
}
void pushtop(date_t name,queue*qulist)
{

	listqueue* newqu = new listqueue;
	newqu->name_id = name;
	if (qulist->size == 0)
	{
		qulist->rear = newqu;
	}
	newqu->next = qulist->front->next;
	qulist->front->next = newqu;
	qulist->size++;
}
void pushback(date_t name, queue* qulist)
{
	listqueue* newqu = new listqueue;
	newqu->name_id = name;
	newqu->next = nullptr;
	if (qulist->size == 0)
	{
		qulist->front->next = newqu;
	}
	qulist->rear->next = newqu;
	qulist->rear = newqu;
	qulist->size++;
}
int toppop(queue*qulist)
{
	if (empty(qulist))
		return -1;
	date_t temp = qulist->front->next->name_id;
	listqueue* ptemp = qulist->front->next;
	qulist->front->next = qulist->front->next->next;
	delete ptemp;
	qulist->size--;
	return temp;
}
int backpop(queue*qulist)
{
	if (empty(qulist))
		return -1;
	date_t temp = qulist->rear->name_id;
	listqueue* ptem = qulist->front->next;
	if (qulist->front->next == qulist->rear)
	{
		qulist->size--;
		qulist->front->next = nullptr;
		delete ptem;
		qulist->rear = nullptr;
	}
	else
	{
		while (ptem != nullptr)
		{
			if (ptem->next == qulist->rear)
			{
				listqueue* mpte = qulist->rear;
				ptem->next = nullptr;
				qulist->rear = ptem;
				qulist->size--;
				delete mpte;
				break;
			}
			ptem = ptem->next;
		}
	}
	return temp;
}
void Viewfront(queue* qulist)
{
	if (!empty(qulist))
		cout << qulist->front->next->name_id << endl;
	else
		cout << "队列为空" << endl;
}
void Viewrear(queue* qulist)
{
	if(!empty(qulist))
	cout << qulist->rear->name_id << endl;
	else {
		cout << "队列为空" << endl;
	}
}
void testlistqueue()
{
	queue* L=new queue;
	initqueue(L);
	while (true) {
		cout << "\n队列操作:" << endl;
		cout << "1. 尾部插入元素" << endl;
		cout << "2. 头部弹出元素" << endl;
		cout << "3. 查看队列头元素" << endl;
		cout << "4. 获取队列的大小" << endl;
		cout << "5. 判断栈是否为空" << endl;
		cout << "6.查看队列尾元素" << endl;
		cout << "7.判断队列是否为满" << endl;
		cout << "8.头部插入元素" << endl;
		cout << "9.尾部弹出元素" << endl;
		int choice;
		cout << "请选择操作:";
		cin >> choice;
		switch (choice)
		{
		case 1:
			cout << "请输入插入元素" << endl;
			date_t re;cin >> re;
			pushback(re,L);
			break;
		case 2:
			cout << toppop(L) << endl;
			break;
		case 3:
			Viewfront(L);
			break;
		case 4:
			cout << L->size << endl;
			break;
		case 5:
			cout << empty(L) << endl;
			break;
		case 6:
			Viewrear(L);
			break;
		case 7:
			
			break;
		case 8:
			cout << "请输入插入元素" << endl;
			cin >> re;
			pushtop(re,L);
			break;
		case 9:
			cout << backpop(L) << endl;;
			break;
		default:
			break;
		}
	}
}
int main()
{
	testlistqueue();
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值