36 lines
1.3 KiB
Markdown
Executable File
36 lines
1.3 KiB
Markdown
Executable File
# Overview
|
|
|
|
|
|
- A stack is ta LIFO list. Meaning "last in first out"
|
|
- Access is allowed only from one end of the stack
|
|
- adding an item is called pulsing the stack
|
|
- retrieving an item is called popping the stack
|
|
|
|
# Typical uses
|
|
|
|
- Parentheses matching; ()), or (())
|
|
- compiler's syntax check for matching braces is implemented by using stack
|
|
|
|
- The simplest application of a stack is to reverse a word.
|
|
• You push a given word to stack - letter by letter - and then pop letters from the stack.
|
|
|
|
- Another application is an "undo" mechanism in text editors;
|
|
•this operation is accomplished by keeping all text changes in a stack
|
|
|
|
# Parenthesis count
|
|
|
|
- Methodology (logical steps to solve the problem):
|
|
- scan expression from left to right;
|
|
- when a left parenthesis is encountered, add its position to the stack;
|
|
- when a right parenthesis is encountered, remove matching position from stack (more example in a moment).
|
|
|
|
# try-catch-throw
|
|
|
|
- When you enter a try block, push the address of this block onto a stack.
|
|
\
|
|
- When an exception is thrown, pop the try block that is at the top of the stack; if the stack is empty, terminate the program.
|
|
|
|
- If the popped try block has no matching catch block, go back to the previous step.
|
|
|
|
- If the popped try block has a matching catch block, execute the matching catch block.
|