数据结构--二叉树构造模板

需要四个函数

#pragma once
#pragma once
#include<iostream>
#include"lk_ queue.h"
#include"lk_stack.h"
#include"node.h"
using namespace std;
template <class ElemType>
struct BinTreeNode
{
	// 数据成员:
	ElemType data;						// 数据成分
	BinTreeNode<ElemType>* leftChild;	// 左孩子指针成分
	BinTreeNode<ElemType>* rightChild;	// 右孩子指针成分

//	构造函数模板:
	BinTreeNode();		// 无参数的构造函数模板 
	BinTreeNode(const ElemType& ee,		// 已知数据元素值,指向左右孩子的指针构造一个结点
		BinTreeNode<ElemType>* lChild = NULL,
		BinTreeNode<ElemType>* rChild = NULL);
};

// 二叉树结点类模板的实现部分
template <class ElemType>
BinTreeNode<ElemType>::BinTreeNode()
// 操作结果:构造一个叶结点
{
	leftChild = rightChild = NULL;	// 叶结点左右孩子为空
}

template <class ElemType>
BinTreeNode<ElemType>::BinTreeNode(const ElemType& e,
	BinTreeNode<ElemType>* lChild, BinTreeNode<ElemType>* rChild)
	// 操作结果:构造一个数据成分为val,左孩子为lChild,右孩子为rChild的结点
{
	data = e;					// 数据元素值
	leftChild = lChild;			// 左孩子
	rightChild = rChild;		// 右孩子
}

// 二叉树类模板
template <class ElemType>
class BinaryTree
{
protected:
	// 数据成员:
	BinTreeNode<ElemType>* root;

	// 辅助函数模板:
	BinTreeNode<ElemType>* CopyTreeHelp(const BinTreeNode<ElemType>* r);// 复制二叉树
	void DestroyHelp(BinTreeNode<ElemType>*& r);						// 销毁以r为根二叉树
	void PreOrderHelp(BinTreeNode<ElemType>* r, void (*visit)(const ElemType&)) const;	// 先序遍历
	void InOrderHelp(BinTreeNode<ElemType>* r, void (*visit)(const ElemType&)) const;	// 中序遍历
	void PostOrderHelp(BinTreeNode<ElemType>* r, void (*visit)(const ElemType&)) const;// 后序遍历
	int HeightHelp(const BinTreeNode<ElemType>* r) const;	// 返回二叉树的高
	int NodeCountHelp(const BinTreeNode<ElemType>* r) const;// 返回二叉树的结点个数
	const BinTreeNode<ElemType>* ParentHelp(BinTreeNode<ElemType>* r,
		const BinTreeNode<ElemType>* cur) const;			// 返回cur的双亲

public:
	// 二叉树方法声明及重载编译系统默认方法声明:
	BinaryTree();											// 无参数的构造函数模板
	virtual ~BinaryTree();									// 析构函数
	const BinTreeNode<ElemType>* GetRoot() const;			// 返回二叉树的根
	bool Empty() const;										// 判断二叉树是否为空
	bool GetElem(const BinTreeNode<ElemType>* cur, ElemType& e) const;	// 用e返回结点元素值
	bool SetElem(BinTreeNode<ElemType>* cur, const ElemType& e);	// 将结点cur的值置为e
	void InOrder(void (*visit)(const ElemType&)) const;		// 二叉树的中序遍历	
	void PreOrder(void (*visit)(const ElemType&)) const;	// 二叉树的先序遍历
	void PostOrder(void (*visit)(const ElemType&)) const;	// 二叉树的后序遍历
	void LevelOrder(void (*visit)(const ElemType&))const;	//层次遍历二叉树
	void NonRecurPreOrder(void(*visit)(const ElemType&))const
	{
		//const BinTreeNode<ElemType>& bt;
		const BinTreeNode<ElemType>* cur = root;
		LinkStack<const BinTreeNode<ElemType>*>s;
		while (cur != NULL)
		{
			(*visit)(cur->data);
			s.Push(cur);
			if (cur->leftChild != NULL)
			{
				cur = cur->leftChild;
			}
			else if (!s.Empty())
			{
				while (!s.Empty())
				{
					s.Pop(cur);
					cur = cur->rightChild;
					if (cur != NULL)break;
				}
			}
			else
				cur = NULL;
		}
	}
	int NodeCount() const;									// 求二叉树的结点个数
	const BinTreeNode<ElemType>* LeftChild(const BinTreeNode<ElemType>* cur) const;
	// 返回二叉树结点cur的左孩子
	const BinTreeNode<ElemType>* RightChild(const BinTreeNode<ElemType>* cur) const;
	// 返回二叉树结点cur的右孩子
	const BinTreeNode<ElemType>* Parent(const BinTreeNode<ElemType>* cur) const;
	// 返回二叉树结点cur的双亲
	void InsertLeftChild(BinTreeNode<ElemType>* cur, const ElemType& e);// 插入左孩子
	void InsertRightChild(BinTreeNode<ElemType>* cur, const ElemType& e);// 插入右孩子
	void DeleteLeftChild(BinTreeNode<ElemType>* cur);		// 删除左子树
	void DeleteRightChild(BinTreeNode<ElemType>* cur);		// 删除右子村
	int	Height() const;										// 求二叉树的高
	BinaryTree(const ElemType& e);							// 建立以e为根的二叉树
	BinaryTree(const BinaryTree<ElemType>& source);			// 复制构造函数模板
	BinaryTree(BinTreeNode<ElemType>* r);					// 建立以r为根的二叉树
	BinaryTree<ElemType>& operator=(const BinaryTree<ElemType>& source);	// 重载赋值运算符
};

