DS UnitII
DS UnitII
DEFINITION
TOP is the pointer that points the top element in the stack.
The elements are removed from a stack in the reverse order of that in
Stack can either be a fixed size one or it may have a sense of dynamic resizing.
Which is the first element to delete?
Stack operations
When data is PUSHed onto stack, To use a stack efficiently, we need to check the
status of stack as well. For the same purpose, the following functionality is added to
stacks −
peek() − get the top data element of the stack, without removing it.
The pointer that represents the top of the stack, hence named top.
The top pointer provides top value of the stack without actually removing it.
Status of Stack
Push Operation
The process of putting a new data element onto stack is known as a Push
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
return
endif
top ← top + 1
stack[top] ← data
end procedure
Algorithm for PUSH Operation
Implementation of this algorithm in C, is very easy. See the following code −Example
void push(int data)
{
if(!isFull())
{
top = top + 1;
stack[top] = data;
}
else
{
printf(”Could not insert data, Stack is full.\n”);
}
}
Pop Operation
Accessing the content while removing it from the stack, is known as a Pop Operation. In
an array implementation of pop() operation, the data element is not actually removed,
instead top is decremented to a lower position in the stack to point to the next value. But in
linked-list implementation, pop() actually removes data element and de _allocates memory
space.
A Pop operation may involve the following steps −
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Algorithm for Pop Operation
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
Step 3 − If the stack is not empty, Modify the ith data element from the top of the
stack.
Step 3 − If the stack is not empty, Modify the ith data element from the top of the
stack.
expression can be written in three different but equivalent notations, i.e., without
Infix Notation
between operands.
It is easy for us humans to read, write, and speak in infix notation but the same
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗
A postfix expression is a collection of operators and operands in which the operator is placed
after the operands. That means, in a postfix expression the operator follows the operands.
Example
Postfix Expression Evaluation using Stack Data Structure
A postfix expression can be evaluated using the Stack data structure. To evaluate a postfix expression
1.Read all the symbols one by one from left to right in the given Postfix Expression
3.If the reading symbol is operator (+ , - , * , / etc.,), then perform TWO pop operations and store the two
popped oparands in two different variables (operand1 and operand2). Then perform reading symbol
operation using operand1 and operand2 and push result back on to the Stack.
4.Finally, perform a pop operation and display the popped value as final result.
Algorithm postfixEvaluation(postfix)
4. If the scanned character is an operator, then pop operands from the stack. Perform
6. In the end, the stack will contain only 1 value, that is the answer to the postfix
expression.
Algorithm postfixEvaluation(postfix)
if ch is an operator ⨀ , then
done
Example
Example
Output: AB*C+
Input: (A+B)*(C/D)
Output: AB+CD/*
Convertion of infix expression into a postfix expression
Operator precedence - This refers to the priority given to each operator in an expression.
Associativity - If two operators have the same precedence, associativity is used to determine
the order of evaluation.
Rules for the conversion from infix to postfix expression
The infix notation is parsed from left to right, and then converted to postfix.
Assume initially the postfix expression is empty, and we will fill the postfix
If we encounter an operator:-
4.1. If the operator has higher precedence than the one on top of the stack , push it in to the stack.
4.2. If the operator has lower or equal precedence than the one on top of the stack, we keep popping out the one on
top of the stack and appending it to the postfix expression. And we push the new one in to the stack.
When the last token of infix expression has been scanned, we pop the remaining elements from stack and append
them to our postfix expression.*
Input:
A*(B*C+D*E)+F
Output:
ABC*DE*+*F+
Recursion
naturally split into several tasks of the same kind, but simpler.
Recursion
Properties
A recursive function can go infinite like a loop. To avoid infinite running of recursive
function, there are two properties that a recursive function must have −
Base criteria − There must be at least one base criteria or condition, such that, when
Progressive approach − The recursive calls should progress in such a way that each
Implementation
whenever a function (caller) calls another function (callee) or itself as callee, the caller
function transfers execution control to the callee. This transfer process may also
Fn = Fn-1 + Fn-2
F8 = 0 1 1 2 3 5 8 13
or, this −
Recursion - Fibonacci Series
Fibonacci series generates the subsequent number by adding two previous numbers.
Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be
taken 0, 1 or 1, 1 respectively.
Fn = Fn-1 + Fn-2
F8 = 0 1 1 2 3 5 8 13
or, this −
Recursion - Fibonacci Series