Data Structures and
Algorithms
Week 3: Stack and Queues
Ferdin Joe John Joseph, PhD
Faculty of Information Technology
Thai-Nichi Institute of Technology, Bangkok
Week 3
• Stack
• Queues
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
2
Developing an ADT
• ADT stack operations
• Create an empty stack
• Determine whether a stack is empty
• Add a new item to the stack
• Remove from the stack the item that was added most
recently
• Remove all the items from the stack
• Retrieve from the stack the item that was added most
recently
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
3
Developing an ADT During the
Design of a Solution
• A stack
• Last-in, first-out (LIFO)
property
• The last item placed on
the stack will be the
first item removed
• Analogy
• A stack of dishes in a
cafeteria
Stack of cafeteria dishes
Lecture series for Data Structures and
Algorithms, Data Science and Analytics, Thai-
Nichi Institute of Technology
7A-4
Refining the Definition of the ADT
Stack
• Pseudocode for the ADT stack operations
createStack()
// Creates an empty stack.
isEmpty()
// Determines whether a stack is empty.
push(newItem) throws StackException
// Adds newItem to the top of the stack.
// Throws StackException if the insertion is
// not successful.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
5
Refining the Definition of the ADT
Stack
• Pseudocode for the ADT stack operations (Cont)
pop() throws StackException
// Retrieves and then removes the top of the stack.
// Throws StackException if the deletion is not
// successful.
popAll()
// Removes all items from the stack.
peek() throws StackException
// Retrieves the top of the stack. Throws
// StackException if the retrieval is not successful
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
6
Simple Applications of the ADT Stack:
Checking for Balanced Braces
• A stack can be used to verify whether a program
contains balanced braces
• An example of balanced braces
abc{defg{ijk}{l{mn}}op}qr
• An example of unbalanced braces
abc{def}}{ghij{kl}m
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
7
Checking for Balanced Braces
• Requirements for balanced braces
• Each time you encounter a "{", push it on the stack
• Each time you encounter a “}”, it matches an already
encountered “{”, pop "{" off the stack
• When you reach the end of the string, you should have
matched each “{” and the stack should be empty
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
8
Checking for Balanced Braces
Traces of the algorithm that checks for balanced braces
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
9
Implementations of the ADT Stack
• The ADT stack can be implemented using
• An array
• A linked list
• The ADT list in the JCF
• StackInterface
• Provides a common specification for the three
implementations
• StackException
• Used by StackInterface
• Extends java.lang.RuntimeException
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
10
Implementations of the ADT Stack
Implementation of the
ADT stack that use a)
an array; b) a linked list;
c) an ADT list
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
11
An Array-Based Implementation of
the ADT Stack
• StackArrayBased class
• Implements StackInterface
• Instances
• Stacks
• Private data fields
• An array of Objects called items
• The index top
An array-based implementation
Lecture series for Data Structures and
Algorithms, Data Science and Analytics, Thai-
Nichi Institute of Technology
12
A Reference-Based
Implementation of the ADT Stack
A reference-based
implementation
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
13
Comparing Implementations
• All of the three implementations are ultimately
array based or reference based
• Fixed size versus dynamic size
• An array-based implementation
• Uses fixed-sized arrays
• Prevents the push operation from adding an item to the stack if
the stack’s size limit has been reached
• A reference-based implementation
• Does not put a limit on the size of the stack
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
14
Evaluating Postfix Expressions
• A postfix calculator
• Requires you to enter postfix expressions
• Example: 2 3 4 + * (= 2*(3+4))
• When an operand is entered, the calculator
• Pushes it onto a stack
• When an operator is entered, the calculator
• Applies it to the top two operands of the stack
• Pops the operands from the stack
• Pushes the result of the operation on the stack
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
15
Evaluating Postfix Expressions
The action of a postfix calculator when evaluating the expression 2 * (3 + 4)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
16
Evaluating Postfix Expressions
• To evaluate a postfix expression which is entered as
a string of characters
• Simplifying assumptions
• The string is a syntactically correct postfix expression
• No unary operators are present
• No exponentiation operators are present
• Operands are single lowercase letters that represent integer
values
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
17
Converting Infix Expressions to
Equivalent Postfix Expressions
• An infix expression can be evaluated by first being converted
into an equivalent postfix expression
• Facts about converting from infix to postfix
• Operands always stay in the same order with respect to one
another
• An operator will move only “to the right” with respect to the
operands
• All parentheses are removed
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
18
Converting Infix Expressions to
Equivalent Postfix Expressions
A trace of the algorithm that converts the infix expression a - (b + c * d)/e to postfix form
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
19
Java Implementation
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
20
Week 3
• Stack
• Queues
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
21
The Abstract Data Type Queue
• A queue
• New items enter at the back, or rear, of the queue
• Items leave from the front of the queue
• First-in, first-out (FIFO) property
• The first item inserted into a queue is the first item to leave
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
22
The Abstract Data Type Queue
• ADT queue operations
• Create an empty queue
• Determine whether a queue is empty
• Add a new item to the queue
• Remove from the queue the item that was added
earliest
• Remove all the items from the queue
• Retrieve from the queue the item that was added
earliest
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
23
The Abstract Data Type Queue
• Queues
• Are appropriate for many real-world situations
• Example: A line to buy a movie ticket
• Have applications in computer science
• Example: A request to print a document
• A simulation
• Discrete event simulator
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
24
The Abstract Data Type Queue
• Pseudocode for the ADT queue operations
createQueue()
// Creates an empty queue.
isEmpty()
// Determines whether a queue is empty
enqueue(newItem) throws QueueException
// Adds newItem at the back of a queue. Throws
// QueueException if the operation is not
// successful
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
25
The Abstract Data Type Queue
• Pseudocode for the ADT queue operations (Cont)
dequeue() throws QueueException
// Retrieves and removes the front of a queue.
// Throws QueueException if the operation is
// not successful.
dequeueAll()
// Removes all items from a queue
peek() throws QueueException
// Retrieves the front of a queue. Throws
// QueueException if the retrieval is not
// successful
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
26
The Abstract Data Type Queue
Some queue operations
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
27
Simple Applications of the ADT
Queue: Reading a String of
Characters
• A queue can retain characters in the order in
which they are typed
queue.createQueue()
while (not end of line) {
Read a new character ch
queue.enqueue(ch)
}
• Once the characters are in a queue, the system can
process them as necessary
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
28
Recognizing Palindromes
• A palindrome
• A string of characters that reads the same from left to
right as its does from right to left
• To recognize a palindrome, a queue can be used in
conjunction with a stack
• A stack can be used to reverse the order of occurrences
• A queue can be used to preserve the order of
occurrences
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
29
Recognizing Palindromes
• A nonrecursive recognition
algorithm for palindromes
• As you traverse the
character string from left to
right, insert each character
into both a queue and a
stack
• Compare the characters at
the front of the queue and
the top of the stack
The results of inserting a string
into both a queue and a stack
Lecture series for Data Structures and
Algorithms, Data Science and Analytics, Thai-
Nichi Institute of Technology
7A-30
Implementations of the ADT
Queue
• A queue can have either
• An array-based implementation
• A reference-based implementation
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
31
A Reference-Based Implementation
• Possible implementations of a queue
• A linear linked list with two external references
• A reference to the front
• A reference to the back
A reference-based implementation of a queue: a) a linear linked list with two
external references
Lecture series for Data Structures and
Algorithms, Data Science and Analytics, Thai-
Nichi Institute of Technology
7A-32
A Reference-Based Implementation
• Possible implementations of a queue (Continued)
• A circular linked list with one external reference
• A reference to the back
A reference-based implementation of a queue: b) a circular linear linked list with one
external reference
Lecture series for Data Structures and
Algorithms, Data Science and Analytics, Thai-
Nichi Institute of Technology
7A-33
A Reference-Based
Implementation
Inserting an item into a nonempty queue
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
34
A Reference-Based
Implementation
Inserting an item into an empty queue: a) before insertion; b) after insertion
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
35
A Reference-Based
Implementation
Deleting an item from a queue of more than one item
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
36
An Implementation That Uses the
ADT List
• If the item in position 1 of a list list represents
the front of the queue, the following
implementations can be used
• dequeue()
list.remove(1)
• peek()
list.get(1)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
37
An Implementation That Uses the
ADT List
• If the item at the end of the list represents the
back of the queue, the following implementations
can be used
• enqueue(newItem)
list.add(list.size()+1, newItem)
An implementation that uses the ADT list
Lecture series for Data Structures and
Algorithms, Data Science and Analytics, Thai-
Nichi Institute of Technology
7A-38
Application: Simulation
A blank line at at time a) 0; b) 12
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
39
Application: Simulation
A blank line at at time c) 20; d) 38
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
40
Java Implementation
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
41
The ADT Priority Queue
• Organize data by priorities
• Example: weekly “to do” list
• Priority value
• We will say high value  high priority
• Operations
• Test for empty
• Add to queue in sorted position
• Remove/get entry with highest priority
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
42
Real-life Priority Queue
• Example : triage in a hospital emergency room
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
43
Real-life Priority Queue
• Example : Public Transport in Thailand
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
44
Priority Queue
A priority queue is an ADT with the property that
only the highest-priority element can be accessed
at any time.
Queue
Enque an item
Item returned has been in the queue the
longest amount of time.
Priority Queue
Enque a pair <item, priority>
Item returned has the highest priority.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
45
Tracking Your Assignments
• Pseudocode to organize assignments,
responsibilities
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
46
Application: Simulation
• Simulation models behavior of systems
• Problem to solve
• Approximate average time bank customer must wait for
service from a teller
• Decrease in customer wait time with each new teller added
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
47
Application: Simulation
• Sample arrival and transaction times
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
48
Application: Simulation
• A bank line at time (a) 0; (b) 20; (c) 22; (d) 26
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
49
Application: Simulation
• A bank line at time (a) 0; (b) 20; (c) 22; (d) 26
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
50
Application: Simulation
• Pseudocode for an event loop
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
51
Application: Simulation
• Time-driven simulation
• Simulates the ticking of a clock
• Event-driven simulation considers
• Only the times of certain events,
• In this case, arrival-s and departures
• Event list contains
• All future arrival and departure events
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
52
Application: Simulation
• A typical instance of (a) an arrival event;
(b) a departure event
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
53
Application: Simulation
• Two tasks required to process each event
• Update the bank line: Add or remove customers
• Update the event queue: Add or remove events
• New customer
• Always enters bank line
• Served while at the front of the line
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
54
Application: Simulation
• A trace of the bank simulation algorithm for the data
(20, 6), (22, 4), (23, 2), (30, 3)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
55
Java Implementation
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
56
Java Implementation
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
57
Activity - Graded
• Create a priority queue code using Java for
implementing Visa on Arrival passengers. Priority
should be of order below
• Elderly 55+
• Pregnant Women
• Differently abled
• Women with infants
Code should be submitted to canvas portal before
next class. Those who complete by today’s class will
receive a bonus point.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
58
Next Week
Tree ADT and its applications
Tree Traversals
Binary Trees
Implementation in Java
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
59

