Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Linear Data Structures (Queue)

Presentasi serupa


Presentasi berjudul: "Linear Data Structures (Queue)"— Transcript presentasi:

1 Linear Data Structures (Queue)
Oleh : Nur Hayatin, S.ST Teknik Informatika - Universitas Muhammadiyah Malang (UMM) Tahun Akademik

2 Sub Topik Queue Operasi Queue Implementasi Queue Latihan

3 QUEUE (Antrian)

4 Definisi Urutan elemen yang mengikuti konsep FIFO.
FIFO(First In First Out) Front  menunjuk pada elemen yang paling atas. Rear  menunjuk pada elemen yang paling belakang.

5 Gambaran Proses Front : Depan Rear : Belakang

6 Queue (Antrian) Front Rear

7 Queue (Antrian) Front Rear

8 Queue (Antrian) Front Rear

9 Queue (Antrian) Front Rear

10 Queue (Antrian) Front Rear

11 Queue (Antrian) Front Rear

12 Operasi Queue

13 Operasi Enqueue/put (operasi penambahan elemen) Dequeue/remove
(operasi penghapusan elemen) Get front (operasi pengaksesan elemen terdepan) Get rear (operasi pengaksesan elemen paling belakang)

14 Operasi Enqueue (add) Operasi penambahan elemen pada antrian.
Dilakukan pada rear. Increment rear.

15 Operasi Dequeue (remove)
Operasi penghapusan elemen pada antrian. Untuk Queue array terjadi pergeseran elemen dari belakang ke depan. Dilakukan pada Front. Decrement rear.

16 Operasi Get Front Pengambilan/pengaksesan elemen yang paling depan.
Elemen yang ditunjuk oleh front.

17 Operasi Get Rear Pengambilan/pengaksesan elemen yang paling belakang.
Elemen yang ditunjuk oleh rear.

18 Contoh Penerapan Queue
mailbox dalam komunikasi antar proses simulasi dan modeling (misalnya simulasi sistem pengendali lalu lintas udara) dalam memprediksi performansi Waiting Line pada Sistem Operasi

19 QUEUE DENGAN CIRCULAR ARRAY

20 Circular Array Mampu melakukan proses penghapusan elemen tanda melakukan pergeseran.

21 Aturan Circular Array Proses penghapusan dilakukan dengan cara nilai depan (front) ditambah 1 : depan=depan + 1. Proses penambahan elemen sama dengan queue linear array yaitu nilai belakang ditambah 1 : belakang=belakang + 1. Jika depan = maks dan ada elemen yang akan dihapus, maka nilai depan = 1. Jika belakang = maks dan depan tidak 1 maka jika ada elemen yang akan ditambahkan, nilai belakang=1. Jika hanya tinggal 1 elemen di queue (depan = belakang), dan akan dihapus maka depan diisi 0 dan belakang diisi dengan 0 (queue kosong).

22 Circular Array

23 Proses Circular Array

24 Proses Circular Array

25 Queue dalam Program

26 The Interface Queue public interface Queue { public boolean isEmpty();
public Object getFrontEelement(); public Object getRearEelement(); public void put(Object theObject); public Object remove(); }

27 Class ArrayQueue

