530 likes | 823 Views
Linear Data Structures (Queue). Oleh : Nur Hayatin, S.ST. Teknik Informatika - Universitas Muhammadiyah Malang (UMM) Tahun Akademik 2010-2011. Sub Topik. Queue Operasi Queue Implementasi Queue Latihan. QUEUE (Antrian). Definisi. Urutan elemen yang mengikuti konsep FIFO.
E N D
Linear Data Structures(Queue) Oleh : Nur Hayatin, S.ST Teknik Informatika - Universitas Muhammadiyah Malang (UMM) Tahun Akademik 2010-2011
Sub Topik • Queue • Operasi Queue • Implementasi Queue • Latihan
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.
Gambaran Proses Front : Depan Rear : Belakang
Queue (Antrian) Front Rear
Queue (Antrian) Front Rear
Queue (Antrian) Front Rear
Queue (Antrian) Front Rear
Queue (Antrian) Front Rear
Queue (Antrian) Front Rear
Operasi • Enqueue/put (operasi penambahan elemen) • Dequeue/remove (operasi penghapusan elemen) • Get front (operasi pengaksesan elemen terdepan) • Get rear (operasi pengaksesan elemen paling belakang)
Operasi Enqueue (add) • Operasi penambahan elemen pada antrian. • Dilakukan pada rear. • Increment rear.
Operasi Dequeue (remove) • Operasi penghapusan elemen pada antrian. • Untuk Queue array terjadi pergeseran elemen dari belakang ke depan. • Dilakukan pada Front. • Decrement rear.
Operasi Get Front • Pengambilan/pengaksesan elemen yang paling depan. • Elemen yang ditunjuk oleh front.
Operasi Get Rear • Pengambilan/pengaksesan elemen yang paling belakang. • Elemen yang ditunjuk oleh rear.
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
Circular Array • Mampu melakukan proses penghapusan elemen tanda melakukan pergeseran.
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).
The Interface Queue public interface Queue { public boolean isEmpty(); public Object getFrontEelement(); public Object getRearEelement(); public void put(Object theObject); public Object remove(); }
Inisialisasi Awal public class ArrayQueue implements Queue { int front=0; int rear=-1; Object [] queue;
Constructor public ArrayQueue(int initialCapacity) { if (initialCapacity < 1) throw new IllegalArgumentException ("initialCapacity must be >= 1"); queue = new Object [initialCapacity + 1]; } public ArrayQueue() { this(10); }
Method empty() public boolean isEmpty() { return rear == -1; }
Method getFront() public Object getFrontElement() { if (isEmpty()) return null; else return queue[front]; }
Method getRear() public Object getRearElement() { if (isEmpty()) return null; else return queue[rear]; }
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; }
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; }
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()); } } }
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;} }
Inisialisasi Awal public class LinkedQueue implements Queue { protected ChainNode front; protected ChainNode rear;
Method isEmpty() public boolean isEmpty() { return front == null; }
Method getFront() public Object getFrontElement() { if (isEmpty()) return null; else return front.element; }
Method getRear() public Object getRearElement() { if (isEmpty()) return null; else return rear.element; }
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; }
Method remove() public Object remove() { if (isEmpty()) return null; Object frontElement = front.element; front = front.next; if (isEmpty()) rear = null; // enable garbage collection return frontElement; }
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()); } } }
Inisialisasi Awal • class CircularQueue • { • private intmaxSize; • private int[] queArray; • private int front; • private int rear; • private intnItems;
Constructor • public CircularQueue(int s) • { • maxSize = s; • queArray = new int[maxSize]; • front = 0; • rear = -1; • nItems = 0; • }
Method isEmpty() • public booleanisEmpty() • { • return (nItems==0); • }
Method peekFront() public intpeekFront() { return queArray[front]; }
Method insert() • public void insert(int j) • { • if(rear == maxSize-1) • rear = -1; • queArray[++rear] = j; • nItems++; • }