template <class ElemType>
void DisplayBTWithTreeShapeHelp(const BinTreeNode<ElemType>* r, int level);
//	按树状形式显示以r为根的二叉树,level为层次数,可设根结点的层次数为1
template <class ElemType>
void DisplayBTWithTreeShape(const BinaryTree<ElemType>& bt);
//	树状形式显示二叉树 
template <class ElemType>
void CreateBinaryTreeHelp(BinTreeNode<ElemType>*& r, ElemType pre[], ElemType in[],
	int preLeft, int preRight, int inLeft, int inRight);
// 已知二叉树的先序序列pre[preLeft..preRight]和中序序列in[inLeft..inRight]构造以r为根的
// 二叉树
template <class ElemType>
BinaryTree<ElemType> CreateBinaryTree(ElemType pre[], ElemType in[], int n);
// 已知先序和中序序列构造二叉树

// 二叉树类模板的实现部分
template <class ElemType>
BinaryTree<ElemType>::BinaryTree()
// 操作结果:构造一个空二叉树
{
	root = NULL;
}

template <class ElemType>
BinaryTree<ElemType>::~BinaryTree()
// 操作结果:销毁二叉树——析造函数
{
	DestroyHelp(root);
}

template <class ElemType>
const BinTreeNode<ElemType>* BinaryTree<ElemType>::GetRoot() const
// 操作结果:返回二叉树的根
{
	return root;
}

template <class ElemType>
bool BinaryTree<ElemType>::Empty() const
// 操作结果:判断二叉树是否为空
{
	return root == NULL;
}

template <class ElemType>
bool BinaryTree<ElemType>::GetElem(const BinTreeNode<ElemType>* cur, ElemType& e) const
// 操作结果:用e返回结点cur元素值,如果不存在结点cur,返回false,否则返回true
{
	if (cur == NULL)
	{	// 不存在结点cur
		return false;				// 返回false
	}
	else
	{	// 存在结点cur
		e = cur->data;				// 用e返回元素值
		return true;				// 返回true
	}
}

template <class ElemType>
bool BinaryTree<ElemType>::SetElem(BinTreeNode<ElemType>* cur, const ElemType& e)
// 操作结果:如果不存在结点cur,则返回false,否则返回true,并将结点cur的值设置为e
{
	if (cur == NULL)
	{	// 不存在结点cur
		return false;				// 返回false
	}
	else
	{	// 存在结点cur
		cur->data = e;				// 将结点cur的值设置为e
		return true;				// 返回true
	}
}

template<class ElemType>
inline void BinaryTree<ElemType>::InOrder(void(*visit)(const ElemType&)) const
{
	InOrderHelp(root, visit);
}

template<class ElemType>
inline void BinaryTree<ElemType>::PreOrder(void(*visit)(const ElemType&)) const
{
	PreOrderHelp(root, visit);
}