Data Structures and Algorithm - Week 3 - Stacks and Queues

  • 1.
    Data Structures and Algorithms Week3: Stack and Queues Ferdin Joe John Joseph, PhD Faculty of Information Technology Thai-Nichi Institute of Technology, Bangkok
  • 2.
    Week 3 • Stack •Queues Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 2
  • 3.
    Developing an ADT •ADT stack operations • Create an empty stack • Determine whether a stack is empty • Add a new item to the stack • Remove from the stack the item that was added most recently • Remove all the items from the stack • Retrieve from the stack the item that was added most recently Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 3
  • 4.
    Developing an ADTDuring the Design of a Solution • A stack • Last-in, first-out (LIFO) property • The last item placed on the stack will be the first item removed • Analogy • A stack of dishes in a cafeteria Stack of cafeteria dishes Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai- Nichi Institute of Technology 7A-4
  • 5.
    Refining the Definitionof the ADT Stack • Pseudocode for the ADT stack operations createStack() // Creates an empty stack. isEmpty() // Determines whether a stack is empty. push(newItem) throws StackException // Adds newItem to the top of the stack. // Throws StackException if the insertion is // not successful. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 5
  • 6.
    Refining the Definitionof the ADT Stack • Pseudocode for the ADT stack operations (Cont) pop() throws StackException // Retrieves and then removes the top of the stack. // Throws StackException if the deletion is not // successful. popAll() // Removes all items from the stack. peek() throws StackException // Retrieves the top of the stack. Throws // StackException if the retrieval is not successful Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 6
  • 7.
    Simple Applications ofthe ADT Stack: Checking for Balanced Braces • A stack can be used to verify whether a program contains balanced braces • An example of balanced braces abc{defg{ijk}{l{mn}}op}qr • An example of unbalanced braces abc{def}}{ghij{kl}m Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 7
  • 8.
    Checking for BalancedBraces • Requirements for balanced braces • Each time you encounter a "{", push it on the stack • Each time you encounter a “}”, it matches an already encountered “{”, pop "{" off the stack • When you reach the end of the string, you should have matched each “{” and the stack should be empty Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 8
  • 9.
    Checking for BalancedBraces Traces of the algorithm that checks for balanced braces Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 9
  • 10.
    Implementations of theADT Stack • The ADT stack can be implemented using • An array • A linked list • The ADT list in the JCF • StackInterface • Provides a common specification for the three implementations • StackException • Used by StackInterface • Extends java.lang.RuntimeException Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 10
  • 11.
    Implementations of theADT Stack Implementation of the ADT stack that use a) an array; b) a linked list; c) an ADT list Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 11
  • 12.
    An Array-Based Implementationof the ADT Stack • StackArrayBased class • Implements StackInterface • Instances • Stacks • Private data fields • An array of Objects called items • The index top An array-based implementation Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai- Nichi Institute of Technology 12
  • 13.
    A Reference-Based Implementation ofthe ADT Stack A reference-based implementation Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 13
  • 14.
    Comparing Implementations • Allof the three implementations are ultimately array based or reference based • Fixed size versus dynamic size • An array-based implementation • Uses fixed-sized arrays • Prevents the push operation from adding an item to the stack if the stack’s size limit has been reached • A reference-based implementation • Does not put a limit on the size of the stack Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 14
  • 15.
    Evaluating Postfix Expressions •A postfix calculator • Requires you to enter postfix expressions • Example: 2 3 4 + * (= 2*(3+4)) • When an operand is entered, the calculator • Pushes it onto a stack • When an operator is entered, the calculator • Applies it to the top two operands of the stack • Pops the operands from the stack • Pushes the result of the operation on the stack Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 15
  • 16.
    Evaluating Postfix Expressions Theaction of a postfix calculator when evaluating the expression 2 * (3 + 4) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 16
  • 17.
    Evaluating Postfix Expressions •To evaluate a postfix expression which is entered as a string of characters • Simplifying assumptions • The string is a syntactically correct postfix expression • No unary operators are present • No exponentiation operators are present • Operands are single lowercase letters that represent integer values Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 17
  • 18.
    Converting Infix Expressionsto Equivalent Postfix Expressions • An infix expression can be evaluated by first being converted into an equivalent postfix expression • Facts about converting from infix to postfix • Operands always stay in the same order with respect to one another • An operator will move only “to the right” with respect to the operands • All parentheses are removed Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 18
  • 19.
    Converting Infix Expressionsto Equivalent Postfix Expressions A trace of the algorithm that converts the infix expression a - (b + c * d)/e to postfix form Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 19
  • 20.
    Java Implementation Lecture seriesfor Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 20
  • 21.
    Week 3 • Stack •Queues Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 21
  • 22.
    The Abstract DataType Queue • A queue • New items enter at the back, or rear, of the queue • Items leave from the front of the queue • First-in, first-out (FIFO) property • The first item inserted into a queue is the first item to leave Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 22
  • 23.
    The Abstract DataType Queue • ADT queue operations • Create an empty queue • Determine whether a queue is empty • Add a new item to the queue • Remove from the queue the item that was added earliest • Remove all the items from the queue • Retrieve from the queue the item that was added earliest Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 23
  • 24.
    The Abstract DataType Queue • Queues • Are appropriate for many real-world situations • Example: A line to buy a movie ticket • Have applications in computer science • Example: A request to print a document • A simulation • Discrete event simulator Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 24
  • 25.
    The Abstract DataType Queue • Pseudocode for the ADT queue operations createQueue() // Creates an empty queue. isEmpty() // Determines whether a queue is empty enqueue(newItem) throws QueueException // Adds newItem at the back of a queue. Throws // QueueException if the operation is not // successful Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 25
  • 26.
    The Abstract DataType Queue • Pseudocode for the ADT queue operations (Cont) dequeue() throws QueueException // Retrieves and removes the front of a queue. // Throws QueueException if the operation is // not successful. dequeueAll() // Removes all items from a queue peek() throws QueueException // Retrieves the front of a queue. Throws // QueueException if the retrieval is not // successful Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 26
  • 27.
    The Abstract DataType Queue Some queue operations Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 27
  • 28.
    Simple Applications ofthe ADT Queue: Reading a String of Characters • A queue can retain characters in the order in which they are typed queue.createQueue() while (not end of line) { Read a new character ch queue.enqueue(ch) } • Once the characters are in a queue, the system can process them as necessary Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 28
  • 29.
    Recognizing Palindromes • Apalindrome • A string of characters that reads the same from left to right as its does from right to left • To recognize a palindrome, a queue can be used in conjunction with a stack • A stack can be used to reverse the order of occurrences • A queue can be used to preserve the order of occurrences Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 29
  • 30.
    Recognizing Palindromes • Anonrecursive recognition algorithm for palindromes • As you traverse the character string from left to right, insert each character into both a queue and a stack • Compare the characters at the front of the queue and the top of the stack The results of inserting a string into both a queue and a stack Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai- Nichi Institute of Technology 7A-30
  • 31.
    Implementations of theADT Queue • A queue can have either • An array-based implementation • A reference-based implementation Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 31
  • 32.
    A Reference-Based Implementation •Possible implementations of a queue • A linear linked list with two external references • A reference to the front • A reference to the back A reference-based implementation of a queue: a) a linear linked list with two external references Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai- Nichi Institute of Technology 7A-32
  • 33.
    A Reference-Based Implementation •Possible implementations of a queue (Continued) • A circular linked list with one external reference • A reference to the back A reference-based implementation of a queue: b) a circular linear linked list with one external reference Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai- Nichi Institute of Technology 7A-33
  • 34.
    A Reference-Based Implementation Inserting anitem into a nonempty queue Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 34
  • 35.
    A Reference-Based Implementation Inserting anitem into an empty queue: a) before insertion; b) after insertion Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 35
  • 36.
    A Reference-Based Implementation Deleting anitem from a queue of more than one item Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 36
  • 37.
    An Implementation ThatUses the ADT List • If the item in position 1 of a list list represents the front of the queue, the following implementations can be used • dequeue() list.remove(1) • peek() list.get(1) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 37
  • 38.
    An Implementation ThatUses the ADT List • If the item at the end of the list represents the back of the queue, the following implementations can be used • enqueue(newItem) list.add(list.size()+1, newItem) An implementation that uses the ADT list Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai- Nichi Institute of Technology 7A-38
  • 39.
    Application: Simulation A blankline at at time a) 0; b) 12 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 39
  • 40.
    Application: Simulation A blankline at at time c) 20; d) 38 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 40
  • 41.
    Java Implementation Lecture seriesfor Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 41
  • 42.
    The ADT PriorityQueue • Organize data by priorities • Example: weekly “to do” list • Priority value • We will say high value  high priority • Operations • Test for empty • Add to queue in sorted position • Remove/get entry with highest priority Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 42
  • 43.
    Real-life Priority Queue •Example : triage in a hospital emergency room Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 43
  • 44.
    Real-life Priority Queue •Example : Public Transport in Thailand Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 44
  • 45.
    Priority Queue A priorityqueue is an ADT with the property that only the highest-priority element can be accessed at any time. Queue Enque an item Item returned has been in the queue the longest amount of time. Priority Queue Enque a pair <item, priority> Item returned has the highest priority. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 45
  • 46.
    Tracking Your Assignments •Pseudocode to organize assignments, responsibilities Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 46
  • 47.
    Application: Simulation • Simulationmodels behavior of systems • Problem to solve • Approximate average time bank customer must wait for service from a teller • Decrease in customer wait time with each new teller added Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 47
  • 48.
    Application: Simulation • Samplearrival and transaction times Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 48
  • 49.
    Application: Simulation • Abank line at time (a) 0; (b) 20; (c) 22; (d) 26 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 49
  • 50.
    Application: Simulation • Abank line at time (a) 0; (b) 20; (c) 22; (d) 26 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 50
  • 51.
    Application: Simulation • Pseudocodefor an event loop Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 51
  • 52.
    Application: Simulation • Time-drivensimulation • Simulates the ticking of a clock • Event-driven simulation considers • Only the times of certain events, • In this case, arrival-s and departures • Event list contains • All future arrival and departure events Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 52
  • 53.
    Application: Simulation • Atypical instance of (a) an arrival event; (b) a departure event Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 53
  • 54.
    Application: Simulation • Twotasks required to process each event • Update the bank line: Add or remove customers • Update the event queue: Add or remove events • New customer • Always enters bank line • Served while at the front of the line Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 54
  • 55.
    Application: Simulation • Atrace of the bank simulation algorithm for the data (20, 6), (22, 4), (23, 2), (30, 3) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 55
  • 56.
    Java Implementation Lecture seriesfor Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 56
  • 57.
    Java Implementation Lecture seriesfor Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 57
  • 58.
    Activity - Graded •Create a priority queue code using Java for implementing Visa on Arrival passengers. Priority should be of order below • Elderly 55+ • Pregnant Women • Differently abled • Women with infants Code should be submitted to canvas portal before next class. Those who complete by today’s class will receive a bonus point. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 58
  • 59.
    Next Week Tree ADTand its applications Tree Traversals Binary Trees Implementation in Java Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 59