患者到医院看病模拟(队列)
![](https://i-blog.csdnimg.cn/blog_migrate/1767931b689a86c9947497f4715d5b81.png)
![](https://i-blog.csdnimg.cn/blog_migrate/79fff81d213349ba7fdc7eb14edf86f0.png)
#include <iostream>
using namespace std;
template <class T>
struct LinkNode
{
T data;
LinkNode* link;
LinkNode() {
link = NULL;
}
LinkNode(const T& item) {
data = item;
link = NULL;
}
};
template <class T>
class Queue {
private:
LinkNode<T>* const first = new LinkNode<T>();
int length;
public:
Queue() {
length = 0;
}
bool Insert(T& a) {
LinkNode<T>* newnode = new LinkNode<T>(a);
LinkNode<T>* current = first;
for (int i = 1; i <= length; i++) {
current = current->link;
}
current->link = newnode;
length++;
//cout << "病历号为" << current->link->data << "的病人就诊" << endl;
return true;
}
bool Insert2(T& a) {
LinkNode<T>* newnode = new LinkNode<T>(a);
LinkNode<T>* current = first;
for (int i = 1; i <= length; i++) {
current = current->link;
}
current->link = newnode;
length++;
//cout << "病历号为" << current->link->data << "的患者就诊" << endl;
return true;
}
bool Remove() {
LinkNode<T>* current = first;
if (current->link == NULL) {
cout << "无病人就诊" << endl;
}
else {
current = current->link;
first->link = current->link;
cout << "病历号为" << current->data << "的病人就诊" << endl;
delete current;
length--;
}
return true;
}
void Print() {
LinkNode<T>* current = first;
cout << "今天不再接收病人排队,下列排队的病人依次就诊:";
for (int i = 1; i <= length; i++) {
current = current->link;
cout << current->data << " ";
}
cout << endl;
}
};
int main() {
char ch;
int num;
Queue<int> q1;
cin >> ch;
while (1) {
switch (ch)
{
case 'A':
cin >> num;
q1.Insert(num);
break;
case 'a':
cin >> num;
q1.Insert2(num);
break;
case 'N':
case 'n':
q1.Remove();
break;
case 'S':
case 's':
q1.Print();
return 0;
break;
default:
cout << "输入命令不合法!" << endl;
cin >> ch;
continue;
break;
}
cin >> ch;
}
}