The section provides brief introduction to stack and its operations (PUSH and POP) along with the implementation of stack in real scenario to convert infix expression to postfix expression.
chaitra-1.pptx fake news detection using machine learning
Stack - Operations and Applications
1. Chapter 2 (Part 1)
Stack
2.1 Introduction to Stack
Stack is a data structure in which new items may be added or
existing items may be removed from one end. It follows Last In
First Out (LIFO) principle. The end from which addition and
deletion of data items is performed is known as top of stack.
2.2 Stack Operations
2.2.1 Push
The push operation adds an item to the top of the stack. The
push operation is not possible only if a limit on number of items
in the stack is specified and the stack is full.
2.2.2 Pop
The pop operation removes an item that is present at the top of
the stack. The stack with no items is known as empty stack. The
pop operation is not possible in an empty stack.
2.2.3 StackTop
2. The stackTop operation determines what the top item on a stack
is without removing the item. This operation is not defined for
empty stack.
2.3 ADT for Stack
abstract typedef <<data_type>> STACK (data_type);
abstract is_empty(s)
STACK(data_type) s;
postcondition empty == (len(s) == 0);
abstract data_type pop(s)
STACK(data_type) s;
precondition is_empty(s) == FALSE;
postconsition pop == top(s);
s == sub(s, 1, len(s)-1)
abstract push(s, data)
STACK s;
data_type data;
postcondition s = s + <data>
2.4 Algorithm for Stack Operations
Consider a stack (named as Stack) with the end from which
addition and removal of data items can be done be ‘top’. Let us
assume that the stack is of fixed size given by size.
2.4.1 Check for empty stack
1. Start
2. Is top == -1?
a) Yes : status = TRUE
b) No : status = FALSE
3. 3. Return status
4. Stop
2.4.2 PUSH Operation
1. Start
2. Get an item to be pushed (lets say i)
3. Is top > size -1?
a) Yes : display “Stack is full” and goto step 6
b) No : goto step 4
4. Top = top + 1
5. Stack[top] = i
6. Stop
2.4.3 POP Operation
1. Start
2. Check for empty stack?
a) Yes : display “Stack is empty” and goto step 5
3. Pop_item = stack[top]
4. Top = top - 1
5. Stop
2.5 Static and Dynamic Implementation of Stack
4. Static Implementation Dynamic Implementation
Array is used to create stack. Pointer is used to create stack.
Simple to create. Complex
Size of stack is declared at the
beginning.
Size of stack is not declared at
the beginning.
The stack is of fixed size. The stack is of variable size.
2.6 Prefix, Infix and Postfix Notation
Prefix notation specifies that the operator precedes the two
operands. It is also known as polish notation. Eg : +AB
Infix notation specifies that the operator is in between the two
operands. Eg : A + B
Postfix notation specifies that the operator follows the two
operands. It is also known as reverse polish notation. Eg : AB+
2.7 Importance of Postfix Notation
- The order of execution of postfix expression is always from
left to right and uses stack. It do not need to analyze any
precedence rule. This made computation of expression
easy.
- Calculation is faster due to no need of parenthesis leading
to less number of operations.
2.8 Conversion from Infix to Postfix Expression
5. While (token present to read):
Read a token
If (token is number):
Push it to the output queue
If (token is a function):
Push it onto the operator stack
If (token is an operator):
While ((function at top of operator stack) or (operator at top of operator stack
with high precedence) or (operator at top of operator stack with equal precedence and left
associative)) and (operator at top of operator stack is not a left bracket) :
Pop operators from the operator stack onto the output queue
Push the token onto the operator stack
If (token is left bracket “(“ ) :
Push it onto operator stack
If (token is right bracket “)” ) :
While (operator at top of operator stack is not a left bracket):
Pop operator from operator stack onto the output queue
Pop left bracket from the stack
If (no token present to read) :
While (operator tokens present on the stack) :
Pop operator from operator stack onto the output queue
2.9 Evaluation of Postfix Expression
For each token in postfix expression:
If (token is operator) :
6. Operand2 = pop from stack
Operand1 = pop from stack
Result = evaluate token with operand1 and operand2
Push result back onto stack
Else if (token is operand) :
Push token onto stack
Result = pop from stack