Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Algoritma dan Struktur Datahttp://www.unhas.ac.id/amil/S1TIF/StrukturData/
L3. Queue
Amil Ahmad Ilham
1
Stacks (Review)
• A stack is a LIFO (LAST IN FIRST OUT) sequence.• Addition and removal takes place only at one end, called
the top.
• Examples:• Stacks of plates
• Trains
• Vending Machines
• Expression Evaluation
• Navigating a maze
• Map coloring
Queue
• A Queue is a FIFO (FIRST IN FIRST OUT) sequence.• Addition takes place only at the tail, and removal takes
place only at the head.
• Examples:• Line of cars at a light
• Line of people at a bank
• CPU access, disk access, printer access.
• Packets waiting at a router
• Simulation
Lifetime of a Queue
The Effects of Queue OperationsOperation State of the Queue
after the operationValue Returned
Comment
Initially, the queue is empty
enqueue(a) a The queue contains item a.
enqueue(b) a b a is the front of the queue and b is the rear
enqueue(c) a b c c is added to the rear.
isEmpty() a b c false The queue is not empty
size() a b c 3 The queue contains 3 items.
peek() a b c a Returns the front item of the queue without removing it
dequeue() b c a Remove the front item from the queue and return it. b is now the front item.
dequeue() c b Remove and return b
dequeue() c Remove and return c
isEmpty() true The queue is empty.
dequeue() exception Trying to dequeue an empty queue throws an exception
The Interface for the Queue Prototype (QueuePT)
Type Description
void enqueue(Object item)Adds an item to the rear of the queue. Throws an exception if the item is null or the queue is full.
Object dequeue()Returns the item at the front of this queue and removes from the queue. Throws an exception if the queue is empty.
Object peek()Returns the item at the front of this queue without removing it from the queue. Throws an exception if the queue is empty.
boolean isEmpty()Return true if this queue contains no items.
boolean isFull()Returns true if this queue is full.
int size()Returns the number of items in this queue.
Implementation of the Queue Prototype
• Queue Library implemention using array• Queue.java
• ArrayQueue.java
• QueueEmptyException.java
• QueueFullException.java
• QueueTest.java
Queue.java• adalah interface yang berisi daftar method yang digunakan untuk implementasi queue.
8
ArrayQueue.java• Implementasi interface
Queue menggunakanArray
9
ArrayQueue.java• Implementasi interface
Queue menggunakanArray
10
Tugas #1:Lengkapi kode pada setiapmethod
QueueEmptyException.java• adalah class yang dipanggil jika operasi peek atau dequeue dilaksanakan pada
saat antrian sudah kosong.
11
QueueFullException.java• adalah class yang dipanggil jika operasi enqueue dilaksanakan pada saat antrian
sudah penuh.
12
QueueTest.java
• Class yang berisimethod ‘main”
• Berisi object ArrayQueue
13
Output QueueTest.java
14
Tugas #2
Modifikasi class QueueTest.java untuk melakukan pengaturan antriansbb:
• jumlah orang yang mau antri: diinput dari keyboard
• maksimun antrian 5 orang
• Proses enqueue dan dequeue dilakuan secara bergantian sampai semua antriandilayani.
• Lihat output program di slide 16
• Hint: gunakan method size, enqueue, peek, dequeue, isEmpty yang ada di class ArrayQueue
15
Ouptut Tugas #2
16
SIKOLA
• Buat dalam 1 file zip:• File ArrayQueue.java yang sudah dilengkapi (Tugas #1)
• Print screen output QueueTest.java (untuk menguji ArrayQueue.java yang sudah dilengkapi)
• File java (Tugas #2)
• Print screen output (Tugas #2)
• Upload 1 file zip ke Sikola paling lambat Senin, 20 April 2020 Pukul13.30 Wita.
17