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

发布时间:2024年01月19日
#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 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 pop(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;
}
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;
		int choice;
		cout << "请选择操作:";
		cin >> choice;
		switch (choice)
		{
		case 1:
			cout << "请输入插入元素" << endl;
			date_t re;cin >> re;
			pushback(re,L);
			break;
		case 2:
			cout << pop(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;
		default:
			break;
		}
	}
}
int main()
{
	testlistqueue();
	
}

文章来源:https://blog.csdn.net/Chinausers/article/details/135699942
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。