温故而知新->数据结构->队列->程序实现2_利用类

队列 ~ 程序实现二

本篇博客基于 温故而知新 -> 数据结构 -> 线性表 ->队列 博客中的理论知识,然后利用 C++ 中的 对数据结构中的 队列 进行代码实现!

其中涉及了队列的 (入队)(出队)(队头与队尾元素)改(没写(~ ̄▽ ̄)~),判空,打印等操作!并附带了实例以及对应的运行结果!

注意:本篇博客中 队列 的程序实现借鉴了 利用类实现单向链表 的程序!

具体内容如下
(1)queue.h

#pragma once
#include<iostream>
using namespace std;
#include<assert.h>

typedef int QDataType;
typedef struct QNode
{
	QDataType _val;
	struct QNode *_next;
};

struct QNode *createQNode(QDataType val);

class Queue
{
public:
	Queue() :_front(nullptr)
		, _rear(nullptr)
		, _size(0)
	{}

	~Queue()
	{
		if (_front != NULL)//不是空队列
		{
			/* 下面注释部分有点Bug,虽然也可以实现功能 */
			/*QNode *next = _front->_next;
			while (_front != NULL)
			{
				free(_front);
				_front = next;
				next = _front->_next;
			}*/
			QNode *next = _front;
			while (_front != NULL)
			{
				_front = _front->_next;
				free(next);
				next = _front;
			}
			_front = _rear = NULL;
		}
	}

	// 队尾入队列
	void QueuePush(QDataType data);
	// 队头出队列
	void QueuePop();
	// 打印队列
	void QueuePrint();
	// 获取队列头部元素
	QDataType QueueFront();
	// 获取队列队尾元素
	QDataType QueueBack();
	// 获取队列中有效元素个数
	int QueueSize();
	// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
	bool QueueEmpty();

private:
	QNode *_front;//队头 -- 取出数据
	QNode *_rear; //队尾 -- 插入数据
	int _size;	  //队列中元素个数
};

(2)main.cpp

#include"queue.h"
/* 结合单链表进行实现 */
struct QNode *createQNode(QDataType val)
{
	QNode *node = (QNode*)malloc(sizeof(QNode));
	node->_val = val;
	node->_next = nullptr;
	return node;
}
// 队尾入队列
void Queue::QueuePush(QDataType data)
{
	QNode *node = createQNode(data);
	if (_front == NULL) //此处主要是判断是否是空队列
		_front = _rear = node;
	else
	{
		_rear->_next = node;
		_rear = node;
	}
	_size++;
}
// 队头出队列
void Queue::QueuePop()
{
	assert(_front != NULL);
	QNode *cur = _front;
	_front = _front->_next;
	free(cur);
	_size--;
	if (_front == NULL)//即队列为空
		_rear = NULL;
}
// 打印队列
void Queue::QueuePrint()
{
	assert(_front != NULL);
	QNode *cur = _front;
	cout << "队列内容:";
	while (cur != NULL)
	{
		cout << cur->_val << " ";
		cur = cur->_next;
	}cout << endl;
}
// 获取队列头部元素 -- 获得队头元素的值
QDataType Queue::QueueFront()
{
	assert(_front != NULL);
	return _front->_val;
}
// 获取队列队尾元素
QDataType Queue::QueueBack()
{
	assert(_front != NULL);
	return _rear->_val;
}
// 获取队列中有效元素个数
int Queue::QueueSize()
{
	return _size;
}
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
bool Queue::QueueEmpty()
{
	if (_front == NULL && _rear == NULL)
		return true;
	else
		return false;
}

void test()
{
	Queue q1;
	/* 实验入队操作 */
	q1.QueuePush(1);
	q1.QueuePush(2);
	q1.QueuePush(3);
	q1.QueuePush(4);
	q1.QueuePrint();// 1 2 3 4
	cout << endl;

	/* 实验出队操作 -- 先进先出 */
	q1.QueuePop();
	q1.QueuePrint();// 2 3 4
	q1.QueuePop();
	q1.QueuePrint();// 3 4
	q1.QueuePop();
	q1.QueuePrint();// 4
	//q1.QueuePop();
	//q1.QueuePrint();// 会有警告,因为assert(),且此时队列为空
	cout << endl;

	/* 实验获取队头元素 */
	cout << "此时队头元素为:" << q1.QueueFront() << endl;
	/* 实验获取队尾元素 */
	cout << "此时队尾元素为:" << q1.QueueBack() << endl;
	cout << endl;

	cout << "此时队列元素个数为:" << q1.QueueSize() << endl;
	cout << "此时队列是否为空(是为1,否为0):" << q1.QueueEmpty() << endl;

	q1.QueuePop();

	cout << "此时队列元素个数为:" << q1.QueueSize() << endl;
	cout << "此时队列是否为空(是为1,否为0):" << q1.QueueEmpty() << endl;
	cout << endl;
}

int main()
{
	test();
	system("pause");
	return 0;
}

(3)运行结果
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值