03 Stacks Queues 6 Labs
03 Stacks Queues 6 Labs
03 Stacks Queues 6 Labs
The problem can be solve by trial and error approach(THỬ VÀ SAI-mò) using a Stack.
1- Stacks
2- Queues
3- Priority Queues
Exercises: 6 assignments in demonstrations.
n= 12 : 2
base=2
0 6:2
Convert a positive 0 3:2
1100
number to a binary 1 1:2
string. 1
1
1
0
0
The java.util.Stack
uses an array in it’s
implementation.
a = b + (c – d) * (e – f));
g[10] = h[i[9]] + j + k) * l; Mismatched
while (m < (n[8] + o]) { p = 7; r = 6; }
Push to the
stack an open
delimiter and
pop it from
the stack
when
appropriate
close
delemiter is
detected in
input string
StringTokenizer:
3
4
*
5
6
*
+
3
/
Result:
11111111111
10000010001
10101010101
E0101000101
10111110101
10101000101
10001010101
11111010101
101M1010101
10000010101
11111111111
Result path :
[(3,0, E), (3,1, 0), (2,1, 0), (1,1, 0), (1,2, 0), (1,3, 0), (1,4, 0), (1,5, 0), (2,5, 0), (3,5, 0), (3,6,
0), (3,7, 0), (4,7, 0), (5,7, 0), (5,6, 0), (5,5, 0), (6,5, 0), (7,5, 0), (8,5, 0), (9,5, 0), (9,4, 0),
(9,3, 0), (8,3, M)]
BUILD SUCCESSFUL (total time: 0 seconds)
row-1,
col
row+1,
col
11111111111
10000010001
10101010101
E0101000101
10111110101
10101000101
10001010101
11111010101
101M1010101
10000010101
11111111111
Stacks & Queues 30
Stack: Demo 5: The Maze Problem…
11111111111
10000010001
10101010101
E0101000101
10111110101
10101000101
10001010101
11111010101
101M1010101
10000010101
11111111111
A queue is a waiting line that grows by adding elements to its end and
shrinks by taking elements from its front
A queue is a structure in which both ends are used:
One for adding new elements
One for removing them
A queue is an FIFO structure: first in/first out
Where a queue should be used?
A queue should be used when processing order of data is the same as
creating order.
Examples:
Queue for customers who are waiting for paying money in a shopping
store.
Printing server: A program receives all printing requirements in a
network environment.
Stacks & Queues 33
Queues: How to Implement a queue?
If we do not want to
shift elements when
an element is pick out
the queue then a
circular mechanism Add remove
is used.
What configuration
will describe the queue
is full?
Read by yourself
Remember
-One side for adding an element to the queue(enqueue)
- Other side for removing an element (dequeue)
Multi-threading in Java:
Java supports multi-threading. The java.lang.Runnable
interface declares a method of a Java thread, public void
run(void).
The topmost class of Java thread is the java.lang.Thread.
This class implemented the Runnable interface.
2 ways to create threads in Java
(1) Create a class implementing the Runnable interface
(2) Create a sub-class of the Thread class should be defined in
which the run() method should be overridden appropriately.
The following demonstration depicts both of two ways above.