写在前面:
数据结构实验课上课的时候写的,有点仓促。
实验内容及要求:
编程建立循环队列存储结构,对排队买票过程进行模拟。要求程序在控制台屏幕上显示字符菜单:
1. 排队——输入新到达的买票人姓名,加入买票队列中;
2. 售票——排队队列中最前面的人购票成功,显示信息并将其从队列中删除;
3. 查看队列——从队首到队尾依次列出所有正在排队买票人的姓名;
4. 结束——退出系统。
“排队”时,若队满,应提示等待(排队不成功);
“售票”时,若队空,应提示无人排队(售票失败)。
实验目的:掌握循环队列的基本操作。
(之前有人反映说多文件太麻烦了,所以现在以后都改成单文件了)
main.cpp:
#include <iostream>
using namespace std;
template<class A>
class SqQueue {
private:
A* start;
A* end;
A* queue;
int size{};
int max_size{};
public:
explicit SqQueue<A>(int max_size);
bool push_back(A item);
bool pop_front();
A front();
bool empty();
void free();
template<class B>
void iterate(void(*p) (const B& str));
~SqQueue();
};
template<class A>
SqQueue<A>::SqQueue(int max_size) {
this->size = 0;
this->max_size = max_size;
this->queue = new A[max_size + 1];
this->start = queue;
this->end = queue + 1;
}
template<class A>
A SqQueue<A>::front() {
return *start;
}
template<class A>
bool SqQueue<A>::pop_front() {
if (size == 0 || max_size == 0) {
cout << "The queue is empty" << endl;
return false;
}
start = start + 1;
if (start == nullptr) {
start = queue;
}
size--;
return true;
}
template<class A>
bool SqQueue<A>::push_back(A item) {
if (size == 0) {
(*start) = item;
size++;
return true;
} else if (size == max_size - 1) {
cout << "The queue is full" << endl;
return false;
}
end += 1;
if (end == nullptr) {
end = queue;
*(queue + max_size) = item;
size++;
return true;
} else {
*(end - 1) = item;
size++;
return true;
}
}
template<class A>
void SqQueue<A>::free() {
delete queue;
}
template<class A>
SqQueue<A>::~SqQueue() {
free();
}
template<class A>
bool SqQueue<A>::empty() {
if (size == 0) {
return true;
} else {
return false;
}
}
template<class A>template<class B>
void SqQueue<A>::iterate(void (*p)(const B &)) {
if (size == 0) {
cout << "The queue is empty!" << endl;
return ;
}
A* temp = start;
while (temp != end) {
p(*temp);
temp++;
if (temp == nullptr) {
temp = queue;
}
}
}
void show_menu() {
cout << "--------------------------------------------------" << endl;
cout << " Options " << endl;
cout << " 1.Join the queue " << endl;
cout << " 2.Buy the ticket " << endl;
cout << " 3.Check the queue " << endl;
cout << " 4.Exit " << endl;
cout << "--------------------------------------------------" << endl;
}
void output(const string& str) {
cout << str << " is waiting." << endl;
}
int main() {
int size = 0;
cout << "Enter the size of the queue" << endl;
cout << "(The actual size will be 1 less)" << endl;
cin >> size;
int status;
SqQueue<string> waiting_queue(size);
while (true) {
show_menu();
cin >> status;
string guest;
switch (status) {
case 1:
cout << "Joining the queue" << endl;
cout << "(Please enter the guest's name:)" << endl;
cin >> guest;
if (waiting_queue.push_back(guest)) {
cout << "Succeed!" << endl;
} else {
cout << "The queue is full, joining failed!" << endl;
}
break;
case 2:
cout << "Start ticketing!" << endl;
if (waiting_queue.empty()) {
cout << "No one is waiting!" << endl;
} else {
cout << waiting_queue.front() << " is buying tickets..." << endl;
waiting_queue.pop_front();
cout << "Succeed!" << endl;
}
break;
case 3:
cout << "Checking the queue..." << endl;
waiting_queue.iterate(&output);
break;
case 4:
cout << "Exit successfully!" << endl;
return 0;
default:
break;
}
}
}
有问题评论区指教,谢谢
文章评论