template<class ElemType>
inline void BinaryTree<ElemType>::PostOrder(void(*visit)(const ElemType&)) const
{
	PostOrderHelp(root, visit);
}

template<class ElemType>
inline void BinaryTree<ElemType>::LevelOrder(void(*visit)(const ElemType&)) const
{
	LinkQueue<const BinTreeNode<ElemType>* >q;
	const BinTreeNode<ElemType>* cur;
	if (root != NULL) q.InQueue(root);
	while (!q.Empty())
	{
		q.OutQueue(cur);
		(*visit)(cur->data);
		if (cur->leftChild != NULL)
			q.InQueue(cur->leftChild);
		if (cur->rightChild != NULL)
			q.InQueue(cur->rightChild);
	}
}

template<class ElemType>
inline int BinaryTree<ElemType>::NodeCount() const
{
	return NodeCountHelp(root);
}

template <class ElemType>
BinaryTree<ElemType>::BinaryTree(const ElemType& e)
// 操作结果:建立以e为根的二叉树
{
	root = new BinTreeNode<ElemType>(e);
}

template <class ElemType>
const BinTreeNode<ElemType>* BinaryTree<ElemType>::LeftChild(const BinTreeNode<ElemType>* cur) const
// 操作结果:返回二叉树结点cur的左孩子
{
	return cur->leftChild;
}

template <class ElemType>
const BinTreeNode<ElemType>* BinaryTree<ElemType>::RightChild(const BinTreeNode<ElemType>* cur) const
// 操作结果:返回二叉树结点cur的右孩子
{
	return cur->rightChild;
}

template <class ElemType>
const BinTreeNode<ElemType>* BinaryTree<ElemType>::ParentHelp(BinTreeNode<ElemType>* r, const BinTreeNode<ElemType>* cur) const
// 操作结果:返回以r为根的二叉树, 结点cur的双亲
{
	if (r == NULL) return NULL;		// 空二叉树
	else if (r->leftChild == cur || r->rightChild == cur) return r; // r为cur的双亲
	else
	{	// 在子树上求双亲
		const BinTreeNode<ElemType>* temPtr;	// 临时指针

		temPtr = ParentHelp(r->leftChild, cur);	// 在左子树上求cur的双亲	
		if (temPtr != NULL) return temPtr;		// 双亲在左子树上

		temPtr = ParentHelp(r->rightChild, cur);// 在右子树上求cur的双亲	
		if (temPtr != NULL) return temPtr;		// 双亲在右子树上
		else return NULL;						// 表示cur无双亲
	}
}

template <class ElemType>
const BinTreeNode<ElemType>* BinaryTree<ElemType>::Parent(const BinTreeNode<ElemType>* cur) const
// 操作结果:返回二叉树结点cur的双亲
{
	return ParentHelp(root, cur);
}

template <class ElemType>
void BinaryTree<ElemType>::InsertLeftChild(BinTreeNode<ElemType>* cur, const ElemType& e)
// 初始条件:cur非空,
// 操作结果:插入元素值为e的结点为cur的左孩子,如果cur的左孩子非空,则cur原有左子树成为e的左子树
{
	if (cur == NULL)
	{	// cur空,返回
		return;
	}
	else
	{	// 插入左孩子
		BinTreeNode<ElemType>* child = new BinTreeNode<ElemType>(e);// 元素值为e结点
		if (cur->leftChild != NULL)
		{	// cur的左孩子非空
			child->leftChild = cur->leftChild;	// cur原有左子树成为e的左子树
		}
		cur->leftChild = child;					// e成为cur的左孩子
		return;
	}
}

template <class ElemType>
void BinaryTree<ElemType>::InsertRightChild(BinTreeNode<ElemType>* cur, const ElemType& e)
// 初始条件:cur非空
// 操作结果:插入元素值为e的结点为cur的右孩子,如果cur的右孩子非空,则cur原有右子树成为e的右子树
{
	if (cur == NULL)
	{	// cur为空,返回
		return;
	}
	else
	{	// 插入右孩子
		BinTreeNode<ElemType>* child = new BinTreeNode<ElemType>(e);// 元素值为e结点
		if (cur->rightChild != NULL)
		{	// cur的右孩子非空
			child->rightChild = cur->rightChild;	// cur原有右子树成为e的右子树
		}
		cur->rightChild = child;					// e成为cur的右孩子
		return;
	}
}

