SlideShare a Scribd company logo
1 of 61
Download to read offline
College of Computing & Information Technology
King Abdulaziz University
CPCS-204 – Data Structures I
Stacks & Their Applications
(Postfix/Infix)
Stacks & Their Applications page 2
© Dr. Jonathan (Yahya) Cazalas
Outline
 Stacks
 What are they and how they work
 Stack Applications
 Infix to Postfix conversion
 Evaluation of Postfix expressions
Stacks & Their Applications page 3
© Dr. Jonathan (Yahya) Cazalas
Stacks – An Overview
 Abstract Data Type (ADT):
 What is an Abstract Data Type?
 To answer this, let us ask the negative of this:
 What is NOT an Abstract Data Type (in JAVA)?
 int
 int is a built-in type in the JAVA language
 double
 double is a built-in type in the JAVA language
 There are certainly many others we can list
 These data types are already built into the language.
Stacks & Their Applications page 4
© Dr. Jonathan (Yahya) Cazalas
Stacks – An Overview
 Abstract Data Type (ADT):
 So again, what is an Abstract Data Type?
 It is a data type that is NOT built into the language
 (well, for our purposes, it isn’t!)
 It is a data type that we will “build”
 We will specify what it is, how it is used, etc.
 It is often defined in terms of its behavior rather than
its implemented representation
 Nice definition from Wikipedia:
 An abstract data type is defined indirectly, only by the
operations that may be performed on it (i.e. behavior)
 http://en.wikipedia.org/wiki/Abstract_data_type
Stacks & Their Applications page 5
© Dr. Jonathan (Yahya) Cazalas
Stacks – An Overview
 Stacks:
 Stacks are an Abstract Data Type
 Does this mean they are *not* built into JAVA?
 Yes and no!
 You can say that stacks are not built into the core feature
set of JAVA.
 However, over time, many new features (classes) were
added to JAVA.
 Therefore, yes, stacks are now part of JAVA
 There is a class called “Stack”
 However, for the purposes of this class (CPCS-204), we
first *assume* that there is no built-in stack class
 We want to built it ourself!
Stacks & Their Applications page 6
© Dr. Jonathan (Yahya) Cazalas
Stacks – An Overview
 Stacks:
 Stacks are an Abstract Data Type
 So for our discussion, we say they are not built into
JAVA
 We must define them and their behaviors
 So what is a stack?
 A data structure that stores information in the form of a
stack.
 Consists of a variable number of homogeneous elements
 i.e. elements of the same type
Stacks & Their Applications page 7
© Dr. Jonathan (Yahya) Cazalas
Stacks – An Overview
 Stacks:
 Access Policy:
 The access policy for a stack is simple: the first element
to be removed from the stack is the last element that was
placed onto the stack
 The main idea is that the last item placed on to the stack is the
first item removed from the stack
 Known as the “Last in, First out” access policy
 LIFO for short
 The classical example of a stack is cafeteria trays.
 New, clean trays are added to the top of the stack.
 and trays are also taken from the top
 So the last tray in is the first tray taken out
Stacks & Their Applications page 8
© Dr. Jonathan (Yahya) Cazalas
Stacks – An Overview
 Stacks:
 Basic Operations:
 PUSH:
 This PUSHes an item on top of the stack
 POP:
 This POPs off the top item in the stack and returns it
 Other important tidbit:
 The end of the stack,
 where PUSHes and POPs occur,
 is usually referred to as the TOP of the stack
Stacks & Their Applications page 9
© Dr. Jonathan (Yahya) Cazalas
Stacks – An Overview
Top
6
Element to be inserted into S
(before push) (after push)
24
13
7
22
9
Top
Stack S Stack S
6
24
13
7
22
9
 PUSH Operation:
Stacks & Their Applications page 10
© Dr. Jonathan (Yahya) Cazalas
 POP Operation:
Stacks – An Overview
6
24
13
7
22
9
Top
Element removed
6
(stack after pop)
24
13
7
22
9
Top
(stack before pop)
6
Stacks & Their Applications page 11
© Dr. Jonathan (Yahya) Cazalas
Stacks – An Overview
 Stacks:
 Other useful operations:
 isEmpty:
 Typically implemented as a boolean function
 Returns TRUE if no items are in the stacck
 isFull:
 Returns TRUE if no more items can be added to the stack
 In theory, a stack should NEVER become full
 Actual implementations do have limits on the number of
elements a stack can store
 top:
 Simply returns the value at the top of the stack without actually
popping the stack.
Stacks & Their Applications page 12
© Dr. Jonathan (Yahya) Cazalas
Stacks – An Overview
 Stacks:
 Other useful operations:
 Note:
 Each of those operations ACCESS the stack
 But they do NOT modify the stack
 A PUSH can only be done if the stack isn’t full
 A POP can only be done on a non-empty stack
 Implementation of a stack:
 Can be done using both static and dynamic memory
 Array or a linked list
 Implemented as an array, the stack could possibly become full
 As a linked list, this is MUCH LESS LIKELY to occur
 We will cover detailed implementations NEXT TIME.
Stacks & Their Applications page 13
© Dr. Jonathan (Yahya) Cazalas
Using Stacks
 So when is stack useful?
 When data needs to be stored and then retrieved
in reverse order
 There are several examples/applications outside
the scope of this class
 Be patient and they will come up
 For now, we go over two classical examples…
 This examples help facilitate learning about stacks and
their operations.
Stacks & Their Applications page 14
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Evaluating Arithmetic Expressions
 Consider the expression 5 * 3 + 2 + 6 * 4
 How do we evaluate this?
 DUH
 No, but seriously, think about what happens during the
evaluation
 We multiply 5 and 3 to get 15, and then we add 2 to
that result to get 17.
 Then we store that off in our head somewhere.
 We then multiply 6 and 4 to get 24
 Lastly, we then retrieve the stored value (17) and add
it to 24 to get the result…41.
Stacks & Their Applications page 15
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Evaluating Arithmetic Expressions
 Consider the expression 5 * 3 + 2 + 6 * 4
 That was only easy cuz we knowza some math
and rules of precedence, etc.
 What if you didn’t know those rules?
 Well, there’s an easy way of writing out this sequence
of events.
 It does seem weird at first glance…but it works!
 5 3 * 2 + 6 4 * +
 you read this left to right
 the operators are ALWAYS in the correct evaluation
order
 This notation is called postfix notation.
