0% found this document useful (0 votes)
11 views14 pages

Harmanjit Singh

Uploaded by

harmansaharan.03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views14 pages

Harmanjit Singh

Uploaded by

harmansaharan.03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

ADVANDED DATA STRUCTURE AND ALGORITHM

Stack
Submitted by:
Harmanjit Singh
UID:72212506 Submitted to:
CLASS:BCA IBM(CS) 3rd MRS. NIKITA SINGLA
• Stack is a Linear Data Structure in which the elements are
added and deleted always from one end called TOP.
• It follows an order called as LIFO(Last in First Out) i.e. last
element added in the stack will be the first one to be deleted.
• Stack Operations
a) PUSH ----- adding an element on to the Stack
b) POP ------- deleting an element from the Stack
• Stack can be represented by 2 ways:
a) Array Representation of Stack
b) Linked List Representation of Stack

(Array Representation of Stack)


Terminologies

STACK _Array : An Array Name of the STACK.


TOP : A variable that stores the index number of the top element.
MAXSTACK: A variable that tells the maximum number of
elements that a stack can hold.
Conditions in the Stack
a) Under Flow: It means that the STACK is empty
The condition is checked only when the POP operation has to be
performed on the STACK
Syntax: TOP = -1 or TOP = NULL
a) Over Flow: It means that the STACK is already filled and it
does not have any more space to accommodate the new
element .
The condition is checked only when the PUSH operation
has to be performed on the STACK

Syntax: TOP = MAXSTACK - 1


Procedure : PUSH(ITEM) to add element to the STACK
Step 1 : Set TOP: = -1
Step2: If(TOP == MAXSTACK – 1) , then:
Write: Overflow and Return
Step 3: Set TOP: = TOP-1
Step4: STACK_ Array[TOP] = ITEM
Step 5: Return
• Procedure : POP() to delete element from the STACK

Step1: If(TOP == -1) , then:


Write: Underflow and Return
Step 2: Set ITEM: = STACK_ Array[TOP]
Step3: Set TOP = TOP -1
Step 4: Return
Application
• For Solving Arithmetic expression that includes the operands
and only Arithmetic Operators
• ^,/ ,*,+and – ( 5 arithmetic operators)
• Operators arranged in Precedence Order
a) ^
b) *,/
c) +,-
• Infix
– Operator is written in-between the operands. A+B
• Prefix
– Operator is written before the operands, also called polish
notation. +AB
• Postfix
– Operators are written after the operands, also called suffix
or reverse polish notation. AB+
Algorithm for evaluating a postfix expression

1. Add a right parenthesis “)” at the end of P.


2. Scan P from left to right and repeat steps 3 and 4 for each
element of P until the “)” is encountered.
3. If an operand is encountered , put it on Stack.
4. If an operator Ø is encountered, then
a) Remove the two top elements of stack, where A is the top
element and B is the next to top element
b) Evaluate B Ø A
c) Place the result of (b) back on stack
[end of if structure]
[end of step 2 loop]

5.Set value equal to the top element on stack


6. Exit
• Evaluate the following Post Fix expression
P: 5 , 6, 2 , + , * , 12 , 4 , / , -

Answer : 37
Algorithm for converting infix to postfix
Postfix(Q, P)
Suppose Q is an arithmetic expression in infix and equivalent
postfix expression P
1. Push “(” into stack and add “)” to the end of Q
2. Scan Q from left to right and repeat steps 3 to 6 for each
element of Q until the stack is empty
3. If an operand is encountered add it to P
4. If a left parenthesis is encountered, push it onto Stack
5. If an operator Ø is encountered, then:
(a) add Ø to stack
(b) Repeatedly pop from stack and add P each operator
which has the same precedence as or higher precedence
than Ø
6. If a Right parenthesis ) is encountered then,
(a) Repeatedly Pop from stack and add to P each
operator until a left parenthesis is encountered.
(b) Remove the left parenthesis. [Do not add
the left parenthesis to P.]
[End of IF structure]

[End of step 2 loop]

7. Exit
Infix Expression: A+ (B*C-(D/E^F)*G)*H, where ^ is an exponential operator.

You might also like