template <class ElemType>
void BinaryTree<ElemType>::DeleteLeftChild(BinTreeNode<ElemType>* cur)
// 初始条件:cur非空
// 操作结果:删除cur左子树
{
	if (cur == NULL)
	{	// cur为空
		return;
	}
	else
	{	// cur非空
		DestroyHelp(cur->leftChild);	// 删除cur左子树
	}
}

template <class ElemType>
void BinaryTree<ElemType>::DeleteRightChild(BinTreeNode<ElemType>* cur)
// 初始条件:cur非空
// 操作结果:删除cur右子树
{
	if (cur == NULL)
	{	// cur为空
		return;
	}
	else
	{	// cur非空
		DestroyHelp(cur->rightChild);	// 删除cur右子树
	}
}

template<class ElemType>
inline int BinaryTree<ElemType>::Height() const
{
	return HeightHelp(root);
}

template <class ElemType>
BinTreeNode<ElemType>* BinaryTree<ElemType>::CopyTreeHelp(const BinTreeNode<ElemType>* r)
// 操作结果:将以r为根的二叉树复制成新的二叉树,回新二叉树的根
{
	if (r == NULL)
	{	// 复制空二叉树
		return NULL;					// 空二叉树根为空	
	}
	else
	{	// 复制非空二叉树
		BinTreeNode<ElemType>* lChild = CopyTreeHelp(r->leftChild);		// 复制左子树
		BinTreeNode<ElemType>* rChild = CopyTreeHelp(r->rightChild);	// 复制右子树
		BinTreeNode<ElemType>* rt = new BinTreeNode<ElemType>(r->data, lChild, rChild);
		// 复制根结点
		return rt;
	}
}

template<class ElemType>
inline void BinaryTree<ElemType>::DestroyHelp(BinTreeNode<ElemType>*& r)
{
	if (r != NULL)
	{
		DestroyHelp(r->leftChild);
		DestroyHelp(r->rightChild);
		delete r;
		r = NULL;
	}
}

template <class ElemType>
BinaryTree<ElemType>::BinaryTree(const BinaryTree<ElemType>& source)
// 操作结果:由已知二叉树构造新二叉树——复制构造函数模板
{
	root = CopyTreeHelp(source.root);	// 复制二叉树
}

template <class ElemType>
BinaryTree<ElemType>::BinaryTree(BinTreeNode<ElemType>* r)
// 操作结果:建立以r为根的二叉树
{
	root = r;	// 复制二叉树
}


template<class ElemType>
inline void BinaryTree<ElemType>::PreOrderHelp(BinTreeNode<ElemType>* r, void(*visit)(const ElemType&)) const
{
	if (r != NULL)
	{
		(*visit)(r->data);
		PreOrderHelp(r->leftChild, visit);
		PreOrderHelp(r->rightChild, visit);
	}
}

template<class ElemType>
void BinaryTree<ElemType>::InOrderHelp(BinTreeNode<ElemType>* r, void (*visit)(const ElemType&))const
{
	if (r != NULL)
	{
		InOrderHelp(r->leftChild, visit);
		(*visit)(r->data);
		InOrderHelp(r->rightChild, visit);
	}
}

template<class ElemType>
inline void BinaryTree<ElemType>::PostOrderHelp(BinTreeNode<ElemType>* r, void(*visit)(const ElemType&)) const
{
	if (r != NULL)
	{
		PostOrderHelp(r->leftChild, visit);
		PostOrderHelp(r->rightChild, visit);
		(*visit)(r->data);
	}
}

template<class ElemType>
inline int BinaryTree<ElemType>::HeightHelp(const BinTreeNode<ElemType>* r) const
{
	if (r == NULL)
		return 0;
	else
	{
		int lHeight = HeightHelp(r->leftChild);
		int rHeight = HeightHelp(r->rightChild);
		return (lHeight > rHeight ? lHeight : rHeight) + 1;
	}
}
template<class ElemType>
inline int BinaryTree<ElemType>::NodeCountHelp(const BinTreeNode<ElemType>* r) const
{
	if (r == NULL)
		return 0;
	else return NodeCountHelp(r->leftChild) + NodeCountHelp(r->rightChild) + 1;
}
template <class ElemType>
BinaryTree<ElemType>& BinaryTree<ElemType>::operator=(const BinaryTree<ElemType>& source)
// 操作结果:由已知二叉树source复制到当前二叉树——重载赋值运算符
{
	if (&source != this)
	{
		DestroyHelp(root);				// 释放原二叉树所占用空间
		root = CopyTreeHelp(source.root);	// 复制二叉树
	}
	return *this;
}


