Files
G4G0-2/Data Structures/AI Notes/Stack.md
2025-03-16 18:59:42 +00:00

31 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.