数据结构—线性实习题目(二)4

患者到医院看病模拟(队列)

数据结构—线性实习题目(二)4_第1张图片数据结构—线性实习题目(二)4_第2张图片

#include 
using namespace std;
 
template 
struct LinkNode
{
	T data;
	LinkNode* link;
	LinkNode() {
		link = NULL;
	}
	LinkNode(const T& item) {
		data = item;
		link = NULL;
	}
};
 
template 
class Queue {
private:
	LinkNode* const first = new LinkNode();
	int length;
public:
	Queue() {
		length = 0;
	}
 
	bool Insert(T& a) {
		LinkNode* newnode = new LinkNode(a);
		LinkNode* 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* newnode = new LinkNode(a);
		LinkNode* 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* 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* 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 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;
	}
}

你可能感兴趣的:(数据结构,数据结构,算法,c++)