template <class ElemType>
void DisplayBTWithTreeShapeHelp(const BinTreeNode<ElemType>* r, int level)
// 操作结果:按树状形式显示以r为根的二叉树,level为层次数,可设根结点的层次数为1
{
	if (r != NULL)
	{	// 空树不显式,只显式非空树
		DisplayBTWithTreeShapeHelp<ElemType>(r->rightChild, level + 1);//显示右子树
		cout << endl;					//显示新行	
		for (int temPos = 0; temPos < level - 1; temPos++)
			cout << " ";				//确保在第level列显示结点
		cout << r->data;				//显示结点
		DisplayBTWithTreeShapeHelp<ElemType>(r->leftChild, level + 1);//显示左子树
	}
}

template <class ElemType>
void DisplayBTWithTreeShape(const BinaryTree<ElemType>& bt)
// 操作结果:树状形式显示二叉树 
{
	DisplayBTWithTreeShapeHelp<ElemType>(bt.GetRoot(), 1);
	// 树状显示以bt.GetRoot()为根的二叉树
	cout << endl;
}

template <class ElemType>
void CreateBinaryTreeHelp(BinTreeNode<ElemType>*& r, ElemType pre[], ElemType in[],
	int preLeft, int preRight, int inLeft, int inRight)
	// 操作结果:已知二叉树的先序序列pre[preLeft..preRight]和中序序列in[inLeft..inRight]构造
	//	以r为根的二叉树
{
	if (preLeft > preRight || inLeft > inRight)
	{	// 二叉树无结点,空二叉树
		r = NULL;									// 空二叉树根为空
	}
	else
	{	// 二叉树有结点,非空二叉树
		r = new BinTreeNode<ElemType>(pre[preLeft]);// 生成根结点
		int mid = inLeft;							// mid为pre[preLeft]在in[]中的位置
		while (in[mid] != pre[preLeft])
		{	// 查找pre[preLeft]在in[]中的位置,也就是中序序列中根的位置
			mid++;
		}
		CreateBinaryTreeHelp(r->leftChild, pre, in, preLeft + 1, preLeft + mid - inLeft, inLeft, mid - 1);
		// 生成左子树
		CreateBinaryTreeHelp(r->rightChild, pre, in, preLeft + mid - inLeft + 1, preRight, mid + 1,
			inRight);								// 生成右子树
	}
}

template <class ElemType>
BinaryTree<ElemType> CreateBinaryTree(ElemType pre[], ElemType in[], int n)
// 操作结果:已知先序和中序序列构造二叉树
{
	BinTreeNode<ElemType>* r;						// 二叉树的根
	CreateBinaryTreeHelp<ElemType>(r, pre, in, 0, n - 1, 0, n - 1);
	// 由先序和中序序列构造以r为根的二叉树 
	return BinaryTree<ElemType>(r);					// 返回以r为根的二叉树
}
lk_ queue.h
#pragma once
#include "node.h"	

// 链队列类模板

template<class ElemType>
class LinkQueue
{
protected:
	// 数据成员:
	Node<ElemType>* front, * rear;										// 队头队尾指针
	int count;															// 元素个数

public:
	// 抽象数据类型方法声明及重载编译系统默认方法声明:
	LinkQueue();														// 无参数的构造函数模板
	virtual ~LinkQueue();												// 析构函数模板
	int Length() const;													// 求队列长度
	bool Empty() const;													// 判断队列是否为空
	void Clear();														// 将队列清空
	void Traverse(void (*visit)(const ElemType&)) const;				// 遍历队列
	bool OutQueue(ElemType& e);											// 出队操作
	bool OutQueue();													// 出队操作
	bool GetHead(ElemType& e) const;									// 取队头操作
	bool InQueue(const ElemType& e);									// 入队操作
	LinkQueue(const LinkQueue<ElemType>& source);						// 复制构造函数模板
	LinkQueue<ElemType>& operator =(const LinkQueue<ElemType>& source);	// 重载赋值运算符
};

