Stacks
Stack is an abstract data structure which follows LIFO(last in first out) logic. Stack is a real time structure where we use in our daily life scenario.
e.g. :pack of cards, books placed in a box.
Stack is just like a container having shelves t store the data in each segment. Two operations are applied on stack.
Push: It is like inserting a data item into the stack. The data is filed from bottom to top of the stack.
Pop: Removing an element from the stack is said to be pop operation in stack.
Let us see an example:
The important term used in stack is “top”, which refers the uppermost element of the stack.
Stack has a definite size. While pushing an element into the stack, the user should verify whether the stack is full or not. But before removing or popping an element , the user have to check for the stack empty condition.
Now, let’s have a look in push and pop algorithms
Push algorithm:
- Check for the stack full condition.
- If the stack is full return stack is full.
- Else insert the element into the stack.
- Increment the position of top by 1.
- return success.
Pop algorithm:
- Check for the stack empty condition
- If stack is empty return stack empty.
- Else access the data at the top in stack.
- Decrement the position of top by 1.
- Return success.
Applications of stack:
- Expression evaluation
- Expression conversion
- Function call
- Backtracking
- Syntax parsing
From the above applications, let’s get deep into one of the applications i.e. Expression Conversion.
Before knowing expression conversion, what is an expression?
Expression is said to be a combination of operators and operands. Operators are the arithmetic symbols. The arithmetic operations are done on arithmetic operands.
e. g: a +b
where a, b are operands and + is an operator.
There are 3 different types of expressions:
- Infix: It is a human readable type, where the operator are in between operands.
e. g : a +b
2. Postfix: It is a computer readable type , where the operators are placed after the operands.
e. g: ab+
3. Prefix: Here , it is a reverse type of postfix where the operands comes first and the operators.
e. g: +ab
Converting an expression from one type to another type is called as expression conversion.
In the next story ,we will discuss the conversion of an expression from infix to post fix.
Thank you.