0% found this document useful (0 votes)
65 views35 pages

Lecture Week 5 - 1: Stack

Here are the step-by-step workings: 1. Scan 5, push onto stack: [5] 2. Scan 6, push onto stack: [5, 6] 3. Scan 2, push onto stack: [5, 6, 2] 4. Scan +, pop 2 and 6, add and push result: [5, 8] 5. Scan *, pop 8 and 5, multiply and push result: [40] 6. Scan 12, push onto stack: [40, 12] 7. Scan 4, push onto stack: [40, 12, 4] 8. Scan /, pop 4 and 12, divide and push result: [40, 3]

Uploaded by

Maham urooj
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)
65 views35 pages

Lecture Week 5 - 1: Stack

Here are the step-by-step workings: 1. Scan 5, push onto stack: [5] 2. Scan 6, push onto stack: [5, 6] 3. Scan 2, push onto stack: [5, 6, 2] 4. Scan +, pop 2 and 6, add and push result: [5, 8] 5. Scan *, pop 8 and 5, multiply and push result: [40] 6. Scan 12, push onto stack: [40, 12] 7. Scan 4, push onto stack: [40, 12, 4] 8. Scan /, pop 4 and 12, divide and push result: [40, 3]

Uploaded by

Maham urooj
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/ 35

LECTURE WEEK 5_1 Stack

RECALL: ADT’S
Primitive data types = int, string, char… (support basic functions like add, subtract
…)
User defined data types= user will define data types + user define operations as well.
To simplify the process of solving problems, we combine the data structures with
their operations and we call this “Abstract Datatype.
 1. Declaration of data
 2. Declaration of operators
When defining ADT’s dont worry about implementation details. They come into the picture only when
we want to use them. Different kinds of ADT’s are suited to different kind of applications.
STACK
A stack is a simple data structure used for storing data.
In a stack , the order in which the data arrives is important.
STACK
A stack is a linear list structure in which insertion and deletion is done at one
end called “TOP”
The last element inserted will be the first one to be deleted.
It is called as LIFO (Last in First Out ) list.
The depth of stack is the number of elements it contains.
When element is inserted in a stack, the concept is called “Push”
When element is removed from a stack , the concept is called “Pop”
Trying to pop out an empty stack is called “underflow” condition
Trying to push an element in full stack is called as “overflow” condition
STACK
STACK OPERATIONS (MAIN)
Push (int data) => insert data into the stack where top is indication. If stack is
full then overflow exception occured
int Pop () => Removes and returns the last inserted element from the stack, if
stack is empty then underflow exception occured
AUXILIARY STACK
OPERATIONS
int Top() => returns the last inserted element without removing it . If stack is
empty error occurred.
int size() => returns the no. of elements stored in stack
bool IsEmpty( ) => indicates whether stack is empty or not. Returned true if
stack is empty else return false.
Bool IsFull( )=> indicates whether stack is full or not. Return true if stack is
full else return false.
STACK
IMPLEMENTATION
USING ARRAYS
IMPLEMENTING STACK USING
ARRAY
STACK

1 2 3 4 5 6 7 8

1
MAXSTK 8
TOP
PUSH()
This procedure pushes an ITEM onto a stack

PUSH(STACK,TOP,MAXSTK,ITEM)
1. IF TOP = MAXSTK, then
Print : Overflow, and Return
2. Set TOP = TOP + 1 [Increase TOP by 1]
3. Set STACK[TOP]= ITEM [Insert ITEM in new TOP
position]
4. Return
POP()
This procedure deletes the top element of stack and assigns it to the variable ITEM

POP(STACK,TOP,ITEM)
1. IF TOP = 0, then
Print : Underflow, and Return
2. Set ITEM = STACK[TOP] [Assigns TOP element to ITEM]
3. Set TOP= TOP - 1 [Decreases TOP by 1]
4. Return
LIMITATIONS
– The maximum size of the stack must be defined priori and
cannot be changed

– Trying to push a new element into a full stack causes an


implementation-specific exception
QUESTION 1
Consider the following stack of characters, where STACK is allocated N=8
memory cells.
A C D F K

Describe the stack as the following operations take


