Notes of Stack
Notes of Stack
What is Stack:
A stack is a linear data structure in python in which addition and deletion of elements can be done at one
end only that is called TOP. A stack is known as LIFO (Last – In, First – Out) data structure in python.
Insertion in stack is also known as a PUSH operation. Deletion from stack is also known as POP operation
in stack.
1. Reversing a word/Line.
2. Undo Mechanism in Text Editors.
3. Using in recursion.
4. Stack application in backtracking. Backtracking is used in a large number of puzzles games like
Sudoku.
5. <html> code
Working with stack is similar to working with list. Basic operations that we should know are :
1. How to create an empty stack?
2. How to add elements to a stack?
3. How to delete / remove elements from the stack
4. How to traverse or displaying elements of stack?
5. How to check for empty stack?
Create an empty stack:
st = [ ] or st = list( )
Underflow: It refers to situation when one tries to pop/delete an item from an empty stack or queue.
Example:
List=[]
List.pop()
What is Peek: Get the top data element of the stack, without removing it.
We can do this by: list[-1].
Steps algorithm for Push Operations:
Steps 1: Checks if the stack is full.
Steps 2: If stack is full, produce an error and exit.
Steps 3: If stack is not full, Add an element in stack using append().
Steps 4: Then Access the List or Stack.