SlideShare a Scribd company logo
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 1
Chapter 20 Lists, Stacks, Queues,
and Priority Queues
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 2
Objectives
 To explore the relationship between interfaces and classes in the Java
Collections Framework hierarchy (§20.2).
 To use the common methods defined in the Collection interface for operating
collections (§20.2).
 To use the Iterator interface to traverse the elements in a collection (§20.3).
 To use a for-each loop to traverse the elements in a collection (§20.3).
 To explore how and when to use ArrayList or LinkedList to store elements
(§20.4).
 To compare elements using the Comparable interface and the Comparator
interface (§20.5).
 To use the static utility methods in the Collections class for sorting, searching,
shuffling lists, and finding the largest and smallest element in collections
(§20.6).
 To develop a multiple bouncing balls application using ArrayList (§20.7).
 To distinguish between Vector and ArrayList and to use the Stack class for
creating stacks (§20.8).
 To explore the relationships among Collection, Queue, LinkedList, and
PriorityQueue and to create priority queues using the PriorityQueue class
(§20.9).
 To use stacks to write a program to evaluate expressions (§20.10).
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 3
Java Collection Framework
hierarchy
A collection is a container object that holds
a group of objects, often referred to as
elements. The Java Collections Framework
supports three types of collections, named
lists, sets, and maps.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 4
Java Collection Framework
hierarchy, cont.
Set and List are subinterfaces of Collection.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 5
The Collection Interface
The Collection interface is the root interface for manipulating a collection of obj
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 6
The List Interface
A list stores elements in a sequential order,
and allows the user to specify where the
element is stored. The user can access the
elements by index.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 7
The List Interface, cont.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 8
The List Iterator
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 9
ArrayList and LinkedList
The ArrayList class and the LinkedList class are concrete
implementations of the List interface. Which of the two
classes you use depends on your specific needs. If you
need to support random access through an index without
inserting or removing elements from any place other than
the end, ArrayList offers the most efficient collection. If,
however, your application requires the insertion or
deletion of elements from any place in the list, you should
choose LinkedList. A list can grow or shrink
dynamically. An array is fixed once it is created. If your
application does not require insertion or deletion of
elements, the most efficient data structure is the array.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 10
java.util.ArrayList
«interface»
java.util.List<E>
Creates an empty list with the default initial capacity.
Creates an array list from an existing collection.
Creates an empty list with the specified initial capacity.
Trims the capacity of this ArrayList instance to be the
list's current size.
+ArrayList()
+ArrayList(c: Collection<? extends E>)
+ArrayList(initialCapacity: int)
+trimToSize(): void
«interface»
java.util.Collection<E>
java.util.ArrayList<E>
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 11
java.util.LinkedList
«interface»
java.util.List<E>
Creates a default empty linked list.
Creates a linked list from an existing collection.
Adds the object to the head of this list.
Adds the object to the tail of this list.
Returns the first element from this list.
Returns the last element from this list.
Returns and removes the first element from this list.
Returns and removes the last element from this list.
+LinkedList()
+LinkedList(c: Collection<? extends E>)
+addFirst(o: E): void
+addLast(o: E): void
+getFirst(): E
+getLast(): E
+removeFirst(): E
+removeLast(): E
«interface»
java.util.Collection<E>
java.util.LinkedList<E>
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 12
Example: Using ArrayList and
LinkedList
This example creates an array list filled
with numbers, and inserts new elements
into the specified location in the list. The
example also creates a linked list from the
array list, inserts and removes the elements
from the list. Finally, the example traverses
the list forward and backward.
Run
TestArrayAndLinkedList
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 13
The Comparator Interface
Sometimes you want to compare the elements of
different types. The elements may not be instances of
Comparable or are not comparable. You can define a
comparator to compare these elements. To do so, define
a class that implements the java.util.Comparator
interface. The Comparator interface has two methods,
compare and equals.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 14
The Comparator Interface
public int compare(Object element1, Object element2)
Returns a negative value if element1 is less than
element2, a positive value if element1 is greater than
element2, and zero if they are equal.
GeometricObjectComparator
TestComparator
Run
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 15
The Collections Class
The Collections class contains various static methods for
operating on collections and maps, for creating
synchronized collection classes, and for creating read-
only collection classes.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 16
The Collections Class UML Diagram
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 17
Case Study: Multiple Bouncing Balls
Run
MultipleBounceBall
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 18
The Vector and Stack Classes
The Java Collections Framework was introduced
with Java 2. Several data structures were
supported prior to Java 2. Among them are the
Vector class and the Stack class. These classes
were redesigned to fit into the Java Collections
Framework, but their old-style methods are
retained for compatibility. This section
introduces the Vector class and the Stack class.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 19
The Vector Class
In Java 2, Vector is the same as ArrayList,
except that Vector contains the synchronized
methods for accessing and modifying the vector.
None of the new collection data structures
introduced so far are synchronized. If
synchronization is required, you can use the
synchronized versions of the collection classes.
These classes are introduced later in the section,
“The Collections Class.”
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 20
The Vector Class, cont.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 21
The Stack Class
The Stack class represents a last-in-first-
out stack of objects. The elements are
accessed only from the top of the stack.
You can retrieve, insert, or remove an
element from the top of the stack.
java.util.Stack<E>
+Stack()
+empty(): boolean
+peek(): E
+pop(): E
+push(o: E) : E
+search(o: Object) : int
java.util.Vector<E>
Creates an empty stack.
Returns true if this stack is empty.
Returns the top element in this stack.
Returns and removes the top element in this stack.
Adds a new element to the top of this stack.
Returns the position of the specified element in this stack.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 22
Queues and Priority Queues
A queue is a first-in/first-out data structure.
Elements are appended to the end of the queue and
are removed from the beginning of the queue. In a
priority queue, elements are assigned priorities.
When accessing elements, the element with the
highest priority is removed first.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 23
The Queue Interface
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 24
Using LinkedList for Queue
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 25
The PriorityQueue Class
Run
PriorityQueueDemo
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 26
Case Study: Evaluating Expressions
Stacks can be used to evaluate expressions.
Evaluate Expression
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 27
Algorithm
Phase 1: Scanning the expression
The program scans the expression from left to right to extract operands,
operators, and the parentheses.
1.1. If the extracted item is an operand, push it to operandStack.
1.2. If the extracted item is a + or - operator, process all the operators at the
top of operatorStack and push the extracted operator to operatorStack.
1.3. If the extracted item is a * or / operator, process the * or / operators at
the top of operatorStack and push the extracted operator to operatorStack.
1.4. If the extracted item is a ( symbol, push it to operatorStack.
1.5. If the extracted item is a ) symbol, repeatedly process the operators from
the top of operatorStack until seeing the ( symbol on the stack.
Phase 2: Clearing the stack
Repeatedly process the operators from the top of operatorStack until
operatorStack is empty.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 28
Example

More Related Content

PPTX
List interface in collections framework
PPTX
Lecture 9
PPTX
collection framework.pptx
PDF
Collection framework (completenotes) zeeshan
PPTX
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
PPT
Collections and generic class
PDF
java unit 4 pdf - about java collections
PDF
Java Collections Tutorials
List interface in collections framework
Lecture 9
collection framework.pptx
Collection framework (completenotes) zeeshan
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
Collections and generic class
java unit 4 pdf - about java collections
Java Collections Tutorials

Similar to DataStructureLists Stacks Queues and more (20)

PPTX
Advanced Java - UNIT 3.pptx
PPTX
Collections lecture 35 40
PPTX
Collections - Lists & sets
PDF
Array list (java platform se 8 )
PPTX
Java Collection Framework 2nd year B.Tech.pptx
PDF
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
PDF
Collection framework
PPT
11000121065_NAITIK CHATTERJEE.ppt
PDF
Linked list (java platform se 8 )
PPTX
Nature Activities Binder _ by Slidesgo.pptx
DOCX
ArrayList.docx
PPTX
Java Unit 2 (Part 2)
PPTX
VTUOOPMCA5THMODULECollection OverV .pptx
PPTX
mca5thCollection OverViCollection O.pptx
PPTX
VTUOOPMCA5THMODULECollection OverVi.pptx
PPTX
VTUOOPMCA5THMODULEvCollection OverV.pptx
PPTX
Java.util
PPTX
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
PPT
Ppt chapter11
PPTX
Collection
Advanced Java - UNIT 3.pptx
Collections lecture 35 40
Collections - Lists & sets
Array list (java platform se 8 )
Java Collection Framework 2nd year B.Tech.pptx
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Collection framework
11000121065_NAITIK CHATTERJEE.ppt
Linked list (java platform se 8 )
Nature Activities Binder _ by Slidesgo.pptx
ArrayList.docx
Java Unit 2 (Part 2)
VTUOOPMCA5THMODULECollection OverV .pptx
mca5thCollection OverViCollection O.pptx
VTUOOPMCA5THMODULECollection OverVi.pptx
VTUOOPMCA5THMODULEvCollection OverV.pptx
Java.util
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
Ppt chapter11
Collection
Ad

More from ssuser69ff25 (10)

PPT
Course25465NeuralNetworkDeepLearning.ppt
PPT
logisticregressionJeffWitnerMarch2016.ppt
PPT
LogisticRegressionDichotomousResponse.ppt
PPT
RegressionwithABinaryDependentVariables.ppt
PPTX
BinaryLogisticRegressionwithaDepend.pptx
PPT
Deep-Learning-2017-Lecture3FullyConnected.ppt
PPTX
Long-term real-time network traffic flow prediction using LSTM recurrent neur...
PPT
Deep-Learning-2017-Lecture3FullyConnected.ppt
PPT
dataStructure Course about lists stacks queues and more
PPTX
Long-term real-time network traffic flow prediction using LSTM recurrent neur...
Course25465NeuralNetworkDeepLearning.ppt
logisticregressionJeffWitnerMarch2016.ppt
LogisticRegressionDichotomousResponse.ppt
RegressionwithABinaryDependentVariables.ppt
BinaryLogisticRegressionwithaDepend.pptx
Deep-Learning-2017-Lecture3FullyConnected.ppt
Long-term real-time network traffic flow prediction using LSTM recurrent neur...
Deep-Learning-2017-Lecture3FullyConnected.ppt
dataStructure Course about lists stacks queues and more
Long-term real-time network traffic flow prediction using LSTM recurrent neur...
Ad

Recently uploaded (20)

PDF
R24 SURVEYING LAB MANUAL for civil enggi
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Well-logging-methods_new................
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
web development for engineering and engineering
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Lecture Notes Electrical Wiring System Components
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Sustainable Sites - Green Building Construction
PPTX
Geodesy 1.pptx...............................................
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Artificial Intelligence
R24 SURVEYING LAB MANUAL for civil enggi
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Operating System & Kernel Study Guide-1 - converted.pdf
Well-logging-methods_new................
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Mechanical Engineering MATERIALS Selection
web development for engineering and engineering
Internet of Things (IOT) - A guide to understanding
Lecture Notes Electrical Wiring System Components
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Sustainable Sites - Green Building Construction
Geodesy 1.pptx...............................................
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
OOP with Java - Java Introduction (Basics)
Foundation to blockchain - A guide to Blockchain Tech
Artificial Intelligence

DataStructureLists Stacks Queues and more

  • 1. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 1 Chapter 20 Lists, Stacks, Queues, and Priority Queues
  • 2. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 2 Objectives  To explore the relationship between interfaces and classes in the Java Collections Framework hierarchy (§20.2).  To use the common methods defined in the Collection interface for operating collections (§20.2).  To use the Iterator interface to traverse the elements in a collection (§20.3).  To use a for-each loop to traverse the elements in a collection (§20.3).  To explore how and when to use ArrayList or LinkedList to store elements (§20.4).  To compare elements using the Comparable interface and the Comparator interface (§20.5).  To use the static utility methods in the Collections class for sorting, searching, shuffling lists, and finding the largest and smallest element in collections (§20.6).  To develop a multiple bouncing balls application using ArrayList (§20.7).  To distinguish between Vector and ArrayList and to use the Stack class for creating stacks (§20.8).  To explore the relationships among Collection, Queue, LinkedList, and PriorityQueue and to create priority queues using the PriorityQueue class (§20.9).  To use stacks to write a program to evaluate expressions (§20.10).
  • 3. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 3 Java Collection Framework hierarchy A collection is a container object that holds a group of objects, often referred to as elements. The Java Collections Framework supports three types of collections, named lists, sets, and maps.
  • 4. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 4 Java Collection Framework hierarchy, cont. Set and List are subinterfaces of Collection.
  • 5. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 5 The Collection Interface The Collection interface is the root interface for manipulating a collection of obj
  • 6. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 6 The List Interface A list stores elements in a sequential order, and allows the user to specify where the element is stored. The user can access the elements by index.
  • 7. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 7 The List Interface, cont.
  • 8. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 8 The List Iterator
  • 9. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 9 ArrayList and LinkedList The ArrayList class and the LinkedList class are concrete implementations of the List interface. Which of the two classes you use depends on your specific needs. If you need to support random access through an index without inserting or removing elements from any place other than the end, ArrayList offers the most efficient collection. If, however, your application requires the insertion or deletion of elements from any place in the list, you should choose LinkedList. A list can grow or shrink dynamically. An array is fixed once it is created. If your application does not require insertion or deletion of elements, the most efficient data structure is the array.
  • 10. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 10 java.util.ArrayList «interface» java.util.List<E> Creates an empty list with the default initial capacity. Creates an array list from an existing collection. Creates an empty list with the specified initial capacity. Trims the capacity of this ArrayList instance to be the list's current size. +ArrayList() +ArrayList(c: Collection<? extends E>) +ArrayList(initialCapacity: int) +trimToSize(): void «interface» java.util.Collection<E> java.util.ArrayList<E>
  • 11. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 11 java.util.LinkedList «interface» java.util.List<E> Creates a default empty linked list. Creates a linked list from an existing collection. Adds the object to the head of this list. Adds the object to the tail of this list. Returns the first element from this list. Returns the last element from this list. Returns and removes the first element from this list. Returns and removes the last element from this list. +LinkedList() +LinkedList(c: Collection<? extends E>) +addFirst(o: E): void +addLast(o: E): void +getFirst(): E +getLast(): E +removeFirst(): E +removeLast(): E «interface» java.util.Collection<E> java.util.LinkedList<E>
  • 12. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 12 Example: Using ArrayList and LinkedList This example creates an array list filled with numbers, and inserts new elements into the specified location in the list. The example also creates a linked list from the array list, inserts and removes the elements from the list. Finally, the example traverses the list forward and backward. Run TestArrayAndLinkedList
  • 13. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 13 The Comparator Interface Sometimes you want to compare the elements of different types. The elements may not be instances of Comparable or are not comparable. You can define a comparator to compare these elements. To do so, define a class that implements the java.util.Comparator interface. The Comparator interface has two methods, compare and equals.
  • 14. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 14 The Comparator Interface public int compare(Object element1, Object element2) Returns a negative value if element1 is less than element2, a positive value if element1 is greater than element2, and zero if they are equal. GeometricObjectComparator TestComparator Run
  • 15. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 15 The Collections Class The Collections class contains various static methods for operating on collections and maps, for creating synchronized collection classes, and for creating read- only collection classes.
  • 16. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 16 The Collections Class UML Diagram
  • 17. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 17 Case Study: Multiple Bouncing Balls Run MultipleBounceBall
  • 18. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 18 The Vector and Stack Classes The Java Collections Framework was introduced with Java 2. Several data structures were supported prior to Java 2. Among them are the Vector class and the Stack class. These classes were redesigned to fit into the Java Collections Framework, but their old-style methods are retained for compatibility. This section introduces the Vector class and the Stack class.
  • 19. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 19 The Vector Class In Java 2, Vector is the same as ArrayList, except that Vector contains the synchronized methods for accessing and modifying the vector. None of the new collection data structures introduced so far are synchronized. If synchronization is required, you can use the synchronized versions of the collection classes. These classes are introduced later in the section, “The Collections Class.”
  • 20. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 20 The Vector Class, cont.
  • 21. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 21 The Stack Class The Stack class represents a last-in-first- out stack of objects. The elements are accessed only from the top of the stack. You can retrieve, insert, or remove an element from the top of the stack. java.util.Stack<E> +Stack() +empty(): boolean +peek(): E +pop(): E +push(o: E) : E +search(o: Object) : int java.util.Vector<E> Creates an empty stack. Returns true if this stack is empty. Returns the top element in this stack. Returns and removes the top element in this stack. Adds a new element to the top of this stack. Returns the position of the specified element in this stack.
  • 22. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 22 Queues and Priority Queues A queue is a first-in/first-out data structure. Elements are appended to the end of the queue and are removed from the beginning of the queue. In a priority queue, elements are assigned priorities. When accessing elements, the element with the highest priority is removed first.
  • 23. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 23 The Queue Interface
  • 24. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 24 Using LinkedList for Queue
  • 25. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 25 The PriorityQueue Class Run PriorityQueueDemo
  • 26. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 26 Case Study: Evaluating Expressions Stacks can be used to evaluate expressions. Evaluate Expression
  • 27. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 27 Algorithm Phase 1: Scanning the expression The program scans the expression from left to right to extract operands, operators, and the parentheses. 1.1. If the extracted item is an operand, push it to operandStack. 1.2. If the extracted item is a + or - operator, process all the operators at the top of operatorStack and push the extracted operator to operatorStack. 1.3. If the extracted item is a * or / operator, process the * or / operators at the top of operatorStack and push the extracted operator to operatorStack. 1.4. If the extracted item is a ( symbol, push it to operatorStack. 1.5. If the extracted item is a ) symbol, repeatedly process the operators from the top of operatorStack until seeing the ( symbol on the stack. Phase 2: Clearing the stack Repeatedly process the operators from the top of operatorStack until operatorStack is empty.
  • 28. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015 28 Example