C++链表、队列、栈模板

本文介绍了如何使用C++创建链表、队列和栈的模板类,提供了相应的模板代码,并给出了测试用例。
摘要由CSDN通过智能技术生成

链表类模板:

//CList.h
#pragma once
#include<Windows.h>
template<class T>
class CNode {
public:
	CNode(T* tElement) :tElement{ tElement }, next{ 0 }{}
	T* Element()const { return tElement; }
	CNode*& Next() { return next; }
private:
	T* tElement;
	CNode* next;
};
template<class T>
class CList {
public:
	CList() :dwCount{ 0 }, head{ 0 }{}
	CList(T* tElement) :dwCount{ 1 }, head(new CNode<T>(tElement)){}
	virtual ~CList() {}
	void Append(CNode<T>*& node, T* tElement);
	void Insert(T* tElement);
	bool Remove(T* tElement);
	DWORD Count()const { return dwCount; }
	CNode<T>*& Head() { return head; }
	T* GetFirst(){ return head != NULL ? head->Element() : NULL; }
	T* GetLast();
	T* GetNext(T* tElement);
	T* Find(DWORD(*Function)(T* tParameter), DWORD dwValue);
protected:
	CList(const CList& list);
	CList& operator = (const CList& list);
private:
	CNode<T>* head;
	DWORD dwCount;
};
template<class T>
void CList<T>::Append(CNode<T>*&node, T* tElement) {
	if (node == NULL) {
		dwCount++;
		node = new CNode<T>(tElement);
		return;
	}
	Append(node->Next(), tElement);
}
template<class T>
void CList<T>::Insert(T* tElement) {
	dwCount++;
	if (head == NULL) {
		head = new CNode<T>(tElement);
		return;
	}
	CNode<T>* tmp = head;
	head = new CNode<T>(tElement);
	head->Next() = tmp;
}
template<class T>
bool CList<T>::Remove(T* tElement) {
	if (head == NULL) {
		return NULL;
	}
	if (head->Element() == tElement) {
		CNode<T>* tmp = head;
		head = head->Next();
		delete tmp;
		dwCount--;
		return true;
	}
	CNode<T> *tmp = head;
	CNode<T> *lst = head->Next();
	while (lst != NULL) {
		if (lst->Element() == tElement) {
			tmp->Next() = lst->Next();
			delete lst;
			dwCount--;
			return true;
		}
		lst = lst->Next();
		tmp = tmp->Next();
	}
	return false;
}
template<class T>
T* CList<T>::GetLast() {
	if (head) {
		CNode<T>* tmp = head;
		while (tmp->Next()) {
			tmp = tmp->Next();
		}
		return tmp->Element();
	}
	return NULL;
}
template<class T>
T* CList<T>::GetNext(T* tElement) {
	if (head == NULL) {
		return NULL;
	}
	if (tElement == NULL) {
		return GetFirst();
	}
	if (head->Element == tElement) {
		return head->Next() != NULL ? head->Next()->Element() : NULL;
	}
	CNode<T>* lst = head->Next();
	while (lst != NULL) {
		if (lst->Element() == tElement) {
			return lst->Next() != NULL ? lst->Next()->Element() : NULL;
		}
		lst = lst->Next();
	}
	return NULL;
}
template<class T>
T* CList<T>::Find(DWORD(*Function)(T* tParameter), DWORD dwValue) {
	try {
		T* tElement = NULL;
		while (tElement = GetNext(tElement)) {
			if (Function(tElement) == dwValue) {
				return tElement;
			}
		}
	}
	catch (...) {}
	return NULL;
}

队列模板:

//CQueue.h
#pragma once
#include"CList.h"
template<class T>
class CQueue :public CList<T> {
public:
	CQueue() :CList<T>() {}
	CQueue(T* tElement) :CList<T>(tElement) {}
	virtual ~CQueue() {}
	virtual void Enqueue(T* tElement) {
		CList<T>::Append(CList<T>::Head(), tElement);
	}
	virtual T* Dequeue() {
		T* tElement = CList<T>::GetFirst();
		CList<T>::Remove(tElement);
		return tElement;
	}
	virtual T* Peek() {
		return CList<T>::GetFirst();
	}
	CList<T>::Count;//直接声明基类的函数成员,使之可以在派生类中使用
protected:
	CQueue(const CQueue<T>& cQueue);
	CQueue<T>& operator = (const CQueue<T>& cQueue);
};

栈模板:

//CStack.h
#pragma once
#include"CList.h"
template<class T>
class CStack :CList<T> {
public:
	CStack() :CList<T>() {}
	CStack(T* tElement) :CList<T>(tElement) {}
	virtual ~CStack() {}
	virtual void Push(T* tElement) {
		CList<T>::Insert(tElement);
	}
	virtual T* Pop() {
		T* tElement = CList<T>::GetFirst();
		CList<T>::Remove(tElement);
		return tElement;
	}
	virtual T* Peek() {
		return CList<T>::GetFirst();
		
	}
	CList<T>::Count;
protected:
	CStack(const CStack<T>& cStack);
	CStack<T>& operator = (const CStack<T>& cStack);
};

以下为测试代码:

//main.cpp
#include<iostream>
using namespace std;
#include"CQueue.h"
#include"CStack.h"
int main()
{
	CQueue<int>* cQueue = new CQueue<int>();
	CStack<double>* cStack = new CStack<double>();
	for (int i = 0;i < 10;i++) {
		cQueue->Enqueue(new int(i));
		cStack->Push(new double(i / 10.0));
	}
	cout << "Queue - integer collection:" << endl;
	for (;cQueue->Count();) {
		cout << *cQueue->Dequeue() << " ";
	}
	cout << endl << endl << "Stack - double collection:" << endl;
	for (;cStack->Count();) {
		cout << *cStack->Pop() << " ";
	}
	delete cQueue;
	delete cStack;
	cout << endl << endl;
	return system("pause");
}
面向对象程序设计课程作业 1. 请创建一个数据型为T的链表类模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List型的模板对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List型的模板对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List型的模板对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List型的模板对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值