ADT Abstract Data Type template class Elem Type
栈的 ADT (Abstract Data Type) template <class Elem. Type> class Abs. Stack { public: Abs. Stack ( ) { } // 默认构造函数 virtual ~ Abs. Stack ( ) { } // 析构函数 virtual int Is. Empty ( ) const = 0; // 判栈空吗? virtual int Is. Full ( ) const = 0; // 判栈满吗? virtual void Make. Empty( ) = 0; //将栈清空。 virtual void Push ( const Elem. Type & X ) = 0; //新结点进栈。 virtual void Pop ( ) = 0; // 栈顶结点出栈。 virtual const Elem. Type & Top( ) const = 0; // 取栈顶结点数据值。 private: Abs. Stack( const Abs. Stack & ) { } // 冻结复制另一堆栈的构造函数。 };
顺序表示的堆栈的实现 static const int Init. Stack. Size = 10; template <class Elem. Type> class Stack: public Abs. Stack<Elem. Type> { private: Elem. Type * Array; // 存放结点的数组名。 int top; // top - 栈顶指针。 int Max. Size; // 栈内能够存放结点的最大个数,即栈的容量。 void Double. Array( int Max ); // 更新数组的容量。 public: Stack ( ); // 构造函数, 初始时构造大小为Init. Stack. Size的栈。 ~Stack ( ) { delete [ ] Array; }; // 析构函数,释放占用的连续空间。 void Make. Empty ( ) { top = -1; }; // 将栈清空。 int Is. Empty ( ) const { return top = = -1; }; //栈空为True, 否则为False。 int Is. Full ( ) const { return top = = Max. Size-1; }; //栈满为True, 否则为False。 const Elem. Type & Top( ) const ; // 读取栈顶结点。 void Push ( const Elem. Type & X ); // 将X的值进栈。 void Pop ( ); // 栈顶结点出栈。 const Stack & operator = ( const Stack & R ); };
顺序表示的堆栈的实现 template<class Elem. Type> Stack<Elem. Type> : : Stack( ) : top( -1), Max. Size(Init. Stack. Size) { //构造函数, 空间大小为Max. Size Array = new Elem. Type[Max. Size]; } template <class Elem. Type> const Elem. Type & Stack<Elem. Type> : : Top( ) const { // 对非空栈,读取栈顶结点并返回结点的值。 Exception( Is. Empty( ), ”Underflow: Satck is Empty!”); return Array[top]; } template <class Elem. Type> void Stack<Elem. Type>: : Push ( const Elem. Type & X ) { if ( top + 1 = = Max. Size ) Double. Array( 2 * Max. Size ); Array[++top] = X; // 新结点放入新的栈顶位置。 } template <class Elem. Type> void Stack<Elem. Type>: : Pop() { //对非空栈, 将栈顶结点出栈 Exception( Is. Empty( ), ”Stack is underflow!”); top--; }
顺序表示的堆栈的实现 template <class Elem. Type> const Stack<Elem. Type> & Stack<Elem. Type> : : operator = ( const Stack<Elem. Type> & R ) { if ( this = = &R ) return *this; delete [ ]Array; Array = new Elem. Type[R. Max. Size]; // 分配存储单元。 top = R. top; for( int j=0; j<= top; j++ ) Array[j] = R. Array[j]; // 逐个复制结点。 return *this; } template <class Elem. Type> void Stack<Elem. Type> : : Double. Array( int Max ) { Elem. Type * oldarr = Array; Exception( Max. Size≥Max, “New size is too small!”); Array = new Elem. Type[Max]; for( int k = 0; k <= top; k++ ) Array[k] = oldarr[k]; // 逐个复制结点。 Max. Size = Max; delete [ ] oldarr; return; }
链接表示的栈(ADT) template <class Elem. Type> class Stack: public Abs. Stack<Elem. Type> { private: List. Node<Elem. Type> * top; // top 栈顶指针。 public: Stack ( ) : top(NULL) { } // 构造函数: 栈顶指针初始化。 ~Stack ( ) { Make. Empty( ); }; // 析构函数,释放占用的连续空间。 void Make. Empty ( ); // 将栈清空。 int Is. Empty ( ) const { return top = = NULL; }; // 栈空为True, 否则为False。 int Is. Full ( ) const { return 0; }; // 总认为栈不会满。 const Elem. Type & Top ( ) const ; // 读取栈顶结点。 void Push ( const Elem. Type & X ); // 将X的值进栈。 void Pop ( ); // 栈顶结点出栈。 const Stack & operator = ( const Stack & R ); };
进栈操作 · 链接表示的栈的 Push 操作: Element Next template <class Elem. Type> top 栈顶 void Stack<Elem. Type>: : Push ( const Elem. Type & X ) { top = new List. Node<Elem. Type> (X, top); } Element Next X Element Next top ∧ ∧ 栈底
进栈操作 · 链接表示的栈的 Push 操作: Element Next template <class Elem. Type> 栈顶 top void Stack<Elem. Type>: : Push ( const Elem. Type & X ) { top = new List. Node<Elem. Type> (X, top); } ∧ Element Next top Element Next X ∧ 栈底
出栈操作 · 链接表示的栈的 Pop 操作: template <class Elem. Type> void Stack<Elem. Type>: : Pop ( ) { // 对非空栈,将栈顶结点出栈。 Exception( Is. Empty( ), ”Stack is underflow!”); List. Node<Elem. Type> * q = top; top = top->Next; delete q; Element Next 栈顶 top } ∧ Element Next top q ∧ 栈底
出栈操作 · 链接表示的栈的 Pop 操作: template <class Elem. Type> void Stack<Elem. Type>: : Pop ( ) { // 对非空栈,将栈顶结点出栈。 Exception( Is. Empty( ), ”Stack is underflow!”); List. Node<Elem. Type> * q = top; top = top->Next; delete q; Element Next 栈顶 top } ∧ Element Next top q ∧ 栈底
出栈操作 · 链接表示的栈的 Pop 操作: template <class Elem. Type> void Stack<Elem. Type>: : Pop ( ) { // 对非空栈,将栈顶结点出栈。 Exception( Is. Empty( ), ”Stack is underflow!”); List. Node<Elem. Type> * q = top; top = top->Next; delete q; Element Next 栈顶 top } ∧ Element Next top ∧ 栈底
队列的ADT template <class Elem. Type> class Abs. Queue { public: Abs. Queue ( ) { } // 默认构造函数 virtual ~ Abs. Queue ( ) { } // 析构函数 virtual int Is. Empty ( ) const = 0; // 判队空吗? virtual int Is. Full ( ) const = 0; // 判队满吗? virtual void Make. Empty( ) = 0; //将队清空。 virtual void En. Queue( const Elem. Type & X ) = 0; //新结点进队。 virtual void De. Queue( ) = 0; // 队首结点出队。 virtual const Elem. Type & Front( ) const = 0; // 取队首结点数据值。 private: Abs. Queue ( const Abs. Queue & ) { } // 冻结复制另一队列的构造函数。 };
基本操作的实现: 进队 template <class Elem. Type> void Queue<Elem. Type>: : En. Queue(const Elem. Type & x) { // 值为x的结点进队。 if ( Is. Full( ) ) Double. Queue( ); Array[rear] = x; // 若队满,则数组容量扩大一倍。 Increment( rear); // rear 加 1; 若为 Max. Size,则rear取 0。 } int Is. Full( ) const {return ( rear + 1) % Max. Size = = front ; } front D 3 rear C 2 D 1 C 0 front E rear E 再进队满 rear 再进队循环、满 front C rear D front 再进队循环、不满 C 再进队、不满
基本操作的实现: 进队 ·基本操作进队的实现程序:扩大数组容量,“分期付款式”内存使用法。 template <class Elem. Type> void Queue<Elem. Type>: : En. Queue(const Elem. Type & x) { // 值为x的结点进队。 if ( Is. Full( ) ) Double. Queue( ); Array[rear] = x; Increment( rear); } front rear = j; // 新的队尾指针。 Max. Size = New. Size; delete [ ]old; 3 C 2 rear 1 E 0 F要进队 满! template <class Elem. Type> void Queue<Elem. Type>: : Double. Queue( ) { //重新创建数组单元个数多一倍的新数组,复制原队列中的结点。 int New. Size = 2 * Max. Size; Elem. Type * old = Array; Array = new Elem. Type[New. Size]; for ( int j = 0, k = front; k < rear; j++, Increment(k) ) Array[j] = old[k]; front = 0; // 新的队首指针。 D rear E D } C front
基本操作的实现: 出队 template <class Elem. Type> void Queue<Elem. Type>: : De. Queue( ) { //对于非空队列,将队首结点出队。 Exception( Is. Empty( ), ”Queue is underflow!”); Increment( front ); } int Is. Empty() const {return front = = rear; } //判断队列是否空. 。为空返回True, 否则返回False。
链接表示的队列 · 链接表示的队列:front、rear 分别是队列的队首和对尾指针。 template <class Elem. Type> class Queue : public Abs. Queue<Elem. Type> { private: List. Node<Elem. Type> * front; // 队首指针。 List. Node<Elem. Type> * rear; // 队尾指针。 public: Queue ( ) : front(NULL), rear(NULL) { } // 构造函数: 队首指针和队尾指针初始化。 ~Queue ( ) { Make. Empty( ); } //析构函数,释放占用的连续空间。 void Make. Empty ( ); // 将队列清空。 int Is. Empty ( ) const { return front = = NULL; } // 队空为True, 否则为False。 int Is. Full ( ) const { return 0; } // 总认为为False。 const Elem. Type & Front ( ) const ; // 读取队首结点数据值。 void En. Queue ( const Elem. Type & X ); // 将X的值进队。 void De. Queue ( ); // 将队首结点出队。 const Queue & operator = ( const Queue & R ); };
基本操作的实现: 进队 · 链接表示的队列:进队操作,front、rear 分别是队列的队首和对尾指针。 template <class Elem. Type> void Queue<Elem. Type>: : En. Queue ( const Elem. Type & X ) { if ( Is. Empty( ) ) front = rear= new List. Node <Elem. Type> ( X ); else rear = rear->Next = new List. Node<Elem. Type> ( X ); } front 初始化 front ∧ rear ∧ front X NULL rear ∧
- Slides: 57