Implementation of the chained queue of the data structure
Tools used: VS2019
Function:
1. Realize the basic functions of the chain queue
2. Use object-oriented thinking to complete
software download:
Click to enter the download interface of CSDN
Table of contents
- Implementation of the chained queue of the data structure
- Function:
- software download:
- 1. Code part
- 2. Running results
-
- 1. Enter the queue (firstly push three data: 100, 200, 300)
- 2. Display the data in the chained queue
- 3. What is the size of the queue at this time?
- 4. Check the queue head element
- 5. Check whether the chained queue is empty at this time, if the queue is empty, output true
- 6. Get a team and try it out
- 7. The team is completed, and the data in the current team is displayed
- 8. What is the size of the queue at this time?
- 3. Final summary
1. Code part
Header file MyQueue.h:
#pragma once
#include <iostream>
using namespace std;
class MyQueue
{
public:
virtual ~MyQueue() {
};
// 判断队列是否为空
virtual bool QueueEmpty() const = 0;
// 查看队列长度
virtual int QueueSize() const = 0;
// 获取队列中队头元素
virtual int& QueueFrontElement() = 0;
// 队列入队
virtual void QueuePush(const int& element) = 0;
// 队列出队
virtual void QueuePop() = 0;
// 展示队列中的元素
virtual void QueueShow() = 0;
};
struct MyNode
{
int data;
MyNode* next;
MyNode(int data = -1, MyNode* next = nullptr) :data(data), next(next) {
}
};
class MyListQueue :public MyQueue
{
public:
MyListQueue(MyNode* queueFront = nullptr, MyNode* queueBack = nullptr, int queueSize = 0);
~MyListQueue();
// 判断队列是否为空
bool QueueEmpty() const;
// 查看队列长度
int QueueSize() const;
// 获取队列中队头元素
int& QueueFrontElement();
// 队列入队
void QueuePush(const int& element);
// 队列出队
void QueuePop();
// 展示队列中的元素
void QueueShow();
private:
MyNode* queueFront;
MyNode* queueBack;
int queueSize;
};
MyQueue.c:
#include "MyQueue.h"
// 参数列表初始化
MyListQueue::MyListQueue(MyNode* queueFront, MyNode* queueBack, int queueSize) :
queueFront(queueFront), queueBack(queueBack), queueSize(queueSize)
{
}
// 析构函数
MyListQueue::~MyListQueue()
{
delete queueFront;
delete queueBack;
queueSize = 0;
}
bool MyListQueue::QueueEmpty() const
{
return queueSize == 0;
}
int MyListQueue::QueueSize() const
{
return queueSize;
}
int& MyListQueue::QueueFrontElement()
{
if (!QueueEmpty())
{
return queueFront->data;
}
else
{
cout << "队列为空,无法获取队头元素" << endl;
exit(0);
}
}
void MyListQueue::QueuePush(const int& element)
{
MyNode* newNode = new MyNode(element);
if (QueueEmpty())
{
queueFront = newNode;
}
else
{
queueBack->next = newNode;
}
queueBack = newNode;
queueSize++;
}
void MyListQueue::QueuePop()
{
if (!QueueEmpty())
{
MyNode* queueFrontNext = queueFront->next;
delete queueFront;
queueFront = queueFrontNext;
queueSize--;
}
else
{
cout << "队列为空,无法出队" << endl << endl;
exit(0);
}
}
void MyListQueue::QueueShow()
{
if (!QueueEmpty())
{
MyNode* pMove = queueFront;
while (pMove)
{
cout << pMove->data << "\t";
pMove = pMove->next;
}
cout << endl << endl;
}
else
{
cout << "队列为空,无法输出" << endl << endl;
exit(0);
}
}
main.c:
#include "MyQueue.h"
/*
* @author : LZY
* @date : 2021/3/30 12:28:06
* @theme : 链式队列的功能实现
*/
void MainMenu();
int main()
{
int choice = -1;
int data = -1;
MyQueue* pListQueue = new MyListQueue;
while (true)
{
MainMenu();
cout << "选择功能:";
cin >> choice;
switch (choice)
{
case 1:
cout << "输入要入队的元素:";
cin >> data;
pListQueue->QueuePush(data);
cout << "入队成功" << endl << endl;
break;
case 2:
pListQueue->QueuePop();
cout << "出队成功" << endl << endl;
break;
case 3:
cout << pListQueue->QueueFrontElement() << endl << endl;
break;
case 4:
cout << boolalpha << "队列是否为空:" << pListQueue->QueueEmpty() << endl << endl;
break;
case 5:
cout << "队列大小:" << pListQueue->QueueSize() << endl << endl;
break;
case 6:
pListQueue->QueueShow();
break;
default:
break;
}
system("pause");
system("cls");
}
return 0;
}
void MainMenu()
{
cout << "*---------链式队列---------*" << endl;
cout << "| |" << endl;
cout << "| 1.入队 |" << endl;
cout << "| 2.出队 |" << endl;
cout << "| 3.队头 |" << endl;
cout << "| 4.队空 |" << endl;
cout << "| 5.大小 |" << endl;
cout << "| 6.展示 |" << endl;
cout << "| |" << endl;
cout << "*--------------------------*" << endl;
}
2. Running results
1. Enter the queue (firstly push three data: 100, 200, 300)
2. Display the data in the chained queue
3. What is the size of the queue at this time?
4. Check the queue head element
5. Check whether the chained queue is empty at this time, if the queue is empty, output true
6. Get a team and try it out
7. The team is completed, and the data in the current team is displayed
8. What is the size of the queue at this time?
3. Final summary
Achieved the desired function.