Data Structures Stack Notes
Stack Operations
> push(): When we insert an element in a stack then the operation is known as a push. If the stack is full then the overflow condition occurs.
> pop(): When we delete an element from the stack, the operation is known as a pop. If the stack is empt means that no element exists in the stack, this state is known as an underflow state.
> isEmpty(): It determines whether the stack is empty or not.
> isFull(): It determines whether the stack is full or not
> peek(): It returns the element at the given position.
> count(): It returns the total number of elements available in a stack.
> change(): It changes the element at the given position.
> display(): It prints all the elements available in the stack.
What is stack ?
A stack is a linear data structure that follows the principle of Last In First Out (LIFO).
This means the last element inserted inside the stack is removed first.
It contains only one pointer top pointer pointing to the topmost element of the stack.
In other words, a stack can be defined as a container in which insertion and deletion can be done from the one end known as the top of the stack
Push operation :
Before inserting an element in a stack, we check whether the stack is full.
If we try to insert the element in a stack, and the stack is full, then the overflow condition occurs.
When we initialize a stack, we set the value of top as -1 to check that the stack is empty.
When the new element is pushed in a stack, first, the value of the top gets incremented, i.e., top=top+l, and the element will be placed at the new position of the top.
The elements will be inserted until we reach the max size of the stack.
Before deleting the element from the stack, we checkwhether the stack is empty.
If we try to delete the element from the empty stack, then the underflow condition occurs.
If the stack is not empty, we first access the element which is pointed by the top
Once the pop operation is performed, the top is decremented by ], i.e., top=top-1.
Read Also: Top 10 Python Interview Question