第十四章编程练习(3)

本文介绍了一个使用模板类实现的队列容器,并通过一个名为Worker的类实例化来展示其应用。通过实现队列操作如入队、出队、判断队列是否为空或满等功能,以及Worker类的属性设置和显示,详细阐述了如何将队列容器应用于工作员管理场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

建立一个容器类模板,然后使用Worker作为容器类参数
QUEUETP.h
#pragma once
#ifndef WORKERMI_H_
#define WORKERMI_H_
#include<string>
template<class T>//创建一个容器类模板
class QueueTP{
private:
	struct Node { T item; Node * next; };
	Node * front;
	Node * rear;
	int items;
	const int qsize;
	QueueTP(const QueueTP & q) :qsize(0) {};
	QueueTP & operator=(const QueueTP & q) { return *this; };
public:
	QueueTP(int qs = 10);
	~QueueTP();
	bool isempty()const;
	bool isfull()const;
	int queuecount()const;
	bool enqueue(const T & item);
	bool dequeue(T & item);
};
class Worker
{
private:
	std::string fullname;
	long id;
public:
	Worker() : fullname("no one"), id(0L) { }
	Worker(const std::string & s, long n)
		: fullname(s), id(n) { }
	~Worker() {};
	 void Set();
	 void Show()const;
};
template <class T>
QueueTP<T>::QueueTP(int qs):qsize(qs)
{
	front = rear = nullptr;
	items = 0;
}
template<class T>
inline QueueTP<T>::~QueueTP()
{
	Node * temp;
	while (front != nullptr)
	{
		temp = front;
		front = front->next;
		delete temp;
	}
	}
template<class T>
inline bool QueueTP<T>::isempty() const
{
	return items ==0;
}
template<class T>
inline bool QueueTP<T>::isfull() const
{
	return items == qsize;
}
template<class T>
inline int QueueTP<T>::queuecount() const
{
	return items;
}
template<class T>
inline bool QueueTP<T>::enqueue(const T & item)
{
	if (isfull())
		return false;
	Node * add = new Node;
	add->item = item;
	add->next = nullptr;
	items++;
	if (front == nullptr)
		front = add;
	else
		rear->next = add;
	rear = add;
	return true;
}
template<class T>
inline bool QueueTP<T>::dequeue(T & item)
{
	if (front == nullptr)
		return false;
	item = front->item;
	items--;
	Node * temp = front;
	front = front->next;
	delete temp;
	if (items == 0)
		rear = nullptr;
	return true;
};
;
#endif
QUEUETP.cpp
#include "QUEUETP.h"
#include <iostream>
void Worker::Set()
{
	using namespace std;
	cout << "Enter worker's name: ";
	getline(cin, fullname);
	cout << "Enter worker's ID: ";
	cin >> id;//获取换行符,不然会影响下一次输入
	while (cin.get() != '\n')
			continue;
}

void Worker::Show() const
{
	using std::cout;
	using std::endl;
	cout << "Name: " << fullname << endl;
	cout << "Employee ID: " << id << endl;
}

#include <iostream>
#include "QUEUETP.h"
const int Size = 5;
int main()
{
	using std::cout;
	using std::cin;
	using std::endl;
	QueueTP<Worker*>lolas(Size);
	Worker *temp;
	int n;
	char ch;
	cout << "Enter the command:\n"
		<< "A or a enter queue, "
		<< "P or p delete queue, "
		<< "Q or q quit.\n";
	while ((cin >> ch)&&ch != 'q' && ch != 'Q')
	{
		while (strchr("apqAPQ", ch) == nullptr)
		{
			cout << "Please enter a p or q: ";
			cin >> ch;
		}
		switch (ch)
		{
		case 'a':
		case 'A':
			temp = new Worker;
			cin.get();
			temp->Set();
			if(lolas.isfull())
				cout<< "Queue already full\n";
			else
				lolas.enqueue(temp);
			break;
		case'p':
		case'P':
			if (lolas.isempty())
				cout << "Queue already empty\n";
			else
				lolas.dequeue(temp);
			break;
		}
		cout << "Please enter a p or q: ";
	}
	cout << "\nHere the total count: ";
	cout << lolas.queuecount();
	cout << "\nDone\n"
		<< "Bye";
	cin.get();
	cin.get();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值