// 链队列类模板的实现部分

template<class ElemType>
LinkQueue<ElemType>::LinkQueue()
// 操作结果:构造一个空队列
{
	rear = front = new Node<ElemType>;									// 生成头结点
	count = 0;															// 初始化元素个数
}

template<class ElemType>
LinkQueue<ElemType>::~LinkQueue()
// 操作结果:销毁队列
{
	Clear();															// 清空队列
	delete front;														// 释放头结点所占空间
}

template<class ElemType>
int LinkQueue<ElemType>::Length() const
// 操作结果:返回队列长度			 
{
	return count;														// count表示队列元素个数
}

template<class ElemType>
bool LinkQueue<ElemType>::Empty() const
// 操作结果:如队列为空,则返回true,否则返回false
{
	return count == 0;													// count == 0表示队列为空
}

template<class ElemType>
void LinkQueue<ElemType>::Clear()
// 操作结果:清空队列
{
	while (!Empty())
	{	// 队列非空,则出列
		OutQueue();														// 出列
	}
}

template <class ElemType>
void LinkQueue<ElemType>::Traverse(void (*visit)(const ElemType&)) const
// 操作结果:依次对队列的每个元素调用函数(*visit)
{
	for (Node<ElemType>* temPtr = front->next; temPtr != NULL; temPtr = temPtr->next)
	{	// 对队列的每个元素调用函数(*visit)
		(*visit)(temPtr->data);
	}
}


template<class ElemType>
bool LinkQueue<ElemType>::OutQueue(ElemType& e)
// 操作结果:如果队列非空,那么删除队头元素,并用e返回其值,返回true,
//	否则返回false
{
	if (!Empty())
	{	// 队列非空
		Node<ElemType>* temPtr = front->next;							// 指向队列头素
		e = temPtr->data;												// 用e返回队头元素
		front->next = temPtr->next;									// front->next指向下一元素
		if (rear == temPtr)
		{	// 表示出队前队列中只有一个元素,出队后为空队列
			rear = front;
		}
		delete temPtr;													// 释放出队的结点
		count--;														// 出队成功后元素个数自减1
		return true;													// 成功
	}
	else
	{	// 队列为空
		return false;													// 失败
	}
}

template<class ElemType>
bool LinkQueue<ElemType>::OutQueue()
// 操作结果:如果队列非空,那么删除队头元素,返回true,
//	否则返回false
{
	if (!Empty())
	{	// 队列非空
		Node<ElemType>* temPtr = front->next;							// 指向队列头素
		front->next = temPtr->next;									// front->next指向下一元素
		if (rear == temPtr)
		{	// 表示出队前队列中只有一个元素,出队后为空队列
			rear = front;
		}
		delete temPtr;													// 释放出队的结点
		count--;														// 出队成功后元素个数自减1
		return true;													// 成功
	}
	else
	{	// 队列为空
		return false;													// 失败
	}
}

template<class ElemType>
bool LinkQueue<ElemType>::GetHead(ElemType& e) const
// 操作结果:如果队列非空,那么用e返回队头元素,返回true,
//	否则返回false
{
	if (!Empty())
	{	// 队列非空
		Node<ElemType>* temPtr = front->next;							// 指向队列头素
		e = temPtr->data;												// 用e返回队头元素
		return true;													// 成功
	}
	else
	{	// 队列为空
		return false;													// 失败
	}
}

template<class ElemType>
bool LinkQueue<ElemType>::InQueue(const ElemType& e)
// 操作结果:插入元素e为新的队尾,插入成功true,否则返回false
{
	Node<ElemType>* temPtr = new Node<ElemType>(e);					// 生成新结点
	if (temPtr == NULL)
	{	// 动态内存耗尽
		return false;													// 失败
	}
	else
	{	// 操作成功
		rear->next = temPtr;											// 新结点追加在队尾
		rear = temPtr;													// rear指向新队尾
		count++;														// 入队成功后元素个数加1 
		return true;													// 成功
	}
}

