Stacks are linear groups that can only be accessed from one end, which is called the top of the stack, and the other end is called the bottom of the stack. A stack is a last-in-first-out data structure.
Stack
Application examples of stacks--expression processing
The basic state of the stack
- Stack empty
- Stack full
- General status
Stack empty
- There are no elements in the stack (for example, an array holds the stack)
Stack full
- The number of elements in the stack reaches the upper limit (in the case of stacks accommodated in arrays)
General status
- There are elements in the stack, but not up to the full state (as an example of a stack accommodated in an array)
Basic operation of the stack
- Initialization
- Into the stack
- Out of the stack
- Empty stack
- Access top of Stack element
- Check the status of the stack (full, empty)
Example 9-8 Stack class template
//Stack.h#ifndef Stack_h#defineStack_h#include<cassert>Template<classTintSIZE = ->classStack {Private: T list[size]; inttop; Public: Stack (); voidPushConstT &item); T pop (); voidClear (); ConstT &peek ()Const; BOOLIsEmpty ()Const; BOOLIsfull ()Const;};//implementation of the templateTemplate <classTintSize>Stack<t, Size>::stack (): Top (-1) {} template<classTintSize>voidStack<t, Size>::p Ush (ConstT &Item) {Assert (!isfull ()); list[++top] =item;} Template<classTintSize>T Stack<t, size>::p op () {Assert (!isEmpty ()); returnlist[top--]; }template<classTintSize>ConstT &stack<t, Size>::p eek ()Const{assert (!isEmpty ()); returnList[top];//returns the top element of the stack}template<classTintSize>BOOLStack<t, Size>::isempty ()Const { returntop = =-1;} Template<classTintSize>BOOLStack<t, Size>::isfull ()Const { returntop = = SIZE-1;} Template<classTintSize>voidStack<t, size>:: Clear () {Top= -1;}#endif //Stack_h
Stack class template