需要四个函数
#pragma once #pragma once #include#include"lk_ queue.h" #include"lk_stack.h" #include"node.h" using namespace std; template struct BinTreeNode { // 数据成员: ElemType data; // 数据成分 BinTreeNode * leftChild; // 左孩子指针成分 BinTreeNode * rightChild; // 右孩子指针成分 // 构造函数模板: BinTreeNode(); // 无参数的构造函数模板 BinTreeNode(const ElemType& ee, // 已知数据元素值,指向左右孩子的指针构造一个结点 BinTreeNode * lChild = NULL, BinTreeNode * rChild = NULL); }; // 二叉树结点类模板的实现部分 template BinTreeNode ::BinTreeNode() // 操作结果:构造一个叶结点 { leftChild = rightChild = NULL; // 叶结点左右孩子为空 } template BinTreeNode ::BinTreeNode(const ElemType& e, BinTreeNode * lChild, BinTreeNode * rChild) // 操作结果:构造一个数据成分为val,左孩子为lChild,右孩子为rChild的结点 { data = e; // 数据元素值 leftChild = lChild; // 左孩子 rightChild = rChild; // 右孩子 } // 二叉树类模板 template class BinaryTree { protected: // 数据成员: BinTreeNode * root; // 辅助函数模板: BinTreeNode * CopyTreeHelp(const BinTreeNode * r);// 复制二叉树 void DestroyHelp(BinTreeNode *& r); // 销毁以r为根二叉树 void PreOrderHelp(BinTreeNode * r, void (*visit)(const ElemType&)) const; // 先序遍历 void InOrderHelp(BinTreeNode * r, void (*visit)(const ElemType&)) const; // 中序遍历 void PostOrderHelp(BinTreeNode * r, void (*visit)(const ElemType&)) const;// 后序遍历 int HeightHelp(const BinTreeNode * r) const; // 返回二叉树的高 int NodeCountHelp(const BinTreeNode * r) const;// 返回二叉树的结点个数 const BinTreeNode * ParentHelp(BinTreeNode * r, const BinTreeNode * cur) const; // 返回cur的双亲 public: // 二叉树方法声明及重载编译系统默认方法声明: BinaryTree(); // 无参数的构造函数模板 virtual ~BinaryTree(); // 析构函数 const BinTreeNode * GetRoot() const; // 返回二叉树的根 bool Empty() const; // 判断二叉树是否为空 bool GetElem(const BinTreeNode * cur, ElemType& e) const; // 用e返回结点元素值 bool SetElem(BinTreeNode * 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 & bt; const BinTreeNode * cur = root; linkStack *>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 * LeftChild(const BinTreeNode * cur) const; // 返回二叉树结点cur的左孩子 const BinTreeNode * RightChild(const BinTreeNode * cur) const; // 返回二叉树结点cur的右孩子 const BinTreeNode * Parent(const BinTreeNode * cur) const; // 返回二叉树结点cur的双亲 void InsertLeftChild(BinTreeNode * cur, const ElemType& e);// 插入左孩子 void InsertRightChild(BinTreeNode * cur, const ElemType& e);// 插入右孩子 void DeleteLeftChild(BinTreeNode * cur); // 删除左子树 void DeleteRightChild(BinTreeNode * cur); // 删除右子村 int Height() const; // 求二叉树的高 BinaryTree(const ElemType& e); // 建立以e为根的二叉树 BinaryTree(const BinaryTree & source); // 复制构造函数模板 BinaryTree(BinTreeNode * r); // 建立以r为根的二叉树 BinaryTree & operator=(const BinaryTree & source); // 重载赋值运算符 }; template void DisplayBTWithTreeShapeHelp(const BinTreeNode * r, int level); // 按树状形式显示以r为根的二叉树,level为层次数,可设根结点的层次数为1 template void DisplayBTWithTreeShape(const BinaryTree & bt); // 树状形式显示二叉树 template void CreateBinaryTreeHelp(BinTreeNode *& r, ElemType pre[], ElemType in[], int preLeft, int preRight, int inLeft, int inRight); // 已知二叉树的先序序列pre[preLeft..preRight]和中序序列in[inLeft..inRight]构造以r为根的 // 二叉树 template BinaryTree CreateBinaryTree(ElemType pre[], ElemType in[], int n); // 已知先序和中序序列构造二叉树 // 二叉树类模板的实现部分 template BinaryTree ::BinaryTree() // 操作结果:构造一个空二叉树 { root = NULL; } template BinaryTree ::~BinaryTree() // 操作结果:销毁二叉树——析造函数 { DestroyHelp(root); } template const BinTreeNode * BinaryTree ::GetRoot() const // 操作结果:返回二叉树的根 { return root; } template bool BinaryTree ::Empty() const // 操作结果:判断二叉树是否为空 { return root == NULL; } template bool BinaryTree ::GetElem(const BinTreeNode * 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 bool BinaryTree ::SetElem(BinTreeNode * 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 inline void BinaryTree ::InOrder(void(*visit)(const ElemType&)) const { InOrderHelp(root, visit); } template inline void BinaryTree ::PreOrder(void(*visit)(const ElemType&)) const { PreOrderHelp(root, visit); } template inline void BinaryTree ::PostOrder(void(*visit)(const ElemType&)) const { PostOrderHelp(root, visit); } template inline void BinaryTree ::LevelOrder(void(*visit)(const ElemType&)) const { linkQueue * >q; const BinTreeNode * 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 inline int BinaryTree ::NodeCount() const { return NodeCountHelp(root); } template BinaryTree ::BinaryTree(const ElemType& e) // 操作结果:建立以e为根的二叉树 { root = new BinTreeNode (e); } template const BinTreeNode * BinaryTree ::LeftChild(const BinTreeNode * cur) const // 操作结果:返回二叉树结点cur的左孩子 { return cur->leftChild; } template const BinTreeNode * BinaryTree ::RightChild(const BinTreeNode * cur) const // 操作结果:返回二叉树结点cur的右孩子 { return cur->rightChild; } template const BinTreeNode * BinaryTree ::ParentHelp(BinTreeNode * r, const BinTreeNode * cur) const // 操作结果:返回以r为根的二叉树, 结点cur的双亲 { if (r == NULL) return NULL; // 空二叉树 else if (r->leftChild == cur || r->rightChild == cur) return r; // r为cur的双亲 else { // 在子树上求双亲 const BinTreeNode * 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 const BinTreeNode * BinaryTree ::Parent(const BinTreeNode * cur) const // 操作结果:返回二叉树结点cur的双亲 { return ParentHelp(root, cur); } template void BinaryTree ::InsertLeftChild(BinTreeNode * cur, const ElemType& e) // 初始条件:cur非空, // 操作结果:插入元素值为e的结点为cur的左孩子,如果cur的左孩子非空,则cur原有左子树成为e的左子树 { if (cur == NULL) { // cur空,返回 return; } else { // 插入左孩子 BinTreeNode * child = new BinTreeNode (e);// 元素值为e结点 if (cur->leftChild != NULL) { // cur的左孩子非空 child->leftChild = cur->leftChild; // cur原有左子树成为e的左子树 } cur->leftChild = child; // e成为cur的左孩子 return; } } template void BinaryTree ::InsertRightChild(BinTreeNode * cur, const ElemType& e) // 初始条件:cur非空 // 操作结果:插入元素值为e的结点为cur的右孩子,如果cur的右孩子非空,则cur原有右子树成为e的右子树 { if (cur == NULL) { // cur为空,返回 return; } else { // 插入右孩子 BinTreeNode * child = new BinTreeNode (e);// 元素值为e结点 if (cur->rightChild != NULL) { // cur的右孩子非空 child->rightChild = cur->rightChild; // cur原有右子树成为e的右子树 } cur->rightChild = child; // e成为cur的右孩子 return; } } template void BinaryTree ::DeleteLeftChild(BinTreeNode * cur) // 初始条件:cur非空 // 操作结果:删除cur左子树 { if (cur == NULL) { // cur为空 return; } else { // cur非空 DestroyHelp(cur->leftChild); // 删除cur左子树 } } template void BinaryTree ::DeleteRightChild(BinTreeNode * cur) // 初始条件:cur非空 // 操作结果:删除cur右子树 { if (cur == NULL) { // cur为空 return; } else { // cur非空 DestroyHelp(cur->rightChild); // 删除cur右子树 } } template inline int BinaryTree ::Height() const { return HeightHelp(root); } template BinTreeNode * BinaryTree ::CopyTreeHelp(const BinTreeNode * r) // 操作结果:将以r为根的二叉树复制成新的二叉树,回新二叉树的根 { if (r == NULL) { // 复制空二叉树 return NULL; // 空二叉树根为空 } else { // 复制非空二叉树 BinTreeNode * lChild = CopyTreeHelp(r->leftChild); // 复制左子树 BinTreeNode * rChild = CopyTreeHelp(r->rightChild); // 复制右子树 BinTreeNode * rt = new BinTreeNode (r->data, lChild, rChild); // 复制根结点 return rt; } } template inline void BinaryTree ::DestroyHelp(BinTreeNode *& r) { if (r != NULL) { DestroyHelp(r->leftChild); DestroyHelp(r->rightChild); delete r; r = NULL; } } template BinaryTree ::BinaryTree(const BinaryTree & source) // 操作结果:由已知二叉树构造新二叉树——复制构造函数模板 { root = CopyTreeHelp(source.root); // 复制二叉树 } template BinaryTree ::BinaryTree(BinTreeNode * r) // 操作结果:建立以r为根的二叉树 { root = r; // 复制二叉树 } template inline void BinaryTree ::PreOrderHelp(BinTreeNode * r, void(*visit)(const ElemType&)) const { if (r != NULL) { (*visit)(r->data); PreOrderHelp(r->leftChild, visit); PreOrderHelp(r->rightChild, visit); } } template void BinaryTree ::InOrderHelp(BinTreeNode * r, void (*visit)(const ElemType&))const { if (r != NULL) { InOrderHelp(r->leftChild, visit); (*visit)(r->data); InOrderHelp(r->rightChild, visit); } } template inline void BinaryTree ::PostOrderHelp(BinTreeNode * r, void(*visit)(const ElemType&)) const { if (r != NULL) { PostOrderHelp(r->leftChild, visit); PostOrderHelp(r->rightChild, visit); (*visit)(r->data); } } template inline int BinaryTree ::HeightHelp(const BinTreeNode * 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 inline int BinaryTree ::NodeCountHelp(const BinTreeNode * r) const { if (r == NULL) return 0; else return NodeCountHelp(r->leftChild) + NodeCountHelp(r->rightChild) + 1; } template BinaryTree & BinaryTree ::operator=(const BinaryTree & source) // 操作结果:由已知二叉树source复制到当前二叉树——重载赋值运算符 { if (&source != this) { DestroyHelp(root); // 释放原二叉树所占用空间 root = CopyTreeHelp(source.root); // 复制二叉树 } return *this; } template void DisplayBTWithTreeShapeHelp(const BinTreeNode * r, int level) // 操作结果:按树状形式显示以r为根的二叉树,level为层次数,可设根结点的层次数为1 { if (r != NULL) { // 空树不显式,只显式非空树 DisplayBTWithTreeShapeHelp (r->rightChild, level + 1);//显示右子树 cout << endl; //显示新行 for (int temPos = 0; temPos < level - 1; temPos++) cout << " "; //确保在第level列显示结点 cout << r->data; //显示结点 DisplayBTWithTreeShapeHelp (r->leftChild, level + 1);//显示左子树 } } template void DisplayBTWithTreeShape(const BinaryTree & bt) // 操作结果:树状形式显示二叉树 { DisplayBTWithTreeShapeHelp (bt.GetRoot(), 1); // 树状显示以bt.GetRoot()为根的二叉树 cout << endl; } template void CreateBinaryTreeHelp(BinTreeNode *& 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 (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 BinaryTree CreateBinaryTree(ElemType pre[], ElemType in[], int n) // 操作结果:已知先序和中序序列构造二叉树 { BinTreeNode * r; // 二叉树的根 CreateBinaryTreeHelp (r, pre, in, 0, n - 1, 0, n - 1); // 由先序和中序序列构造以r为根的二叉树 return BinaryTree (r); // 返回以r为根的二叉树 }
lk_ queue.h #pragma once #include "node.h" // 链队列类模板 templateclass linkQueue { protected: // 数据成员: Node * 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 & source); // 复制构造函数模板 linkQueue & operator =(const linkQueue & source); // 重载赋值运算符 }; // 链队列类模板的实现部分 template linkQueue ::linkQueue() // 操作结果:构造一个空队列 { rear = front = new Node ; // 生成头结点 count = 0; // 初始化元素个数 } template linkQueue ::~linkQueue() // 操作结果:销毁队列 { Clear(); // 清空队列 delete front; // 释放头结点所占空间 } template int linkQueue ::Length() const // 操作结果:返回队列长度 { return count; // count表示队列元素个数 } template bool linkQueue ::Empty() const // 操作结果:如队列为空,则返回true,否则返回false { return count == 0; // count == 0表示队列为空 } template void linkQueue ::Clear() // 操作结果:清空队列 { while (!Empty()) { // 队列非空,则出列 OutQueue(); // 出列 } } template void linkQueue ::Traverse(void (*visit)(const ElemType&)) const // 操作结果:依次对队列的每个元素调用函数(*visit) { for (Node * temPtr = front->next; temPtr != NULL; temPtr = temPtr->next) { // 对队列的每个元素调用函数(*visit) (*visit)(temPtr->data); } } template bool linkQueue ::OutQueue(ElemType& e) // 操作结果:如果队列非空,那么删除队头元素,并用e返回其值,返回true, // 否则返回false { if (!Empty()) { // 队列非空 Node * 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 bool linkQueue ::OutQueue() // 操作结果:如果队列非空,那么删除队头元素,返回true, // 否则返回false { if (!Empty()) { // 队列非空 Node * temPtr = front->next; // 指向队列头素 front->next = temPtr->next; // front->next指向下一元素 if (rear == temPtr) { // 表示出队前队列中只有一个元素,出队后为空队列 rear = front; } delete temPtr; // 释放出队的结点 count--; // 出队成功后元素个数自减1 return true; // 成功 } else { // 队列为空 return false; // 失败 } } template bool linkQueue ::GetHead(ElemType& e) const // 操作结果:如果队列非空,那么用e返回队头元素,返回true, // 否则返回false { if (!Empty()) { // 队列非空 Node * temPtr = front->next; // 指向队列头素 e = temPtr->data; // 用e返回队头元素 return true; // 成功 } else { // 队列为空 return false; // 失败 } } template bool linkQueue ::InQueue(const ElemType& e) // 操作结果:插入元素e为新的队尾,插入成功true,否则返回false { Node * temPtr = new Node (e); // 生成新结点 if (temPtr == NULL) { // 动态内存耗尽 return false; // 失败 } else { // 操作成功 rear->next = temPtr; // 新结点追加在队尾 rear = temPtr; // rear指向新队尾 count++; // 入队成功后元素个数加1 return true; // 成功 } } template linkQueue ::linkQueue(const linkQueue & source) // 操作结果:由队列source构造新队列——复制构造函数模板 { rear = front = new Node ; // 生成头结点 count = 0; // 初始化元素个数 for (Node * temPtr = source.front->next; temPtr != NULL; temPtr = temPtr->next) { // 对source队列的每个元素对当前队列作入队列操作 InQueue(temPtr->data); } } template linkQueue & linkQueue ::operator =(const linkQueue & source) // 操作结果:将队列source赋值给当前队列——重载赋值运算符 { if (&source != this) { Clear(); // 清空当前队列 for (Node * temPtr = source.front->next; temPtr != NULL; temPtr = temPtr->next) { // 对source队列的每个元素对当前队列作入队列操作 InQueue(temPtr->data); } } return *this; }
lk_stack.h: #pragma once #include "node.h" // 链栈类模板 templateclass linkStack { protected: // 数据成员: Node * 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 & source); // 复制构造函数模板 linkStack & operator =(const linkStack & source); // 重载赋值运算符 }; // 链栈类模板的实现部分 template linkStack ::linkStack() // 操作结果:构造一个空栈表 { top = NULL; // 构造栈顶指针 count = 0; // 初始化元素个数 } template linkStack ::~linkStack() // 操作结果:销毁栈 { Clear(); // 清空栈 } template int linkStack ::Length() const // 操作结果:返回栈元素个数 { return count; // count表示栈元素个数 } template bool linkStack ::Empty() const // 操作结果:如栈为空,则返回true,否则返回false { return count == 0; // count == 0表示栈为空 } template void linkStack ::Clear() // 操作结果:清空栈 { while (!Empty()) { // 表栈非空,则出栈 Pop(); // 出栈 } } template void linkStack ::Traverse(void (*visit)(const ElemType&)) const // 操作结果:从栈底到栈顶依次对栈的每个元素调用函数(*visit) { Node * temPtr; // 临时指针变量 linkStack 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 bool linkStack ::Push(const ElemType& e) // 操作结果:将元素e追加到栈顶,如成功则返加true,否则如动态内存已耗尽 // 将返回false { Node * newTop = new Node (e, top); if (newTop == NULL) { // 动态内存耗尽 return false; // 失败 } else { // 操作成功 top = newTop; count++; // 入栈成功后元素个数加1 return true; // 成功 } } template bool linkStack ::Top(ElemType& e) const // 操作结果:如栈非空,用e返回栈顶元素,返回true,否则返回false { if (Empty()) { // 栈空 return false; // 失败 } else { // 栈非空,操作成功 e = top->data; // 用e返回栈顶元素 return true; // 成功 } } template bool linkStack ::Pop(ElemType& e) // 操作结果:如栈非空,删除栈顶元素,并用e返回栈顶元素,返回true,否则 // 返回false { if (Empty()) { // 栈空 return false; // 失败 } else { // 操作成功 Node * oldTop = top; // 旧栈顶 e = oldTop->data; // 用e返回栈顶元素 top = oldTop->next; // top指向新栈顶 delete oldTop; // 删除旧栈顶 count--; // 出栈成功后元素个数自减1 return true; // 功能 } } template bool linkStack ::Pop() // 操作结果:如栈非空,删除栈顶元素,返回true,否则返回false { if (Empty()) { // 栈空 return false; // 失败 } else { // 操作成功 Node * oldTop = top; // 旧栈顶 top = oldTop->next; // top指向新栈顶 delete oldTop; // 删除旧栈顶 count--; // 出栈成功后元素个数自减1 return true; // 功能 } } template linkStack ::linkStack(const linkStack & source) // 操作结果:由栈source构造新栈——复制构造函数模板 { if (source.Empty()) { // source为空 top = NULL; // 构造栈顶指针 count = 0; // 初始化元素个数 } else { // source非空,复制栈 top = new Node (source.top->data); // 生成当前栈项 count = source.count; // 栈元素个数 Node * buttomPtr = top; // 当前栈底指针 for (Node * temPtr = source.top->next; temPtr != NULL; temPtr = temPtr->next) { // 用temPtr依次指向其余元素 buttomPtr->next = new Node (temPtr->data); // 向栈底追加元素 buttomPtr = buttomPtr->next; // buttomPtr指向新栈底 } } } template linkStack & linkStack ::operator = (const linkStack & source) // 操作结果:将栈source赋值给当前栈——重载赋值运算符 { if (&source != this) { if (source.Empty()) { // source为空 top = NULL; // 构造栈顶指针 count = 0; // 初始化元素个数 } else { // source非空,复制栈 Clear(); // 清空当前栈 top = new Node (source.top->data); // 生成当前栈项 count = source.count; // 栈元素个数 Node * buttomPtr = top; // 当前栈底指针 for (Node * temPtr = source.top->next; temPtr != NULL; temPtr = temPtr->next) { // 用temPtr依次指向其余元素 buttomPtr->next = new Node (temPtr->data); // 向栈底追加元素 buttomPtr = buttomPtr->next; // buttomPtr指向新栈底 } } } return *this; }
Node.h: #pragma once // 结点类模板 templatestruct Node { // 数据成员: ElemType data; // 数据成分 Node * next; // 指针成分 // 构造函数模板: Node(); // 无参数的构造函数模板 Node(const ElemType& e, Node * link = NULL); // 已知数据元素值和指针建立结点 }; // 结点类模板的实现部分 template Node ::Node() // 操作结果:构造指针成分为空的结点 { next = NULL; } template Node ::Node(const ElemType& e, Node * link) // 操作结果:构造一个数据成分为e和指针成分为link的结点 { data = e; next = link; }



