WEEK 3
1、栈的链表实现,push,pop和top语句
2、
2.3 The Stack ADT
- Last-In-First-Out (LIFO)
- Objects : A finite ordered list with zero or more elements.
- Operations :
- IsEmpty
- CreatStack
- DisposeStack
- MakeEmpty
- Push
- Top
- Pop
- A Pop(or Top) on an empty stack in an error in the stack ADT.
- Push on a full stack is an implementation error but not an ADT error.
Linked List Implementation (with a header node)
- The calls to malloc and free are expensive. Simply keep another stack as a recycle bin.
C | |
---|---|
C | |
---|---|
C | |
---|---|
C | |
---|---|
Array Implementation of Stacks
C | |
---|---|
-
The stack model must be well encapsulated(封装). That is, no part of your code, except for the stack routines, can attempt to access the Array or TopOfStack variable.
-
Error check must be done before Push or Pop (Top).
C | |
---|---|
C | |
---|---|
C | |
---|---|
Application
- Balancing Symbols
检查括号是否平衡
-
Postfix Evaluation 后缀表达式
-
Infix to Postfix Conversion
-
读到一个操作数时立即把它放到输出中
- 读到一个操作符时从栈中弹出栈元素直到发现优先级更低的元素为止,再将操作符压入栈中
- The order of operands is the same in infix and postfix.
- Operators with higher precedence appear before those with lower precedence.
- Never pop a ’(‘ from the stack except when processing a ‘)’.
- When ‘(’ is not in the stack, its precedence is the highest; but when it is in the stack, its precedence is the lowest.
-
Exponentiation associates right to left.
-
Function Calls (System Stack)
Note : Recursion can always be completely removed. Non recursive programs are generally faster than equivalent recursive programs. However, recursive programs are in general much simpler and easier to understand.
2.4 The Queue ADT
- First-In-First-Out (FIFO)
- Objects : A finite ordered list with zero or more elements.
- Operations :
- IsEmpty
- CreatQueue
- DisposeQueue
- MakeEmpty
- Enqueue
- Front
- Dequeue
Array Implementation of Queues
C | |
---|---|
Circular Queue :
- The maximum capacity of this queue is 5.
Note : Adding a Size field can avoid wasting one empty space to distinguish “full” from “empty”.