top-b top- top→空栈 a进栈 b进栈 top top- edcba to p b edcba e进栈 ∫进栈濫出 e退栈
top 空栈 top top top top top a 进栈 b 进栈 a a b a b c d e e 进栈 a b c d e f 进栈溢出 a b d e e 退栈 c
top cba top-b b top d退栈 C退栈 b退栈 top-a退栈top→空栈
top c 退栈 b 退栈 a b a a 退栈 空栈 top a b d d 退栈 c top a b c top top
template <class Type> void Stack<Type>:: Push( Type item assert(!IsFull () ∥/先决条件断言 elements[++topl=item;∥/加入新元素 template <class Type> int stack<Type>:: Popoi if( IsEmpty() return0;栈空不退栈 top, return 1 ∥退出栈顶元素
template <class Type> void Stack<Type> :: Push ( Type & item ) { assert ( !IsFull ( ) ); //先决条件断言 elements[++top] = item; //加入新元素 } template <class Type> int stack<Type> :: Pop ( ) { if ( IsEmpty ( ) ) return 0; //栈空不退栈 top--; return 1; //退出栈顶元素 }
template <class Type> Type stack<Type>:: GetTop oi assert( IsEmpty());∥先决条件断言 return elements[top];/取出栈顶元素 双栈共享一个栈空间 0 maxSize-1 b[0 t0]{1
template <class Type> Type stack<Type>:: GetTop ( ) { assert ( !IsEmpty ( ) ); //先决条件断言 return elements[top]; //取出栈顶元素 } 双栈共享一个栈空间 b[0] t[0] t[1] b[1] 0 maxSize-1 V
栈的链接表示一链式栈 top- 链式栈无栈满问题,空间可扩充 插入与删除仅在栈顶处执行 a链式栈的栈顶在链头 适合于多栈操作
栈的链接表示 — 链式栈 ◼ 链式栈无栈满问题,空间可扩充 ◼ 插入与删除仅在栈顶处执行 ◼ 链式栈的栈顶在链头 ◼ 适合于多栈操作 top