Contents n n 5 1 The Abstract Data
Contents n n 5. 1 The Abstract Data Type 5. 2 Derived Classes and Inheritance n n 5. 3 Formula-Based Representation n n 栈的公式化描述 5. 4 Linked Representation n n 派生类和继承 栈的链表描述 5. 5 Applications 应用 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 2
5. 1 The Abstract data type n n Definition A stack is a linear list in which insertion(addition) and deletion take place at the same end, The end is called top. The other end of the list is called the bottom e 1, e 2, e 3, …, ei, …, en-1, en ↑ bottom 山东大学计算机科学与技术学院 ↑ top 数据结构 第 5章 堆栈 3
Stack configurations D ←top C B A ←bottom E ←top D C B A ←bottom Insert() l. Stack D ←top C B A ←bottom Delete C ←top B A ←bottom Delete is a table of LIFO (Last-In, First-Out). 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 4
Stack ADT Abstract. Data. Type Stack { instances linear list of elements; one end is called bottom; the other is the top 操作 Create( );create an empty stack; Is. Empty( );return true if stack if empty,false otherwise Is. Full( );return true if stack is full,false otherwise Top( );return the top element of stack Add(x);add element x to the stack. Delete(x);delete top element of stack and put it in x. } 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 5
5. 2 Derive class and inheritance(派生类和继承) n A Class B that is a specialized or restricted version of another class A may be derived (派生)from the class. A is called Based class and B the derived class. B inherits (继承) all of the members –public, protected and private of A, and all data members and functions of A are associated with every object of type B. Furthermore a class can be derived from more than one class, e. g. from A, C. 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 6
Three ways to inherent Using class header syntax (语法) to specify class B: public A //public n n Class B inherent all public and protected members of A class B: public A, private C n Class B inherent the public and protected members of A, but all the public and protected members of C become the private members of B. class B: protected A n n All public and protected member of A become the protected member of B. NOTE: in all model of inheritance, the private members of A remain private to A and are not accessible from members of B. 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 7
usage n If X is of type B, we can use function F on X by writing X. F () only when F is a public member of either B or A. 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 8
5. 3 formula-based representation Since a stack is a linear list with the restriction that additions and deletions take place and the same end. If we use formula-based representation, top element of stack= elemenet[length-1]; and bottom=element[0] The class stack defined in Program 5. 1 is a derived class of Linear. List( Program 3. 1), where the inheritance model is private, it means that the public and protected members of Linear. List are accessible bu members of Stack, but not by stack instances. 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 9
1. Program 3. 1 template <class T> class Linear. List { //公式化描述的线性表 public: Linear. List(int Max. List. Size = 10); ~Linear. List() { delete [] element; } bool Is. Empty() const { return length == 0; } int Length() const { return length; } bool Find(int k, T& x) const; int Search(const T& x) const; Linear. List<T>& Delete(int k, T& x); Linear. List<T>& Insert(int k, const T& x); void Output(ostream& out) const; private: int length; int Max. Size; T *element; }; 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 10
n 映射公式:location(i)=i-1 element 0 1 2 3 4 5 2 4 8 1 Max. Size-1 length=5 bottom n n top 栈顶元素 element[length-1] 栈底元素 element[0] 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 11
Program 5. 1 template<class T> class Stack : private Linear. List <T> { public: Stack(int Max. Stack. Size = 10) : Linear. List<T> (Max. Stack. Size) {} bool Is. Empty() const {return Linear. List<T> : : Is. Empty(); } bool Is. Full() const {return (Length() == Get. Max. Size()); } T Top() const {if (Is. Empty()) throw Out. Of. Bounds(); T x; Find(Length(), x); return x; } Stack<T>& Add(const T& x) {Insert(Length(), x); return *this; } Stack<T>& Delete(T& x) {Linear. List<T>: : Delete(Length(), x); return *this; } }; 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 12
Private inherence n n The public and protected member of linear. List become private to Stack, but the private member of Linear. List are not accessible to members of Stack. The constructor for Stack simply invokes the linear list constructor processing to it the desired stack size Max. Stack. Size. When object of type Stack are to be destroyed, the destructor for Linear. List will be invoked automatically. 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈
n n The Is. Empty function is simply a call to the corresponding function for a linear list, we use the scope resolution operator : : to distinguish between functions and members that have the same name in the base and derived class. Since Max. Size is private in Linear. List, we add a protect function Get. Max. Size() in Stack to acess the value of Max. Size. 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈
template <class T> class Linear. List { public: …… protected: int Get. Max. Size() const {return Max. Size; } private: int length; int Max. Size; T *element; }; 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 15
2. 自定义 Customized Stack template<class T> class Stack { public: Stack(int Max. Stack. Size = 10); ~Stack() { delete [] stack; } bool Is. Empty() const { return top == -1; } bool Is. Full() const { return top == Max. Top; } T Top() const; Stack<T>& Add(const T& x); // push Stack<T>& Delete(T& x); // pop private: int top; int Max. Top; T *stack; }; 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 17
stack 0 1 2 3 4 5 2 4 8 1 Max. Top top=4 template<class T> Stack<T>: : Stack(int Max. Stack. Size) { // 构造函数 Max. Top = Max. Stack. Size – 1; stack = new T[Max. Stack. Size]; top = -1; } template<class T>// return the element at the top T Stack<T>: : Top() const { if (Is. Empty()) throw Out. Of. Bounds(); else return stack[top]; } 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 19
stack 0 1 2 3 4 5 2 4 8 1 Max. Top top=4 template<class T> Stack<T>& Stack<T>: : Add(const T& x) { if (Is. Full()) throw No. Mem(); stack[++top] = x; return *this; } template<class T> Stack<T>& Stack<T>: : Delete(T& x) { if (is. Empty()) throw Out. Of. Bounds(); x = stack[top--]; return *this; } 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 20
Comparison n In a run-time test that involved a for with 100, 000 stack add and deletion operations, program 5. 1 took 50% more time than did by program 5. 2. 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈
类‘Chain’ template <class T> class Chain { public: Chain() { first = 0; } ~Chain(); bool Is. Empty() const { return first == 0; } int Length() const; bool Find(int k, T& x) const; int Search(const T& x) const; Chain<T>& Delete(int k, T& x); Chain<T>& Insert(int k, const T& x); void Output(ostream& out) const; private: Chain. Node<T> *first; }; 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 25
template<class T> class Linked. Stack : private Chain<T> { public: bool Is. Empty() const {return Chain<T>: : Is. Empty(); } bool Is. Full() const; T Top() const {if (Is. Empty()) throw Out. Of. Bounds(); T x; Find(1, x); return x; } Linked. Stack<T>& Add(const T& x) {Insert(0, x); return *this; } Linked. Stack<T>& Delete(T& x) {Chain<T>: : Delete(1, x); return *this; } }; template<class T> bool Linked. Stack<T>: : Is. Full() const {// 堆栈是否满? try {Chain. Node<T> *p = new Chain. Node<T>; delete p; return false; } catch (No. Mem) {return true; } } 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 26
n 2. 自定义链表形式的堆栈 top ……. Definition of nodes of stack template <class T> class Node{ friend Linked. Stack<T>; private: T data; Node<T> *link; }; 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 27
Class Linked. Stack n n n template<class T> class Linked. Stack {// 构建函数 public: n Linked. Stack () {top = 0; } n ~Linked. Stack(); n bool Is. Empty() const n {return top= =0; } n bool Is. Full() const; n T Top() const; n Linked. Stack<T>& Add(const T& x); n Linked. Stack<T>& Delete(T& x); private: n Node<T> *top; // 指向栈顶节点 } 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈
析构函数 n n n n n Template<class T> Linked. Stack<T>: : ~Linked. Stack() {// Stack destructor. . Node<T> *next; while (top) { next = top->link; delete top; top = next; } } 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈
Is. Full function n n n Template<class T> Linked. Stack<T>: : Is. Full() const {// Is the stack full? try { Node<T> *p = new Node<T>; delete p; return false; } catch (No. Mem) {return true; } } 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈
Top n n n Template<class T> T Linked. Stack<T>: : Top() const {// return top element if (Isempty()) throw Out. Of. Bounds(); return top->data; } 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈
Add n n n n n Template<class T> Linked. Stack<T>& Linked. Stack<T>: : Add(const T& x) {// add x to stack Node<T> *p= new Node<T>; p->data = x; p->link = top; top = p; return *this; } 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈
Delete n n n n n Template<class T> Linked. Stack<T>& Linked. Stack<T>: : Delete(T& x) {// delete top element and put in x if (Is. Empty()) throw Out. Of. Bounds(); x = top->data; Nodde<T> *p = top; top = top->link; delete p; return *this; } 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈
括号匹配 #include <iostream. h> #include <string. h> #include <stdio. h> #include "stack. h" const int Max. Length = 100; // 最大的字符串长度 void Print. Matched. Pairs(char *expr) {// 括号匹配 Stack<int> s(Max. Length) ; int j, length = strlen(expr); 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 37
括号匹配 // 从表达式expr 中搜索‘( ’‘和)’ for (int i = 1; i<=length; i++) { if (expr[i -1] = =' ( ' ) s. Add(i); else if (expr[i -1] = =' ) try { s. Delete(j); cout << j <<' ' << i << endl; } catch (Out. Of. Bounds) { cout << "No match for right parenthesis" << " at " << i << endl; } } // 堆栈中所剩下的(都是未匹配的 while(!s. Is. Empty()) { s. Delete(j); cout << "No match for left parenthesis at " <<j< endl; } } 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 38
汉诺塔 void Towers. Of. Hanoi(int n, int x, int y, int z) { / /把n 个碟子从塔x 移动到塔y,可借助于塔z if (n > 0) { Towers. Of. Hanoi(n-1, x, z, y); cout << "Move top disk from tower " << x <<" to top of tower " << y << endl; Towers. Of. Hanoi(n-l, z, y, x); } } 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 41
汉诺塔 class Hanoi{ friend void Towers. Of. Hanoi(int); public: void Towers. Of. Hanoi(int n, int x, int y, int z); private: Stack<int> *S[4]; }; void Hanoi: : Towers. Of. Hanoi(int n, int x, int y, int z) {// 把n 个碟子从塔x 移动到塔y,可借助于塔z int d; // 碟子编号 if (n > 0) { Towers. Of. Hanoi(n-l, x, z, y); S[x]->Delete(d); //从x中移走一个碟子 S[y]->Add(d); //把这个碟子放到y 上 Show. State(); Towers. Of. Hanoi(n-l, z, y, x); } } 山东大学计算机科学与技术学院 数据结构 第 5章 堆栈 51
- Slides: 56