Unit 5 Stack
Unit 5 Stack
• Push()
Algorithm to Push Content on Stack
• STEP 1 : START.
• STEP 2 : Store the element to push into array.
• STEP 3 : Check if top== (MAXSIZE-1) then stack is full else goto step 4.
• STEP 4 : Increment top as top = top+1.
• STEP 5 : Add element to the position stk[top]=num.
• STEP 6 : STOP.
• POP Operation
Algorithm to Pop Content on Stack
• STEP 1: START.
• STEP 2 : Check if top== (-1) then stack is empty else goto step 4.
• STEP 3 : Access the element top is pointing num = stk[top];
• STEP 4 : Decrease the top by 1 top = top-1;
• STEP 6 : STOP.
Representation of Stack using Linked Organization
Reversal of String
Reversing a List Using Stack
Algorithm to Evaluate Postfix Expression
• Step 1 : Read postfix expression from left to right
• Step 2 : If operand is encountered, push it in stack
• Step 3 : If the operator is encountered pop two elements A -> Top
element, B -> next top element Evaluate B op A
• Step 4 : Push result into the stack
• Step 5 : Read next postfix element if not end of postfix string
• Step 6 : Repeat from step 2
• Step 7 : Print the result popped from stack
Rules to convert Infix to Postfix
• Step 1: Scan the infix expression from left to right.
• Step 2: If the scanned character is an operand, put it in the postfix expression.
• Step 3: Otherwise, do the following
• If the precedence and associativity of the scanned operator are greater than the precedence and
associativity of the operator in the stack [or the stack is empty or the stack contains a ‘(‘ ], then push it in
the stack. [‘^‘ operator is right associative and other operators like ‘+‘,’–‘,’*‘ and ‘/‘ are left-associative].
• Check especially for a condition when the operator at the top of the stack and the scanned operator
both are ‘^‘. In this condition, the precedence of the scanned operator is higher due to its right
associativity. So it will be pushed into the operator stack.
• In all the other cases when the top of the operator stack is the same as the scanned operator, then
pop the operator from the stack because of left associativity due to which the scanned operator has
less precedence.
• Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the
scanned operator.
• After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping
then stop there and push the scanned operator in the stack.)
Continue...
• Step 4: If the scanned character is a ‘(‘, push it to the stack.
• Step 5: If the scanned character is a ‘)’, pop the stack and output it until a
‘(‘ is encountered, and discard both the parenthesis.
• Step 6: Repeat steps 2-5 until the infix expression is scanned.
• Step 7: Once the scanning is over, Pop the stack and add the operators in
the postfix expression until it is not empty.
• Step 8: Finally, print the postfix expression.
• To convert an infix expression to a prefix expression, The idea is as follows:
• Step 1: Reverse the infix expression. Note while reversing each ‘(‘ will
become ‘)’ and each ‘)’ becomes ‘(‘.
• Step 2: Convert the reversed infix expression to “nearly” postfix
expression.
• While converting to postfix expression, instead of using pop operation to
pop operators with greater than or equal precedence, here we will only
pop the operators from stack that have greater precedence.
• Step 3: Reverse the postfix expression.
Postfix to Infix Conversion
Algorithm to check well form ness of an
expression
• Declare a character stack (say temp).
• Now traverse the string exp.
• If the current character is a starting bracket ( ‘(‘ or ‘{‘ or ‘[‘ ) then push it to
stack.
• If the current character is a closing bracket ( ‘)’ or ‘}’ or ‘]’ ) then pop from the
stack and if the popped character is the matching starting bracket then fine.
• Else brackets are Not Balanced.
• After complete traversal, if some starting brackets are left in the stack then the
expression is Not balanced, else Balanced.
Recursion using stack
• The recursion use the stack implementation. The stack is a data structure
which can be manipulated by pushing (adding) and popping(removing) data
of the top of the list
• So, when a function is called, this one go to the top of the stack. And then,
as the operation of a stack requires, the function at the top of the stack will
be the first to be executed. This means that the last function called will be
the first one executed.
• when a function requests space for local variables, the allocated space is
added to the top of the stack. So when the end of the function is reached, the
space allocated for local variables appears at the top of the stack.
Backtracking
• Backtracking is a problem-solving algorithmic technique that involves
finding a solution incrementally by trying different
options and undoing them if they lead to a dead end. It is commonly used
in situations where you need to explore multiple possibilities to solve a
problem, like searching for a path in a maze or solving puzzles like Sudoku.
When a dead end is reached, the algorithm backtracks to the previous
decision point and explores a different path until a solution is found or all
possibilities have been exhausted.