28 Inisialisasi Awal public class ArrayQueue implements Queue {
int front=0; int rear=-1; Object [] queue;

29 Constructor public ArrayQueue(int initialCapacity) {
if (initialCapacity < 1) throw new IllegalArgumentException ("initialCapacity must be >= 1"); queue = new Object [initialCapacity + 1]; } public ArrayQueue() this(10);

30 Method empty() public boolean isEmpty() { return rear == -1; }

31 Method getFront() public Object getFrontElement() { if (isEmpty())
return null; else return queue[front]; }

32 Method getRear() public Object getRearElement() { if (isEmpty())
return null; else return queue[rear]; }

33 Method put() public void put(Object theElement) {
if (rear == queue.length - 1) Object [] newArray = new Object [2*queue.length];   System.arraycopy(queue, 0, newArray, 0, queue.length);   queue = newArray; }   queue[++rear] = theElement;

34 Method remove() public Object remove() { if (isEmpty()) return null;
Object frontElement = queue[front]; for(int i = 1;i<=rear; i++) queue[i-1] = queue[i]; --rear; return frontElement; }

35 Method main() public static void main(String [] args) { int x;
ArrayQueue q = new ArrayQueue(3); q.put(new Integer(1)); q.put(new Integer(2)); q.put(new Integer(3)); q.put(new Integer(4)); q.remove(); q.remove(); q.put(new Integer(5)); q.put(new Integer(6)); q.put(new Integer(7)); q.put(new Integer(8)); q.put(new Integer(9)); q.put(new Integer(10)); q.put(new Integer(11)); q.put(new Integer(12)); while (!q.isEmpty()) System.out.println("Rear element is " + q.getRearElement()); System.out.println("Front element is " + q.getFrontElement()); System.out.println("Removed the element " + q.remove()); }

36 Class LinkedQueue

37 Class ChainNode class ChainNode { // package visible data members
Object element; ChainNode next; // package visible constructors ChainNode() {} ChainNode(Object element) {this.element = element;} ChainNode(Object element, ChainNode next) {this.element = element; this.next = next;} }

38 Inisialisasi Awal public class LinkedQueue implements Queue {
protected ChainNode front; protected ChainNode rear;

39 Method isEmpty() public boolean isEmpty() { return front == null; }

40 Method getFront() public Object getFrontElement() { if (isEmpty())
return null; else return front.element; }

41 Method getRear() public Object getRearElement() { if (isEmpty())
return null; else return rear.element; }

42 Method put() public void put(Object theElement) {
ChainNode p = new ChainNode(theElement, null); if (front == null) front = p; // empty queue else rear.next = p; // nonempty queue rear = p; }

43 Method remove() public Object remove() { if (isEmpty()) return null;
Object frontElement = front.element; front = front.next; rear = null; // enable garbage collection return frontElement; }

44 Method main() public static void main(String [] args) { int x;
LinkedQueue q = new LinkedQueue(3); q.put(new Integer(1)); q.put(new Integer(2)); q.put(new Integer(3)); q.put(new Integer(4)); while (!q.isEmpty()) System.out.println("Rear element is " + q.getRearElement()); System.out.println("Front element is " + q.getFrontElement()); System.out.println("Removed the element " + q.remove()); }

45 Class CircularQueue

46 Inisialisasi Awal class CircularQueue { private int maxSize;
private int[] queArray; private int front; private int rear; private int nItems;

47 Constructor public CircularQueue(int s) { maxSize = s;
queArray = new int[maxSize]; front = 0; rear = -1; nItems = 0; }

48 Method isEmpty() public boolean isEmpty() { return (nItems==0); }

49 Method peekFront() public int peekFront() { return queArray[front]; }

50 Method insert() public void insert(int j) { if(rear == maxSize-1)
queArray[++rear] = j; nItems++; }

51 Method remove() public int remove() { int temp = queArray[front++];
if(front == maxSize) front = 0; nItems--; return temp; }

52 Method main() public static void main(String[] args) {
CircularQueue theQueue = new CircularQueue(5); // queue holds 5 items System.out.println("front : " + theQueue.front + "rear : " + theQueue.rear); theQueue.insert(10); theQueue.insert(20); theQueue.insert(30); theQueue.insert(40); theQueue.remove(); System.out.println(“remove 1x, front : " +theQueue.front +" rear : " +theQueue.rear); theQueue.insert(50); theQueue.insert(60); System.out.println("add 2 elemen, front : " +theQueue.front+"rear:”+theQueue.rear); System.out.println(“remove 1x, front : " +theQueue.front+ “rear : " +theQueue.rear); while( !theQueue.isEmpty() ) int n = theQueue.remove(); System.out.print(n); System.out.print(" "); }

53 Pustaka Sartaj Sahni, Presentation L5 & L10
Jokonowo, Bambang S.Si, “Pemrograman Berorientasi Obyek”, Pusat pengembangan bahan ajar UMB, 2006. Noviyanto, “Pemrograman Berorientasi Obyek (PBO) – Array”, 2005 Nugroho, Adi, “Algoritma dan Struktur Data Dalam Bahasa Java”, ANDI Yogyakarta, 2008. Michaell Waite, ”Data Structures and Algorithms in Java”, SAMS, 2001


Download ppt "Linear Data Structures (Queue)"

Presentasi serupa


Iklan oleh Google