Stack Data Structure in Python with different exercise
In computer science, a stack is an abstract data type that serves as a collection of elements, with two main operations:
- Push, which adds an element to the collection, and
- Pop, which removes the most recently added element that was not yet removed.
- Peek operation can, without modifying the stack, return the value of the last element added.
The order in which an element added to or removed from a stack is described as last in, first out, referred to by the acronym LIFO.
Stack concept are done by using arrays and linked lists where the top position is tracked using a variable or header pointer respectively
The method that will be used:
* append(x): Appends x at the end of the list
* pop() : Removes last elements of the list
Stacks are considered as the backbone of Data Structures. Most of the algorithms and applications are implemented using stacks.
- Used in browsers to move to the last page we visited
- used in “undo” mechanism in the text editor.
- Language processing:
- space for parameters and local variables is created internally using a stack.
- compiler’s syntax check for matching braces is implemented by using stack.
- support for recursion.
- Expression evaluation like postfix or prefix in compilers.
- Backtracking (game playing, finding paths, exhaustive searching.
- Python Stack is also used in Memory management, run-time environment for nested language features. etc
- Used in Algorithms like Tower of Hanoi, tree traversals, histogram problem and also in graph algorithms like Topological Sorting.