翦四拿彎訇队 栈( Stack) 队列( Queue) 优先队列( Priority Oueue)
栈 ( Stack ) 队列 ( Queue ) 优先队列 (Priority Queue)
栈( Stack) 只允许在一端插入和删除的顺序表 允许插入和删除进栈 退栈 (压入) 弹出) 的一端称为栈顶 (tp),另一端称 to p n-1 为栈底( bottom) an-2 特点 后进先出(LFO 01 0 botton→
栈 ( Stack ) 只允许在一端插入和删除的顺序表 允许插入和删除 的一端称为栈顶 (top),另一端称 为栈底(bottom) 特点 后进先出 (LIFO)
栈的抽象数据类型 template <class Type> class Stack i ublic Stack(int=10 ) /构造函数 void push( const Type&item);∥进栈 Type Pop o: 出栈 ype GetTop (; /取栈顶元素 void MakeEmp(;∥量空栈 int IsEmpty() const;/判栈空否 int IsFull( const; /判栈满否
template <class Type> class Stack { public: Stack ( int=10 ); //构造函数 void Push ( const Type & item); //进栈 Type Pop ( ); //出栈 Type GetTop ( ); //取栈顶元素 void MakeEmpty ( ); //置空栈 int IsEmpty ( ) const; //判栈空否 int IsFull ( ) const; //判栈满否 } 栈的抽象数据类型
栈的数组表示一顺序栈 include assert h> template <class Type> class Stack i public: Stack( int=10 ); 构造函数 Sck(){ delete[] elements;M/析构函数 void push( const Type&iem);∥进 Type Pop (; ∥/出栈 Type GetTop o; /取栈顶元素 void MakeEmpty(){top=-1;}∥量空栈 int IsEmpty( const return top==-1;)
#include <assert.h> template <class Type> class Stack { public: Stack ( int=10 ); //构造函数 ~Stack ( ) { delete [ ] elements; }//析构函数 void Push ( const Type & item ); //进栈 Type Pop ( ); //出栈 Type GetTop ( ); //取栈顶元素 void MakeEmpty ( ) { top=-1; } //置空栈 int IsEmpty ( ) const { return top == -1; } 栈的数组表示 — 顺序栈
int IsFull( const i return top==maxSize-l;3 private: int top; 顶数组指针 Iype* elements;数组 int max size 最大容量 template <class Type> Stack<Type>:: Stack( int s): top(1), maxSize(s)i elements=new Type[max]; assert( elements!=0);∥断言
int IsFull ( ) const { return top == maxSize-1; } private: int top; //栈顶数组指针 Type *elements; //栈数组 int maxSize; //栈最大容量 } template <class Type> Stack<Type>:: Stack ( int s ) : top (-1), maxSize (s) { elements = new Type[maxSize]; assert ( elements != 0 ); //断言 }