对列类模板的实现

#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
template<typename T>
struct quende {
    
    
	T nodedata;
	quende* next;
};
template<typename T>
class queue {
    
    
private:
protected:
	int quesize;
	quende<T>* head;
	quende<T>* tail;
	bool allocateerror;
	queue& copy(queue& q);
public:
	queue();
	queue(queue& q) {
    
    
		head = NULL;
		tail = NULL;
		copy(q);
	}
	~queue() {
    
    
		clearque();
	}
	bool getallocateerror() {
    
    
		return allocateerror;
	}
	void push(T&);
	bool pop(T&);
	bool isempty() {
    
    
		return (quesize == 0) ? true : false;
	}
	void clearque();
	queue& operator=(queue& q) {
    
    
		copy(q);
		return *this;
	}
};
template<typename T>
queue<T>::queue() {
    
    
	quesize = 0;
	allocateerror = false;
	head = NULL;
	tail = NULL;
}
template<typename T>
queue<T>& queue<T>::copy(queue<T>& que) {
    
    
	quende<T>* p, * q, * r;
	if (head)
		clearque();
	quesize = que.quesize;
	allocateerror = false;
	head = NULL;
	tail = NULL;
	if (!que.head)
		return *this;///返回当前的this指针指向的对象
	head = new quende<T>;
	if (!head) {
    
        ///判断是否分配成功
		allocateerror = true;
		return *this;
	}
	head->nodedata = que.head->nodedata;
	head->next = NULL;
	tail = head;
	r = NULL;
	p = head;
	q = que.head->next;
	while (q) {
    
    
		r = new quende<T>;
		if (!r) {
    
    
			allocateerror = true;
			return *this;
		}
		r->nodedata = q->nodedata;
		r->next = NULL;
		p->next = r;
		tail = r;
		p = p->next;
		q = q->next;
	}
	return *this;
}
template<typename T>
void queue<T>::push(T& x) {
    
    
	quende<T>* p;
	p = new quende<T>;
	if (!p) {
    
    
		allocateerror = true;
		return;
	}
	p->nodedata = x;
	p->next = NULL;
	if (tail) {
    
    
		tail->next = p;
		tail = p;
	}
	else {
    
    
		tail = p;
		head = p;
	}
	quesize++;
	return;
}
template<typename T>
bool queue<T>::pop(T& x) {
    
    
	quende<T>* p;
	if (!head)
		return false;
	x = head->nodedata;
	p = head;
	head = p->next;
	if (head == NULL)
		tail = NULL;
	quesize--;
	delete p;
	return true;
}
template<typename T>
void queue<T>::clearque() {
    
    
	T p;
	allocateerror = false;
	while (pop(p));
	head = tail = NULL;
}
class staff {
    
    
public:
	char name[80];
	int age;
	float salary;
	char sex[8];
	void assign(char* name, int age, float salary, char* sex) {
    
    
		strcpy_s(staff::name, name);
		staff::age = age;
		staff::salary = salary;
		strcpy_s(staff::sex, sex);
	}
	void print() {
    
    
		printf("%10s%6d%10.2f%8s\n", name, age, salary, sex);
	}
};
void viewque(queue<staff>& que) {
    
    
	int i = 1;
	staff p;
	queue<staff>quecopy(que);///创建对象
	system("cls");
	while (quecopy.pop(p)) {
    
    
		printf("%2d:", i++);
		p.print();
	}
}
int main() {
    
    
	queue<staff>que;
	staff p;
	p.assign((char*)"chenweilin", 47, 1500, (char*)"male");
	que.push(p);
	p.assign((char*)"wangling", 34, 850.5, (char*)"male");
	que.push(p);
	p.assign((char*)"Zhengdaling", 27, 1200, (char*)"male");
	que.push(p);
	p.assign((char*)"Fanglibida", 51, 2000, (char*)"female");
	que.push(p);
	viewque(que);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_43840681/article/details/121062493