template<class ElemType>
LinkQueue<ElemType>::LinkQueue(const LinkQueue<ElemType>& source)
// 操作结果:由队列source构造新队列——复制构造函数模板
{
	rear = front = new Node<ElemType>;	// 生成头结点
	count = 0;															// 初始化元素个数
	for (Node<ElemType>* temPtr = source.front->next; temPtr != NULL; temPtr = temPtr->next)
	{	// 对source队列的每个元素对当前队列作入队列操作
		InQueue(temPtr->data);
	}
}

template<class ElemType>
LinkQueue<ElemType>& LinkQueue<ElemType>::operator =(const LinkQueue<ElemType>& source)
// 操作结果:将队列source赋值给当前队列——重载赋值运算符
{
	if (&source != this)
	{
		Clear();														// 清空当前队列 
		for (Node<ElemType>* temPtr = source.front->next; temPtr != NULL; temPtr = temPtr->next)
		{	// 对source队列的每个元素对当前队列作入队列操作
			InQueue(temPtr->data);
		}
	}
	return *this;
}

 

lk_stack.h:
#pragma once
#include "node.h"	

// 链栈类模板
template<class ElemType>
class LinkStack
{
protected:
	// 数据成员:
	Node<ElemType>* top;												// 栈顶指针
	int count;															// 元素个数

public:
	// 抽象数据类型方法声明及重载编译系统默认方法声明:
	LinkStack();														// 无参数的构造函数模板
	virtual ~LinkStack();												// 析构函数模板
	int Length() const;													// 求栈长度			 
	bool Empty() const;													// 判断栈是否为空
	void Clear();														// 将栈清空
	void Traverse(void (*visit)(const ElemType&)) const;				// 遍历栈
	bool Push(const ElemType& e);										// 入栈
	bool Top(ElemType& e) const;										// 返回栈顶元素
	bool Pop(ElemType& e);												// 出栈
	bool Pop();															// 出栈
	LinkStack(const LinkStack<ElemType>& source);						// 复制构造函数模板
	LinkStack<ElemType>& operator =(const LinkStack<ElemType>& source);	// 重载赋值运算符
};


// 链栈类模板的实现部分

template<class ElemType>
LinkStack<ElemType>::LinkStack()
// 操作结果:构造一个空栈表
{
	top = NULL;															// 构造栈顶指针
	count = 0;															// 初始化元素个数
}

template<class ElemType>
LinkStack<ElemType>::~LinkStack()
// 操作结果:销毁栈
{
	Clear();															// 清空栈
}

template <class ElemType>
int LinkStack<ElemType>::Length() const
// 操作结果:返回栈元素个数
{
	return count;														// count表示栈元素个数
}

template<class ElemType>
bool LinkStack<ElemType>::Empty() const
// 操作结果:如栈为空,则返回true,否则返回false
{
	return count == 0;													// count == 0表示栈为空
}

template<class ElemType>
void LinkStack<ElemType>::Clear()
// 操作结果:清空栈
{
	while (!Empty())
	{	// 表栈非空,则出栈
		Pop();															// 出栈
	}
}

template <class ElemType>
void LinkStack<ElemType>::Traverse(void (*visit)(const ElemType&)) const
// 操作结果:从栈底到栈顶依次对栈的每个元素调用函数(*visit)
{
	Node<ElemType>* temPtr;												// 临时指针变量
	LinkStack<ElemType> temS;											// 临时栈,temS中元素顺序与当前栈元素顺序相反
	for (temPtr = top; temPtr != NULL; temPtr = temPtr->next)
	{	// 用temPtr依次指向当前栈的每个元素
		temS.Push(temPtr->data);										// 对当前栈的每个元素入栈到temS中
	}

	for (temPtr = temS.top; temPtr != NULL; temPtr = temPtr->next)
	{	// 用temPtr从栈顶到栈底依次指向栈temS的每个元素
		(*visit)(temPtr->data);											// 对栈temS的每个元素调用函数(*visit)
	}
}

template<class ElemType>
bool LinkStack<ElemType>::Push(const ElemType& e)
// 操作结果:将元素e追加到栈顶,如成功则返加true,否则如动态内存已耗尽
//	将返回false
{
	Node<ElemType>* newTop = new Node<ElemType>(e, top);
	if (newTop == NULL)
	{	// 动态内存耗尽
		return false;													// 失败
	}
	else
	{	// 操作成功
		top = newTop;
		count++;														// 入栈成功后元素个数加1 
		return true;													// 成功
	}
}