Stacks & Their Applications page 16
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Basically, there are 3 types of notations for
expressions
 Infix: operator is between operands
 A + B
 Postfix: operator follows operands
 A B +
 Prefix: operator comes before operands
 + A B
 Again, in a postfix expression, operators are
ALWAYS in correct evaluation order.
Stacks & Their Applications page 17
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Evaluation of infix expressions has 2 basic
steps:
 Convert infix expression to a postfix expression.
 Evaluate the newly converted postfix
expression.
 And guess what…
 Stacks are useful in both of these steps
 Let’s start with seeing how to actually
evaluate that crazy looking expression
Stacks & Their Applications page 18
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Evaluating a Postfix Expression (A B +)
 Of course, we use a stack
 Each operator in a postfix expression refers to the
previous two operands
 When you read an operand
 PUSH it onto the stack
 When you read an operator, it’s associated operands
are POPed off the stack
 The indicated operation (based on the operand) is performed on
the two operators
 Result is PUSHed back onto the stack so it can be available for
use as an operand for the next operator.
 Process stops when there are no more operators in expression
 Final result is obtained by popping off remaining value in stack.
Stacks & Their Applications page 19
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Evaluating a Postfix Expression
 Consider the simple expression: 5 * 3 + 2 + 6 * 4
 As mentioned, this converts to the following
postfix expression: 5 3 * 2 + 6 4 * +
 So follow the rules and evaluate this!
Stacks & Their Applications page 20
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 1: We have an operand, 5. What do we do?
5
The rule stated:
When you
encounter an
operand, PUSH it
onto the stack.
So we PUSH 5
onto the stack.
Stacks & Their Applications page 21
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 2: PUSH 3 on the stack
5
3
Stacks & Their Applications page 22
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 3: We have an operator! What do we do?
5
3
1. POP the top
two operands
off the stack.
2. Perform the
indicated
operation.
3. PUSH the
result back
onto the stack.
1. So POP 3 and
5.
2. 5*3 = 15
3. PUSH 15 back
on the stack
Stacks & Their Applications page 23
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 3: We have an operator! What do we do?
15
1. POP the top
two operands
off the stack.
2. Perform the
indicated
operation.
3. PUSH the
result back
onto the stack.
1. So POP 3 and
5.
2. 5*3 = 15
3. PUSH 15 back
on the stack
Stacks & Their Applications page 24
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 4: PUSH 2 on the stack
15
2
Stacks & Their Applications page 25
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 5: We have an operator! What do we do?
15
2
1. POP the top
two operands
off the stack.
2. Perform the
indicated
operation.
3. PUSH the
result back
onto the stack.
1. So POP 2 and
15.
2. 15 + 2 = 17
3. PUSH 17 back
on the stack
Stacks & Their Applications page 26
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 5: We have an operator! What do we do?
17
1. POP the top
two operands
off the stack.
2. Perform the
indicated
operation.
3. PUSH the
result back
onto the stack.
1. So POP 2 and
15.
2. 15 + 2 = 17
3. PUSH 17 back
on the stack
Stacks & Their Applications page 27
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 6: PUSH 6 on the stack
17
6
Stacks & Their Applications page 28
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 7: PUSH 4 on the stack
17
6
4
Stacks & Their Applications page 29
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 8: We have an operator! What do we do?
17
6
4
1. POP the top
two operands
off the stack.
2. Perform the
indicated
operation.
3. PUSH the
result back
onto the stack.
1. So POP 4 and
6.
2. 6 * 4 = 24
3. PUSH 24 back
on the stack
Stacks & Their Applications page 30
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 8: We have an operator! What do we do?
17
24
1. POP the top
two operands
off the stack.
2. Perform the
indicated
operation.
3. PUSH the
result back
onto the stack.
1. So POP 4 and
6.
2. 6 * 4 = 24
3. PUSH 24 back
on the stack
Stacks & Their Applications page 31
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 9: We have an operator! What do we do?
17
24
1. POP the top
two operands
off the stack.
2. Perform the
indicated
operation.
3. PUSH the
result back
onto the stack.
1. So POP 24
and 17.
2. 17 + 24 = 41
3. PUSH 41 back
on the stack
Stacks & Their Applications page 32
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 9: We have an operator! What do we do?
41
1. POP the top
two operands
off the stack.
2. Perform the
indicated
operation.
3. PUSH the
result back
onto the stack.
1. So POP 24
and 17.
2. 17 + 24 = 41
3. PUSH 41 back
on the stack
Stacks & Their Applications page 33
© Dr. Jonathan (Yahya) Cazalas
5 3 * 2 + 6 4 * +
 Step 10: There are no more operators. So pop
the final value off the stack.
41
Result is 41
We’re Done!
Stacks & Their Applications page 34
© Dr. Jonathan (Yahya) Cazalas
Brief Interlude: Human Stupidity
Stacks & Their Applications page 35
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Again, there are 2 steps to evaluate infix
expressions.
1. Evaluate the postfix expression (once
converted)
 Been there, done that
 (for those that were sleeping, that was the long previous
example)
2. But before we can evaluate, we must first
convert the infix exp. to a postfix exp.
 Infix: 5 * 3 + 2 + 6 * 4
 Postfix: 5 3 * 2 + 6 4 * +
 How do we do this…
Stacks & Their Applications page 36
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Converting Infix Exp. to Postfix Exp.
 Again, we use a stack
 But now, this is strictly an “operator only” stack
 Only the operators are stored on the stack
 Upon reading an operand, the operand is immediately
placed into output list (printed out straight away).
 There are several rules on how this stack should be
used
 But with an example, it should be easy to understand
Stacks & Their Applications page 37
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Converting Infix Exp. to Postfix Exp.
 Rules
1. Assume the operation is a legal one (meaning, it is possible to evaluate it).
2. Upon reading an operand, it is immediately placed into the output list (printed straight away).
3. Only the operators are placed in the stack.
4. To start, the stack is empty. The infix expression is read left to right.
5. The first operator read is pushed directly onto the stack. For all subsequent operators, the
priority of the “incoming-operator” (the one being read) will be compared with the operator on
the top of the stack.
6. If the priority of the incoming-operator is higher than the priority of the operator on the top of
the stack, then the incoming-operator will be simply PUSHed on the stack.
7. If the priority of the incoming-operator is same or lower than the priority of the operator at the
top of the stack, then the operator at top of the stack will be POPed and printed on the output
expression.
8. The process is repeated if the priority of the incoming-operator is still same or lower than the
next operator-in-the stack.
9. When a left parenthesis is encountered in the expression it is immediately pushed on the
stack, as it has the highest priority. However, once it is inside the stack, all other operators
are pushed on top of it, as its inside-stack priority is lowest.
10. When a right parenthesis is encountered, all operators up to the left parenthesis are popped
from the stack and printed out. The left and right parentheses will be discarded. When all
characters from the input infix expression have been read, the operators remaining inside the
stack, are printed out in the order in which they are popped.
Stacks & Their Applications page 38
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Converting Infix Exp. to Postfix Exp.
 Rules
 One more thing, before we begin, we must know the
