题目描述 患者到医院看病的顺序是:先排队等候,再看病治疗。要求设计一个算法,模拟病人等候就诊的过程。 其中:“病人到达”用命令“?A?”(或“?a?")表示,“护士让下一位就诊”用“?N?”(或"?n?")表示,“不再接收病人排队”用“?S?"(或“?s?")表示。 输入描述: ?A?(或?a?)?病历号 ?N?(或?n?)?s?(或?s?) ?x?(其它字符) 输出描述: 病历号为×的病人就诊 若队列中有数据,输出队首元素,否则输出"无病人就诊”不再接收病人排队,并输出当前队列中所有病历号 输入命令不合法!
?
?
#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;
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;
}
}
样例太少,无法通过oj
|