place. TOP
a) POP(STACK,ITEM)
b) POP(STACK,ITEM)
c) PUSH(STACK,L)
d) PUSH(STACK,P)
e) POP(STACK,ITEM)
f) PUSH(STACK,R)
g) PUSH(STACK,S)
QUESTION 2
Suppose STACK is allocated N=6 memory cells and initially STACK is empty. Find
the output of the following procedure.
1. Set A=2 and B=5
2. Call PUSH(STACK,A)
Call PUSH(STACK,4)
Call PUSH(STACK,B+2)
Call PUSH(STACK,9)
Call PUSH(STACK,A+B)
3. Repeat while Top!= 0
Call POP(STACK,ITEM)
End of loop
APPLICATIONS OF STACK
Implementing Function calling (memory management)
Balancing of Symbols (parenthesis opening and closing)
Matching tags in HTML or XML
Evaluating expressions
Page visited history in a Web browser
Infix-to-postfix conversion
EVALUATING EXPRESSION
An algebraic expression is a legal combination of operands and the operators.
a/b-c+d*e-a*c
• Operand is the quantity (unit of data) on which a mathematical
operation is performed.
• Operand may be a variable like x, y, z or a constant like 5, 4,0,9,1 etc.
•Operator is a symbol which signifies a mathematical or logical
operation between the operands.
• Example of familiar operators include +,-,*, /, ^
EXPRESSION EVALUATION
X=a/b-c+d*e-a*c
a = 4, b = c = 2, d = e = 3
PRECEDENCE
When an operand is in between two different operators, which operator will take the
operand first, is decided by the precedence of an operator over others.

a + b * c --> a + ( b * c )
Here, multiplication operation has precedence over addition, b * c will be evaluated
first.
ASSOCIATIVITY
For example, in expression a + b − c, both + and – have the same
precedence

• Here, both + and − are left associative,


so the expression will be evaluated as (a + b) − c.
PRIORITY OF OPERATORS
SOME EXAMPLES ….
1. a+b*c
2. a*b+c/d
3. a*b/c/d
INFIX, POSTFIX, PREFIX
EXPRESSIONS
Infix: in which operands surround the operator
a+b–c
Postfix: called as Reverse Polish Notation. operator comes after
the operands, e.g. xy+, xyz+*
Prefix: Known as Polish notation.In the prefix notation, as the
name only suggests, operator comes before the operands, e.g. +xy,
*+xyz etc.
INFIX TO POSTFIX
CONVERSION ALGORITHM
There is an algorithm to convert an infix expression into a postfix expression. It uses
a stack; but in this case, the stack is used to hold operators rather than numbers. The
purpose of the stack is to reverse the order of the operators in the expression. It also
serves as a storage structure, since no operator can be printed until both of its
operands have appeared.
In this algorithm, all operands are printed (or sent to output) when they are read.
There are more complicated rules to handle operators and parentheses.
INFIX TO POSTFIX
CONVERSION ALGORITHM
POLISH(Q,P)
Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression
P.
1. Push “(“ onto STACK , add “)” to the end of Q.
2. Scan Q from left to right and repeat Step 3 to 6 for each element Q until the STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is countered push it onto the STACK
5. If an operator ɸ is encountered then
a. Repeatedly pop from the STACK and add to P each operator (on the top of the STACK) which has the same
precedence as or higher precedence than ɸ .
b. Add ɸ to STACK
[End of If Structure]
INFIX TO POSTFIX
CONVERSION ALGORITHM
(CON’D)
6. If a right parenthesis is encountered, then:
a. Repeatedly pop from the STACK and add to P each operator (on the top of the STACK) 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 If structure]
7. Exit
EXAMPLE
current operator stack postfix string
symbol
1 A A
2 * * A
3 B * AB
4 + + A B * {pop and print the '*' before
pushing the '+'}
5 C + AB * C
6   AB * C +
EXERCISE
 A+ B * C
 A* (B+C)
A-B+C
A * B ^ C + D
A * (B + C * D) + E
ANSWERS OF EXERCISE
A B C * +
A B C + *
A B - C +
A B C ^ * D +
A B C D * + * E +
INFIX TO POSTFIX: PRACTICE EXERCISE

A. 3 + 5 * 6 – 7 * (8 + 5)=
B. A/B-C+D*E-A*C =
C. 5+ 3 + 4 + 1=
D. (5 + 3) * 10=
E. (b * b – 4 * a * c) / (2 * a) =
POSTFIX NOTATION

A compiler accepts an expression and produces correct code by reworking the


expression into a form called postfix notation.
The conventional way of writing an expression is called infix- the operators come in-
between the operands
Infix A*B/C has postfix AB*C/.
Infix: A/B-C+D*E-A*C
Postfix: AB/C-DE*+AC*-
POSTFIX EVALUATION
ALGORITHM
This algorithm finds the VALUE of an arithmetic expression P written in post fix notation.
1.Add a right parenthesis ”)” at the end of P [This acts as sentinel]
2. Scan P from left to right and repeat Step 3 and 4 for each element of P until the sentinel “)” 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 to the STACK

[End of if structure]
[End of step 2 loop]
5. Set VALUE equal to the top element on STACK
6. Exit
EXAMPLE
Symbol STACK
P: 5,6,2,+,*,12,4,/,- Scanned
5 5
6 5,6
2 5,6,2
+ 5,8
* 40
12 40,12
4 40,12,4
/ 40,3
- 37
)
POSTFIX EXPRESSION
EVALUATION
P: 5,6,2,+,*,12,4,/,-

You might also like