template<class ElemType>
bool LinkStack<ElemType>::Top(ElemType& e) const
// 操作结果:如栈非空,用e返回栈顶元素,返回true,否则返回false
{
	if (Empty())
	{	// 栈空
		return false;													// 失败
	}
	else
	{	// 栈非空,操作成功
		e = top->data;													// 用e返回栈顶元素
		return true;													// 成功
	}
}

template<class ElemType>
bool LinkStack<ElemType>::Pop(ElemType& e)
// 操作结果:如栈非空,删除栈顶元素,并用e返回栈顶元素,返回true,否则
//	返回false
{
	if (Empty())
	{	// 栈空
		return false;													// 失败
	}
	else
	{	// 操作成功
		Node<ElemType>* oldTop = top;									// 旧栈顶
		e = oldTop->data;												// 用e返回栈顶元素
		top = oldTop->next;												// top指向新栈顶
		delete oldTop;													// 删除旧栈顶
		count--;														// 出栈成功后元素个数自减1
		return true;													// 功能
	}
}

template<class ElemType>
bool LinkStack<ElemType>::Pop()
// 操作结果:如栈非空,删除栈顶元素,返回true,否则返回false
{
	if (Empty())
	{	// 栈空
		return false;													// 失败
	}
	else
	{	// 操作成功
		Node<ElemType>* oldTop = top;									// 旧栈顶
		top = oldTop->next;												// top指向新栈顶
		delete oldTop;													// 删除旧栈顶
		count--;														// 出栈成功后元素个数自减1
		return true;													// 功能
	}
}

template<class ElemType>
LinkStack<ElemType>::LinkStack(const LinkStack<ElemType>& source)
// 操作结果:由栈source构造新栈——复制构造函数模板
{
	if (source.Empty())
	{	// source为空
		top = NULL;														// 构造栈顶指针
		count = 0;														// 初始化元素个数
	}
	else
	{	// source非空,复制栈
		top = new Node<ElemType>(source.top->data);						// 生成当前栈项
		count = source.count;											// 栈元素个数
		Node<ElemType>* buttomPtr = top;								// 当前栈底指针
		for (Node<ElemType>* temPtr = source.top->next; temPtr != NULL; temPtr = temPtr->next)
		{	// 用temPtr依次指向其余元素
			buttomPtr->next = new Node<ElemType>(temPtr->data);			// 向栈底追加元素	
			buttomPtr = buttomPtr->next;								// buttomPtr指向新栈底
		}
	}
}


template<class ElemType>
LinkStack<ElemType>& LinkStack<ElemType>::operator = (const LinkStack<ElemType>& source)
// 操作结果:将栈source赋值给当前栈——重载赋值运算符
{
	if (&source != this)
	{
		if (source.Empty())
		{	// source为空
			top = NULL;													// 构造栈顶指针
			count = 0;													// 初始化元素个数
		}
		else
		{	// source非空,复制栈
			Clear();													// 清空当前栈
			top = new Node<ElemType>(source.top->data);					// 生成当前栈项
			count = source.count;										// 栈元素个数
			Node<ElemType>* buttomPtr = top;							// 当前栈底指针
			for (Node<ElemType>* temPtr = source.top->next; temPtr != NULL; temPtr = temPtr->next)
			{	// 用temPtr依次指向其余元素
				buttomPtr->next = new Node<ElemType>(temPtr->data);		// 向栈底追加元素	
				buttomPtr = buttomPtr->next;							// buttomPtr指向新栈底
			}
		}
	}
	return *this;
}
Node.h:
#pragma once
// 结点类模板
template <class ElemType>
struct Node
{
	// 数据成员:
	ElemType data;				// 数据成分
	Node<ElemType>* next;		// 指针成分

// 构造函数模板:
	Node();						// 无参数的构造函数模板
	Node(const ElemType& e, Node<ElemType>* link = NULL);	// 已知数据元素值和指针建立结点
};

// 结点类模板的实现部分
template<class ElemType>
Node<ElemType>::Node()
// 操作结果:构造指针成分为空的结点
{
	next = NULL;
}

template<class ElemType>
Node<ElemType>::Node(const ElemType& e, Node<ElemType>* link)
// 操作结果:构造一个数据成分为e和指针成分为link的结点
{
	data = e;
	next = link;
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值