order of precedence for the operators
 The priority is as follows, with the first being the top
priority (highest precedence)
1. ( Left parenthesis inside the expression
2. * /
3. + -
4. ( Left parenthesis inside the stack
 The left parenthesis has the highest priority when it is
read from the expression, but once it is on the stack,
it assumes the lowest priority.
Stacks & Their Applications page 39
© Dr. Jonathan (Yahya) Cazalas
5 * 3 + 2 + 6 * 4
 Step 1: 5 is an operand. It is placed directly onto
output list.
Resulting Postfix Expression: 5
Stacks & Their Applications page 40
© Dr. Jonathan (Yahya) Cazalas
5 * 3 + 2 + 6 * 4
 Step 2: * is an operator. The stack is empty; so
PUSH * into the stack.
*
Resulting Postfix Expression: 5
Stacks & Their Applications page 41
© Dr. Jonathan (Yahya) Cazalas
5 * 3 + 2 + 6 * 4
 Step 3: 3 is an operand. It is placed directly onto
output list.
*
Resulting Postfix Expression: 3
5
Stacks & Their Applications page 42
© Dr. Jonathan (Yahya) Cazalas
5 * 3 + 2 + 6 * 4
 Step 4: + is an operator. The stack is not empty;
compare precedence of + to *.
*
Resulting Postfix Expression: 3
5
Stacks & Their Applications page 43
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Converting Infix Exp. to Postfix Exp.
 Rules
 One more thing, before we begin, we must know the
order of precedence for the operators
 The priority is as follows, with the first being the
top priority (highest precedence)
1. ( Left parenthesis inside the expression
2. * /
3. + -
4. ( Left parenthesis inside the stack
 The left parenthesis has the highest priority when it is
read from the expression, but once it is on the stack, it
assumes the lowest priority.
Stacks & Their Applications page 44
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Converting Infix Exp. to Postfix Exp.
 Rules
1. Assume the operation is a legal one (meaning, it is possible to evaluate it).
2. Upon reading an operand, it is immediately placed into the output list (printed straight away).
3. Only the operators are placed in the stack.
4. To start, the stack is empty. The infix expression is read left to right.
5. The first operator read is pushed directly onto the stack. For all subsequent operators, the
priority of the “incoming-operator” (the one being read) will be compared with the operator on
the top of the stack.
6. If the priority of the incoming-operator is higher than the priority of the operator on the top of
the stack, then the incoming-operator will be simply PUSHed on the stack.
7. If the priority of the incoming-operator is same or lower than the priority of the
operator at the top of the stack, then the operator at top of the stack will be POPed and
printed on the output expression.
8. The process is repeated if the priority of the incoming-operator is still same or lower than the
next operator-in-the stack.
9. When a left parenthesis is encountered in the expression it is immediately pushed on the
stack, as it has the highest priority. However, once it is inside the stack, all other operators
are pushed on top of it, as its inside-stack priority is lowest.
10. When a right parenthesis is encountered, all operators up to the left parenthesis are popped
from the stack and printed out. The left and right parentheses will be discarded. When all
characters from the input infix expression have been read, the operators remaining inside the
stack, are printed out in the order in which they are popped.
Stacks & Their Applications page 45
© Dr. Jonathan (Yahya) Cazalas
5 * 3 + 2 + 6 * 4
 Step 4: + is an operator. The stack is not empty;
compare precedence of + to *.
*
Resulting Postfix Expression: *
3
5
+ is lower priority
than *.
So we POP * and
PUSH + onto the
stack.
+
Stacks & Their Applications page 46
© Dr. Jonathan (Yahya) Cazalas
5 * 3 + 2 + 6 * 4
 Step 5: 2 is an operand. It is placed directly onto
output list.
+
Resulting Postfix Expression: 2
*
3
5
Stacks & Their Applications page 47
© Dr. Jonathan (Yahya) Cazalas
5 * 3 + 2 + 6 * 4
 Step 6: + is an operator. The stack is not empty;
compare precedence of + to +.
+
Resulting Postfix Expression: 2
*
3
5
Stacks & Their Applications page 48
© Dr. Jonathan (Yahya) Cazalas
5 * 3 + 2 + 6 * 4
 Step 6: + is an operator. The stack is not empty;
compare precedence of + to +.
+
Resulting Postfix Expression: +
2
*
3
5
+ is same priority
as +.
So we POP +
and PUSH + onto
the stack.
+
Stacks & Their Applications page 49
© Dr. Jonathan (Yahya) Cazalas
5 * 3 + 2 + 6 * 4
 Step 7: 6 is an operand. It is placed directly onto
output list.
+
Resulting Postfix Expression: 6
+
2
*
3
5
Stacks & Their Applications page 50
© Dr. Jonathan (Yahya) Cazalas
5 * 3 + 2 + 6 * 4
 Step 8: * is an operator. The stack is not empty;
compare precedence of * to +.
+
Resulting Postfix Expression: 6
+
2
*
3
5
Stacks & Their Applications page 51
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Converting Infix Exp. to Postfix Exp.
 Rules
 One more thing, before we begin, we must know the
order of precedence for the operators
 The priority is as follows, with the first being the
top priority (highest precedence)
1. ( Left parenthesis inside the expression
2. * /
3. + -
4. ( Left parenthesis inside the stack
 The left parenthesis has the highest priority when it is
read from the expression, but once it is on the stack, it
assumes the lowest priority.
Stacks & Their Applications page 52
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Converting Infix Exp. to Postfix Exp.
 Rules
1. Assume the operation is a legal one (meaning, it is possible to evaluate it).
2. Upon reading an operand, it is immediately placed into the output list (printed straight away).
3. Only the operators are placed in the stack.
4. To start, the stack is empty. The infix expression is read left to right.
5. The first operator read is pushed directly onto the stack. For all subsequent operators, the
priority of the “incoming-operator” (the one being read) will be compared with the operator on
the top of the stack.
6. If the priority of the incoming-operator is higher than the priority of the operator on the
top of the stack, then the incoming-operator will be simply PUSHed on the stack.
7. If the priority of the incoming-operator is same or lower than the priority of the operator at the
top of the stack, then the operator at top of the stack will be POPed and printed on the output
expression.
8. The process is repeated if the priority of the incoming-operator is still same or lower than the
next operator-in-the stack.
9. When a left parenthesis is encountered in the expression it is immediately pushed on the
stack, as it has the highest priority. However, once it is inside the stack, all other operators
are pushed on top of it, as its inside-stack priority is lowest.
10. When a right parenthesis is encountered, all operators up to the left parenthesis are popped
from the stack and printed out. The left and right parentheses will be discarded. When all
characters from the input infix expression have been read, the operators remaining inside the
stack, are printed out in the order in which they are popped.
Stacks & Their Applications page 53
© Dr. Jonathan (Yahya) Cazalas
5 * 3 + 2 + 6 * 4
 Step 8: * is an operator. The stack is not empty;
compare precedence of * to +.
+
*
Resulting Postfix Expression: 6
+
2
*
3
5
* is a higher
priority than +.
So we simply
PUSH * onto the
stack.
Stacks & Their Applications page 54
© Dr. Jonathan (Yahya) Cazalas
5 * 3 + 2 + 6 * 4
 Step 9: 4 is an operand. It is placed directly onto
output list.
+
*
Resulting Postfix Expression: 4
6
+
2
*
3
5
Stacks & Their Applications page 55
© Dr. Jonathan (Yahya) Cazalas
5 * 3 + 2 + 6 * 4
 Step 10: Infix exp. has been completely read.
POP remaining operators that are in the stack.
+
*
Resulting Postfix Expression: *
4
6
+
2
*
3
5 +
And now we’re done.
We have an equivalent
postfix expression.
Stacks & Their Applications page 56
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Two more examples:
The contents of the operator stack at the indicated points in the infix expressions (points A, B and C) are
shown below for each case
Stacks & Their Applications page 57
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 Last example:
The contents of the operator stack at the indicated points in the infix expressions (points A, B and C) are
shown below for each case
Stacks & Their Applications page 58
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
 You now know how to:
1. Convert an Infix expression to a Postfix
expression
2. And then evaluate that resulting expression
Stacks & Their Applications page 59
© Dr. Jonathan (Yahya) Cazalas
Stack Application(s)
WASN’T
THAT
DANDY!
Stacks & Their Applications page 60
© Dr. Jonathan (Yahya) Cazalas
Daily Demotivator
College of Computing & Information Technology
King Abdulaziz University
CPCS-204 – Data Structures I
Stacks & Their Applications
(Postfix/Infix)

More Related Content

What's hot

1 list datastructures
1 list datastructures1 list datastructures
1 list datastructuresNguync91368
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structureMahmoud Alfarra
 
23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-dequesRishabh Jindal
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queueAmit Vats
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsLovelitJose
 
Advanced data structures slide 2 2+
Advanced data structures slide 2 2+Advanced data structures slide 2 2+
Advanced data structures slide 2 2+jomerson remorosa
 
Data Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and QueuesData Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and QueuesFerdin Joe John Joseph PhD
 
How and why I turned my old Java projects into a first-class serverless compo...
How and why I turned my old Java projects into a first-class serverless compo...How and why I turned my old Java projects into a first-class serverless compo...
How and why I turned my old Java projects into a first-class serverless compo...Mario Fusco
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Roman Rodomansky
 

What's hot (20)

2 a stacks
2 a stacks2 a stacks
2 a stacks
 
2 b queues
2 b queues2 b queues
2 b queues
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structure
 
23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-deques
 
Recursion Pattern Analysis and Feedback
Recursion Pattern Analysis and FeedbackRecursion Pattern Analysis and Feedback
Recursion Pattern Analysis and Feedback
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queue
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-Expressions
 
Stacks
StacksStacks
Stacks
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
Advanced data structures slide 2 2+
Advanced data structures slide 2 2+Advanced data structures slide 2 2+
Advanced data structures slide 2 2+
 
3 Array operations
3   Array operations3   Array operations
3 Array operations
 
binary_trees2
binary_trees2binary_trees2
binary_trees2
 
Lesson 6 recursion
Lesson 6  recursionLesson 6  recursion
Lesson 6 recursion
 
Data Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and QueuesData Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and Queues
 
How and why I turned my old Java projects into a first-class serverless compo...
How and why I turned my old Java projects into a first-class serverless compo...How and why I turned my old Java projects into a first-class serverless compo...
How and why I turned my old Java projects into a first-class serverless compo...
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Lesson 5 link list
Lesson 5  link listLesson 5  link list
Lesson 5 link list
 

Similar to stacks1

Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfarpitaeron555
 
Functional programming is the most extreme programming
Functional programming is the most extreme programmingFunctional programming is the most extreme programming
Functional programming is the most extreme programmingsamthemonad
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5ecomputernotes
 
IRJET- Comparison of Stack and Queue Data Structures
IRJET- Comparison of Stack and Queue Data StructuresIRJET- Comparison of Stack and Queue Data Structures
IRJET- Comparison of Stack and Queue Data StructuresIRJET Journal
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problemecomputernotes
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptxline24arts
 
ContentsCOSC 2436 – LAB4TITLE .............................docx
ContentsCOSC 2436 – LAB4TITLE .............................docxContentsCOSC 2436 – LAB4TITLE .............................docx
ContentsCOSC 2436 – LAB4TITLE .............................docxdickonsondorris
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfkarymadelaneyrenne19
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxYogesh Pawar
 
Lewis jssap3 e_labman02
Lewis jssap3 e_labman02Lewis jssap3 e_labman02
Lewis jssap3 e_labman02auswhit
 
EN1320 Module 2 Lab 2.1Capturing the Reader’s InterestSelec.docx
EN1320 Module 2 Lab 2.1Capturing the Reader’s InterestSelec.docxEN1320 Module 2 Lab 2.1Capturing the Reader’s InterestSelec.docx
EN1320 Module 2 Lab 2.1Capturing the Reader’s InterestSelec.docxYASHU40
 
advancedzplmacroprogramming_081820.pptx
advancedzplmacroprogramming_081820.pptxadvancedzplmacroprogramming_081820.pptx
advancedzplmacroprogramming_081820.pptxssuser6a1dbf
 
22827361 ab initio-fa-qs
22827361 ab initio-fa-qs22827361 ab initio-fa-qs
22827361 ab initio-fa-qsCapgemini
 
OverviewUsing the C-struct feature, design, implement and .docx
OverviewUsing the C-struct feature, design, implement and .docxOverviewUsing the C-struct feature, design, implement and .docx
OverviewUsing the C-struct feature, design, implement and .docxalfred4lewis58146
 

Similar to stacks1 (20)

Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Javascript
JavascriptJavascript
Javascript
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
 
Functional programming is the most extreme programming
Functional programming is the most extreme programmingFunctional programming is the most extreme programming
Functional programming is the most extreme programming
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
 
IRJET- Comparison of Stack and Queue Data Structures
IRJET- Comparison of Stack and Queue Data StructuresIRJET- Comparison of Stack and Queue Data Structures
IRJET- Comparison of Stack and Queue Data Structures
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problem
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
ContentsCOSC 2436 – LAB4TITLE .............................docx
ContentsCOSC 2436 – LAB4TITLE .............................docxContentsCOSC 2436 – LAB4TITLE .............................docx
ContentsCOSC 2436 – LAB4TITLE .............................docx
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Lewis jssap3 e_labman02
Lewis jssap3 e_labman02Lewis jssap3 e_labman02
Lewis jssap3 e_labman02
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
EN1320 Module 2 Lab 2.1Capturing the Reader’s InterestSelec.docx
EN1320 Module 2 Lab 2.1Capturing the Reader’s InterestSelec.docxEN1320 Module 2 Lab 2.1Capturing the Reader’s InterestSelec.docx
EN1320 Module 2 Lab 2.1Capturing the Reader’s InterestSelec.docx
 
advancedzplmacroprogramming_081820.pptx
advancedzplmacroprogramming_081820.pptxadvancedzplmacroprogramming_081820.pptx
advancedzplmacroprogramming_081820.pptx
 
22827361 ab initio-fa-qs
22827361 ab initio-fa-qs22827361 ab initio-fa-qs
22827361 ab initio-fa-qs
 
OverviewUsing the C-struct feature, design, implement and .docx
OverviewUsing the C-struct feature, design, implement and .docxOverviewUsing the C-struct feature, design, implement and .docx
OverviewUsing the C-struct feature, design, implement and .docx
 
CLIPS Basic Student Guide
CLIPS Basic Student GuideCLIPS Basic Student Guide
CLIPS Basic Student Guide
 

More from Mohamed Elsayed (20)

binary_trees4
binary_trees4binary_trees4
binary_trees4
 
binary_trees3
binary_trees3binary_trees3
binary_trees3
 
binary_trees1
binary_trees1binary_trees1
binary_trees1
 
algorithm_analysis2
algorithm_analysis2algorithm_analysis2
algorithm_analysis2
 
algorithm_analysis1
algorithm_analysis1algorithm_analysis1
algorithm_analysis1
 
recursion2
recursion2recursion2
recursion2
 
recursion1
recursion1recursion1
recursion1
 
linked_lists4
linked_lists4linked_lists4
linked_lists4
 
linked_lists3
linked_lists3linked_lists3
linked_lists3
 
Linked_lists2
Linked_lists2Linked_lists2
Linked_lists2
 
linked_lists
linked_listslinked_lists
linked_lists
 
sorted_listmatch
sorted_listmatchsorted_listmatch
sorted_listmatch
 
binary_search
binary_searchbinary_search
binary_search
 
arrays
arraysarrays
arrays
 
heaps2
heaps2heaps2
heaps2
 
heaps
heapsheaps
heaps
 
hash_tables
hash_tableshash_tables
hash_tables
 
graphs
graphsgraphs
graphs
 
quick_sort
quick_sortquick_sort
quick_sort
 
merge_sort
merge_sortmerge_sort
merge_sort
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 

Recently uploaded (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 

stacks1

  • 1. College of Computing & Information Technology King Abdulaziz University CPCS-204 – Data Structures I Stacks & Their Applications (Postfix/Infix)
  • 2. Stacks & Their Applications page 2 © Dr. Jonathan (Yahya) Cazalas Outline  Stacks  What are they and how they work  Stack Applications  Infix to Postfix conversion  Evaluation of Postfix expressions
  • 3. Stacks & Their Applications page 3 © Dr. Jonathan (Yahya) Cazalas Stacks – An Overview  Abstract Data Type (ADT):  What is an Abstract Data Type?  To answer this, let us ask the negative of this:  What is NOT an Abstract Data Type (in JAVA)?  int  int is a built-in type in the JAVA language  double  double is a built-in type in the JAVA language  There are certainly many others we can list  These data types are already built into the language.
  • 4. Stacks & Their Applications page 4 © Dr. Jonathan (Yahya) Cazalas Stacks – An Overview  Abstract Data Type (ADT):  So again, what is an Abstract Data Type?  It is a data type that is NOT built into the language  (well, for our purposes, it isn’t!)  It is a data type that we will “build”  We will specify what it is, how it is used, etc.  It is often defined in terms of its behavior rather than its implemented representation  Nice definition from Wikipedia:  An abstract data type is defined indirectly, only by the operations that may be performed on it (i.e. behavior)  http://en.wikipedia.org/wiki/Abstract_data_type
  • 5. Stacks & Their Applications page 5 © Dr. Jonathan (Yahya) Cazalas Stacks – An Overview  Stacks:  Stacks are an Abstract Data Type  Does this mean they are *not* built into JAVA?  Yes and no!  You can say that stacks are not built into the core feature set of JAVA.  However, over time, many new features (classes) were added to JAVA.  Therefore, yes, stacks are now part of JAVA  There is a class called “Stack”  However, for the purposes of this class (CPCS-204), we first *assume* that there is no built-in stack class  We want to built it ourself!
  • 6. Stacks & Their Applications page 6 © Dr. Jonathan (Yahya) Cazalas Stacks – An Overview  Stacks:  Stacks are an Abstract Data Type  So for our discussion, we say they are not built into JAVA  We must define them and their behaviors  So what is a stack?  A data structure that stores information in the form of a stack.  Consists of a variable number of homogeneous elements  i.e. elements of the same type
  • 7. Stacks & Their Applications page 7 © Dr. Jonathan (Yahya) Cazalas Stacks – An Overview  Stacks:  Access Policy:  The access policy for a stack is simple: the first element to be removed from the stack is the last element that was placed onto the stack  The main idea is that the last item placed on to the stack is the first item removed from the stack  Known as the “Last in, First out” access policy  LIFO for short  The classical example of a stack is cafeteria trays.  New, clean trays are added to the top of the stack.  and trays are also taken from the top  So the last tray in is the first tray taken out
  • 8. Stacks & Their Applications page 8 © Dr. Jonathan (Yahya) Cazalas Stacks – An Overview  Stacks:  Basic Operations:  PUSH:  This PUSHes an item on top of the stack  POP:  This POPs off the top item in the stack and returns it  Other important tidbit:  The end of the stack,  where PUSHes and POPs occur,  is usually referred to as the TOP of the stack
  • 9. Stacks & Their Applications page 9 © Dr. Jonathan (Yahya) Cazalas Stacks – An Overview Top 6 Element to be inserted into S (before push) (after push) 24 13 7 22 9 Top Stack S Stack S 6 24 13 7 22 9  PUSH Operation:
  • 10. Stacks & Their Applications page 10 © Dr. Jonathan (Yahya) Cazalas  POP Operation: Stacks – An Overview 6 24 13 7 22 9 Top Element removed 6 (stack after pop) 24 13 7 22 9 Top (stack before pop) 6
  • 11. Stacks & Their Applications page 11 © Dr. Jonathan (Yahya) Cazalas Stacks – An Overview  Stacks:  Other useful operations:  isEmpty:  Typically implemented as a boolean function  Returns TRUE if no items are in the stacck  isFull:  Returns TRUE if no more items can be added to the stack  In theory, a stack should NEVER become full  Actual implementations do have limits on the number of elements a stack can store  top:  Simply returns the value at the top of the stack without actually popping the stack.
  • 12. Stacks & Their Applications page 12 © Dr. Jonathan (Yahya) Cazalas Stacks – An Overview  Stacks:  Other useful operations:  Note:  Each of those operations ACCESS the stack  But they do NOT modify the stack  A PUSH can only be done if the stack isn’t full  A POP can only be done on a non-empty stack  Implementation of a stack:  Can be done using both static and dynamic memory  Array or a linked list  Implemented as an array, the stack could possibly become full  As a linked list, this is MUCH LESS LIKELY to occur  We will cover detailed implementations NEXT TIME.
  • 13. Stacks & Their Applications page 13 © Dr. Jonathan (Yahya) Cazalas Using Stacks  So when is stack useful?  When data needs to be stored and then retrieved in reverse order  There are several examples/applications outside the scope of this class  Be patient and they will come up  For now, we go over two classical examples…  This examples help facilitate learning about stacks and their operations.
  • 14. Stacks & Their Applications page 14 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Evaluating Arithmetic Expressions  Consider the expression 5 * 3 + 2 + 6 * 4  How do we evaluate this?  DUH  No, but seriously, think about what happens during the evaluation  We multiply 5 and 3 to get 15, and then we add 2 to that result to get 17.  Then we store that off in our head somewhere.  We then multiply 6 and 4 to get 24  Lastly, we then retrieve the stored value (17) and add it to 24 to get the result…41.
  • 15. Stacks & Their Applications page 15 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Evaluating Arithmetic Expressions  Consider the expression 5 * 3 + 2 + 6 * 4  That was only easy cuz we knowza some math and rules of precedence, etc.  What if you didn’t know those rules?  Well, there’s an easy way of writing out this sequence of events.  It does seem weird at first glance…but it works!  5 3 * 2 + 6 4 * +  you read this left to right  the operators are ALWAYS in the correct evaluation order  This notation is called postfix notation.
  • 16. Stacks & Their Applications page 16 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Basically, there are 3 types of notations for expressions  Infix: operator is between operands  A + B  Postfix: operator follows operands  A B +  Prefix: operator comes before operands  + A B  Again, in a postfix expression, operators are ALWAYS in correct evaluation order.
  • 17. Stacks & Their Applications page 17 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Evaluation of infix expressions has 2 basic steps:  Convert infix expression to a postfix expression.  Evaluate the newly converted postfix expression.  And guess what…  Stacks are useful in both of these steps  Let’s start with seeing how to actually evaluate that crazy looking expression
  • 18. Stacks & Their Applications page 18 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Evaluating a Postfix Expression (A B +)  Of course, we use a stack  Each operator in a postfix expression refers to the previous two operands  When you read an operand  PUSH it onto the stack  When you read an operator, it’s associated operands are POPed off the stack  The indicated operation (based on the operand) is performed on the two operators  Result is PUSHed back onto the stack so it can be available for use as an operand for the next operator.  Process stops when there are no more operators in expression  Final result is obtained by popping off remaining value in stack.
  • 19. Stacks & Their Applications page 19 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Evaluating a Postfix Expression  Consider the simple expression: 5 * 3 + 2 + 6 * 4  As mentioned, this converts to the following postfix expression: 5 3 * 2 + 6 4 * +  So follow the rules and evaluate this!
  • 20. Stacks & Their Applications page 20 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 1: We have an operand, 5. What do we do? 5 The rule stated: When you encounter an operand, PUSH it onto the stack. So we PUSH 5 onto the stack.
  • 21. Stacks & Their Applications page 21 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 2: PUSH 3 on the stack 5 3
  • 22. Stacks & Their Applications page 22 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 3: We have an operator! What do we do? 5 3 1. POP the top two operands off the stack. 2. Perform the indicated operation. 3. PUSH the result back onto the stack. 1. So POP 3 and 5. 2. 5*3 = 15 3. PUSH 15 back on the stack
  • 23. Stacks & Their Applications page 23 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 3: We have an operator! What do we do? 15 1. POP the top two operands off the stack. 2. Perform the indicated operation. 3. PUSH the result back onto the stack. 1. So POP 3 and 5. 2. 5*3 = 15 3. PUSH 15 back on the stack
  • 24. Stacks & Their Applications page 24 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 4: PUSH 2 on the stack 15 2
  • 25. Stacks & Their Applications page 25 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 5: We have an operator! What do we do? 15 2 1. POP the top two operands off the stack. 2. Perform the indicated operation. 3. PUSH the result back onto the stack. 1. So POP 2 and 15. 2. 15 + 2 = 17 3. PUSH 17 back on the stack
  • 26. Stacks & Their Applications page 26 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 5: We have an operator! What do we do? 17 1. POP the top two operands off the stack. 2. Perform the indicated operation. 3. PUSH the result back onto the stack. 1. So POP 2 and 15. 2. 15 + 2 = 17 3. PUSH 17 back on the stack
  • 27. Stacks & Their Applications page 27 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 6: PUSH 6 on the stack 17 6
  • 28. Stacks & Their Applications page 28 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 7: PUSH 4 on the stack 17 6 4
  • 29. Stacks & Their Applications page 29 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 8: We have an operator! What do we do? 17 6 4 1. POP the top two operands off the stack. 2. Perform the indicated operation. 3. PUSH the result back onto the stack. 1. So POP 4 and 6. 2. 6 * 4 = 24 3. PUSH 24 back on the stack
  • 30. Stacks & Their Applications page 30 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 8: We have an operator! What do we do? 17 24 1. POP the top two operands off the stack. 2. Perform the indicated operation. 3. PUSH the result back onto the stack. 1. So POP 4 and 6. 2. 6 * 4 = 24 3. PUSH 24 back on the stack
  • 31. Stacks & Their Applications page 31 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 9: We have an operator! What do we do? 17 24 1. POP the top two operands off the stack. 2. Perform the indicated operation. 3. PUSH the result back onto the stack. 1. So POP 24 and 17. 2. 17 + 24 = 41 3. PUSH 41 back on the stack
  • 32. Stacks & Their Applications page 32 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 9: We have an operator! What do we do? 41 1. POP the top two operands off the stack. 2. Perform the indicated operation. 3. PUSH the result back onto the stack. 1. So POP 24 and 17. 2. 17 + 24 = 41 3. PUSH 41 back on the stack
  • 33. Stacks & Their Applications page 33 © Dr. Jonathan (Yahya) Cazalas 5 3 * 2 + 6 4 * +  Step 10: There are no more operators. So pop the final value off the stack. 41 Result is 41 We’re Done!
  • 34. Stacks & Their Applications page 34 © Dr. Jonathan (Yahya) Cazalas Brief Interlude: Human Stupidity
  • 35. Stacks & Their Applications page 35 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Again, there are 2 steps to evaluate infix expressions. 1. Evaluate the postfix expression (once converted)  Been there, done that  (for those that were sleeping, that was the long previous example) 2. But before we can evaluate, we must first convert the infix exp. to a postfix exp.  Infix: 5 * 3 + 2 + 6 * 4  Postfix: 5 3 * 2 + 6 4 * +  How do we do this…
  • 36. Stacks & Their Applications page 36 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Converting Infix Exp. to Postfix Exp.  Again, we use a stack  But now, this is strictly an “operator only” stack  Only the operators are stored on the stack  Upon reading an operand, the operand is immediately placed into output list (printed out straight away).  There are several rules on how this stack should be used  But with an example, it should be easy to understand
  • 37. Stacks & Their Applications page 37 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Converting Infix Exp. to Postfix Exp.  Rules 1. Assume the operation is a legal one (meaning, it is possible to evaluate it). 2. Upon reading an operand, it is immediately placed into the output list (printed straight away). 3. Only the operators are placed in the stack. 4. To start, the stack is empty. The infix expression is read left to right. 5. The first operator read is pushed directly onto the stack. For all subsequent operators, the priority of the “incoming-operator” (the one being read) will be compared with the operator on the top of the stack. 6. If the priority of the incoming-operator is higher than the priority of the operator on the top of the stack, then the incoming-operator will be simply PUSHed on the stack. 7. If the priority of the incoming-operator is same or lower than the priority of the operator at the top of the stack, then the operator at top of the stack will be POPed and printed on the output expression. 8. The process is repeated if the priority of the incoming-operator is still same or lower than the next operator-in-the stack. 9. When a left parenthesis is encountered in the expression it is immediately pushed on the stack, as it has the highest priority. However, once it is inside the stack, all other operators are pushed on top of it, as its inside-stack priority is lowest. 10. When a right parenthesis is encountered, all operators up to the left parenthesis are popped from the stack and printed out. The left and right parentheses will be discarded. When all characters from the input infix expression have been read, the operators remaining inside the stack, are printed out in the order in which they are popped.
  • 38. Stacks & Their Applications page 38 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Converting Infix Exp. to Postfix Exp.  Rules  One more thing, before we begin, we must know the order of precedence for the operators  The priority is as follows, with the first being the top priority (highest precedence) 1. ( Left parenthesis inside the expression 2. * / 3. + - 4. ( Left parenthesis inside the stack  The left parenthesis has the highest priority when it is read from the expression, but once it is on the stack, it assumes the lowest priority.
  • 39. Stacks & Their Applications page 39 © Dr. Jonathan (Yahya) Cazalas 5 * 3 + 2 + 6 * 4  Step 1: 5 is an operand. It is placed directly onto output list. Resulting Postfix Expression: 5
  • 40. Stacks & Their Applications page 40 © Dr. Jonathan (Yahya) Cazalas 5 * 3 + 2 + 6 * 4  Step 2: * is an operator. The stack is empty; so PUSH * into the stack. * Resulting Postfix Expression: 5
  • 41. Stacks & Their Applications page 41 © Dr. Jonathan (Yahya) Cazalas 5 * 3 + 2 + 6 * 4  Step 3: 3 is an operand. It is placed directly onto output list. * Resulting Postfix Expression: 3 5
  • 42. Stacks & Their Applications page 42 © Dr. Jonathan (Yahya) Cazalas 5 * 3 + 2 + 6 * 4  Step 4: + is an operator. The stack is not empty; compare precedence of + to *. * Resulting Postfix Expression: 3 5
  • 43. Stacks & Their Applications page 43 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Converting Infix Exp. to Postfix Exp.  Rules  One more thing, before we begin, we must know the order of precedence for the operators  The priority is as follows, with the first being the top priority (highest precedence) 1. ( Left parenthesis inside the expression 2. * / 3. + - 4. ( Left parenthesis inside the stack  The left parenthesis has the highest priority when it is read from the expression, but once it is on the stack, it assumes the lowest priority.
  • 44. Stacks & Their Applications page 44 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Converting Infix Exp. to Postfix Exp.  Rules 1. Assume the operation is a legal one (meaning, it is possible to evaluate it). 2. Upon reading an operand, it is immediately placed into the output list (printed straight away). 3. Only the operators are placed in the stack. 4. To start, the stack is empty. The infix expression is read left to right. 5. The first operator read is pushed directly onto the stack. For all subsequent operators, the priority of the “incoming-operator” (the one being read) will be compared with the operator on the top of the stack. 6. If the priority of the incoming-operator is higher than the priority of the operator on the top of the stack, then the incoming-operator will be simply PUSHed on the stack. 7. If the priority of the incoming-operator is same or lower than the priority of the operator at the top of the stack, then the operator at top of the stack will be POPed and printed on the output expression. 8. The process is repeated if the priority of the incoming-operator is still same or lower than the next operator-in-the stack. 9. When a left parenthesis is encountered in the expression it is immediately pushed on the stack, as it has the highest priority. However, once it is inside the stack, all other operators are pushed on top of it, as its inside-stack priority is lowest. 10. When a right parenthesis is encountered, all operators up to the left parenthesis are popped from the stack and printed out. The left and right parentheses will be discarded. When all characters from the input infix expression have been read, the operators remaining inside the stack, are printed out in the order in which they are popped.
  • 45. Stacks & Their Applications page 45 © Dr. Jonathan (Yahya) Cazalas 5 * 3 + 2 + 6 * 4  Step 4: + is an operator. The stack is not empty; compare precedence of + to *. * Resulting Postfix Expression: * 3 5 + is lower priority than *. So we POP * and PUSH + onto the stack. +
  • 46. Stacks & Their Applications page 46 © Dr. Jonathan (Yahya) Cazalas 5 * 3 + 2 + 6 * 4  Step 5: 2 is an operand. It is placed directly onto output list. + Resulting Postfix Expression: 2 * 3 5
  • 47. Stacks & Their Applications page 47 © Dr. Jonathan (Yahya) Cazalas 5 * 3 + 2 + 6 * 4  Step 6: + is an operator. The stack is not empty; compare precedence of + to +. + Resulting Postfix Expression: 2 * 3 5
  • 48. Stacks & Their Applications page 48 © Dr. Jonathan (Yahya) Cazalas 5 * 3 + 2 + 6 * 4  Step 6: + is an operator. The stack is not empty; compare precedence of + to +. + Resulting Postfix Expression: + 2 * 3 5 + is same priority as +. So we POP + and PUSH + onto the stack. +
  • 49. Stacks & Their Applications page 49 © Dr. Jonathan (Yahya) Cazalas 5 * 3 + 2 + 6 * 4  Step 7: 6 is an operand. It is placed directly onto output list. + Resulting Postfix Expression: 6 + 2 * 3 5
  • 50. Stacks & Their Applications page 50 © Dr. Jonathan (Yahya) Cazalas 5 * 3 + 2 + 6 * 4  Step 8: * is an operator. The stack is not empty; compare precedence of * to +. + Resulting Postfix Expression: 6 + 2 * 3 5
  • 51. Stacks & Their Applications page 51 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Converting Infix Exp. to Postfix Exp.  Rules  One more thing, before we begin, we must know the order of precedence for the operators  The priority is as follows, with the first being the top priority (highest precedence) 1. ( Left parenthesis inside the expression 2. * / 3. + - 4. ( Left parenthesis inside the stack  The left parenthesis has the highest priority when it is read from the expression, but once it is on the stack, it assumes the lowest priority.
  • 52. Stacks & Their Applications page 52 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Converting Infix Exp. to Postfix Exp.  Rules 1. Assume the operation is a legal one (meaning, it is possible to evaluate it). 2. Upon reading an operand, it is immediately placed into the output list (printed straight away). 3. Only the operators are placed in the stack. 4. To start, the stack is empty. The infix expression is read left to right. 5. The first operator read is pushed directly onto the stack. For all subsequent operators, the priority of the “incoming-operator” (the one being read) will be compared with the operator on the top of the stack. 6. If the priority of the incoming-operator is higher than the priority of the operator on the top of the stack, then the incoming-operator will be simply PUSHed on the stack. 7. If the priority of the incoming-operator is same or lower than the priority of the operator at the top of the stack, then the operator at top of the stack will be POPed and printed on the output expression. 8. The process is repeated if the priority of the incoming-operator is still same or lower than the next operator-in-the stack. 9. When a left parenthesis is encountered in the expression it is immediately pushed on the stack, as it has the highest priority. However, once it is inside the stack, all other operators are pushed on top of it, as its inside-stack priority is lowest. 10. When a right parenthesis is encountered, all operators up to the left parenthesis are popped from the stack and printed out. The left and right parentheses will be discarded. When all characters from the input infix expression have been read, the operators remaining inside the stack, are printed out in the order in which they are popped.
  • 53. Stacks & Their Applications page 53 © Dr. Jonathan (Yahya) Cazalas 5 * 3 + 2 + 6 * 4  Step 8: * is an operator. The stack is not empty; compare precedence of * to +. + * Resulting Postfix Expression: 6 + 2 * 3 5 * is a higher priority than +. So we simply PUSH * onto the stack.
  • 54. Stacks & Their Applications page 54 © Dr. Jonathan (Yahya) Cazalas 5 * 3 + 2 + 6 * 4  Step 9: 4 is an operand. It is placed directly onto output list. + * Resulting Postfix Expression: 4 6 + 2 * 3 5
  • 55. Stacks & Their Applications page 55 © Dr. Jonathan (Yahya) Cazalas 5 * 3 + 2 + 6 * 4  Step 10: Infix exp. has been completely read. POP remaining operators that are in the stack. + * Resulting Postfix Expression: * 4 6 + 2 * 3 5 + And now we’re done. We have an equivalent postfix expression.
  • 56. Stacks & Their Applications page 56 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Two more examples: The contents of the operator stack at the indicated points in the infix expressions (points A, B and C) are shown below for each case
  • 57. Stacks & Their Applications page 57 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  Last example: The contents of the operator stack at the indicated points in the infix expressions (points A, B and C) are shown below for each case
  • 58. Stacks & Their Applications page 58 © Dr. Jonathan (Yahya) Cazalas Stack Application(s)  You now know how to: 1. Convert an Infix expression to a Postfix expression 2. And then evaluate that resulting expression
  • 59. Stacks & Their Applications page 59 © Dr. Jonathan (Yahya) Cazalas Stack Application(s) WASN’T THAT DANDY!
  • 60. Stacks & Their Applications page 60 © Dr. Jonathan (Yahya) Cazalas Daily Demotivator
  • 61. College of Computing & Information Technology King Abdulaziz University CPCS-204 – Data Structures I Stacks & Their Applications (Postfix/Infix)