Stacks In Python 2

APPLICATIONS OF STACKS

Some of the important applications of stacks include:

Reversing a word/line

A simple example of a stack application is reversal of a given line. This can be accomplished by pushing each character on to a stack as it is read. When the line is finished, characters are then popped off the stack and they will come off in the reverse order.

Recursion

The compiler uses a stack to store the previous state of a program when a function is called or during recursion.

Syntax Parsing

Many compilers use a stack for parsing the syntax of expressions, program blocks, etc. before translating into low-level code.

Backtracking

Another important application is backtracking (backtracking is a form of recursion, but it involves choosing only one option out of the possibilities.)
Backtracking is used in a large number of puzzles like Sudoku and in optimization problems such as knapsack.

Parenthesis Checking

A stack is used to check the proper opening and closing of parenthesis.

Undo mechanism

Undo mechanism in text editors: this operation is accomplished by keeping all the text changes in a stack.

Browser

The browser also uses a stack to store the sites visited by the user. On clicking back button we are taken to previous sites visited.

Expression Evaluation

The stack is used to evaluate prefix, postfix, and infix expressions.

Expression Conversion

An expression can be represented in the prefix, postfix, or infix notation. The stack can be used to convert one form of expression to another.

String Reversal

The stack is used to reverse a string. We push the characters of string one by one into the stack and then pop character from the stack.

Function Call

The stack is used to keep information about the active functions or subroutines.