Q - Universitas Muhammadiyah Malang

Download Report

Transcript Q - Universitas Muhammadiyah Malang

Linear Data Structures

(Stack) Oleh : Nur Hayatin, S.ST

Teknik Informatika - Universitas Muhammadiyah Malang (UMM) Tahun Akademik 2010-2011

Sub Topik

• • • • Stack Operasi Stack Implementasi stack Latihan

STACK (Tumpukan)

Definisi

• • • Urutan elemen yang mengikuti konsep LIFO.

LIFO (Last In First Out) Add & remove dilakukan dari atas (top)

top bottom E D C B A

Gambaran

top bottom Top : elemen paling atas Bottom : elemen paling bawah F E D C B A

Operasi Stack

Operasi

• • • Pop (operasi pengambilan elemen) dilakukan pada elemen paling atas.

Push (operasi penambahan elemen) dilakukan pada elemen paling atas.

Top (operasi penunjuk data paling atas) untuk mengetahui stack dalam keadaan terisi atau kosong. Stack kosong jika top bernilai -1.

Contoh

• Ada sekumpulan perintah stack yaitu 1. push(5) 2. push(7) 3. pop 4. push(3) 5. pop Jika dijalankan, maka yang akan terjadi adalah :

0 1 0 0 1 0 -1 0 -1

Latihan

1. Push(A), push(B), push(c), pop, pop, pop, push(D), push(E), pop, push(F), pop, pop.

2. Push(13), pop, push(14), push(15), pop, pop, push(16), push(17), pop, push(18), pop, push(19), pop, pop, push(20).

Contoh Penerapan Stack

• • Konversi Desimal ke Biner Notasi Polish

Class ArrayStack

The Interface Stack

} public interface Stack { public boolean empty(); public Object peek(); public void push(Object theObject); public Object pop();

Inisialisasi Awal

{ public class ArrayStack implements Stack int top = -1; // current top of stack Object [] stack; // element array

Constructor

public ArrayStack(int initialCapacity) { if (initialCapacity < 1) throw new IllegalArgumentException("initialCapacity must be >= 1"); stack = new Object [initialCapacity]; } public ArrayStack() {this(10);}

Method empty()

public boolean empty() { return top == -1; }

Method peek

() public Object peek() { if (empty()) throw new EmptyStackException(); return stack[top]; }

Method push()

public void push(Object theElement) { if (top != stack.length - 1) stack[++top] = theElement; }

Method pop()

public Object pop() { if (empty()) throw new EmptyStackException(); Object topElement = stack[top]; stack[top] = null; // enable garbage collection top--; return topElement; }

Method main()

} public static void main(String [] args) { int x; ArrayStack s = new ArrayStack(3); // add a few elements s.push(new Integer(1)); s.push(new Integer(2)); s.push(new Integer(3)); s.push(new Integer(4)); } } { // delete all elements while (!s.empty()) System.out.println("Top element is " + s.peek()); System.out.println("Removed the element " + s.pop());

Class LinkedStack

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 LinkedStack implements Stack protected ChainNode topNode;

Method isEmpty()

public boolean empty() { return topNode == null; }

Method peek

() public Object peek() { if (empty()) throw new EmptyStackException(); return topNode.element; }

Method push()

public void push(Object theElement) { topNode = new ChainNode(theElement, topNode); }

Method pop()

public Object pop() { if (empty()) throw new EmptyStackException(); Object topElement = topNode.element; topNode = topNode.next; return topElement; }

Method main()

} public static void main(String [] args) { LinkedStack s = new LinkedStack(); s.push(new Integer(1)); { s.push(new Integer(2)); s.push(new Integer(3)); s.push(new Integer(4)); while (!s.empty()) System.out.println("Top element is " + s.peek()); System.out.println("Removed the element " + s.pop()); } }

Implementasi Stack (Notasi Polish)

Notasi Polish (1)

• • Menggubah notasi infix menjadi notasi postfix.

Contoh : A+B (infix) AB+ (postfix)

Algoritma

• Misal : Q = ekspresi matematika yang ditulis dalam notasi infix P = penampung ekspresi matematika dalam notasi postfix

Algoritma

1. Push tanda “(“ ke stack dan tambahkan tanda “)” di sentinel di Q. 2. Scan Q dari kiri ke kanan, kemudian ulangi langkah c s.d f untuk setiap elemen Q sampai stack Q kosong. 3. Jika yang discan adalah operand, maka tambahkan ke P 4. Jika yang discan adalah “(“ maka push ke stack 5. Jika yang discan adalah “)” maka pop isi stack sampai ditemukan tanda “(“, kemudian tambahkan ke P sedangkan tanda “(“ tidak disertakanke P. 6. Jika yang discan adalah operator, maka : - Jika elemen paling atas dari stack adalah operator yang mempunyai tingkatan sama atau lebih tinggi dari operator yang discan, maka pop operator tsb dan tambahkan ke P. - Push operator tersebut ke stack. 7. Keluar

Contoh

Q = A + ( B * C - ( D / E ^ F ) * G ) * H

Penyelesaian

Q = A + ( B * C - ( D / E ^ F ) * G ) * H >> setelah ditambahkan tanda “)” pada notasi sehingga terdapat 20 simbol sbb :

Penyelesaian

Penyelesaian

• Hasil akhir : Dari proses di atas didapatkan notasi postfix Q = ABC*DEF^/G*-H*+

Notasi Polish (2)

• • Menghitung ekspresi matematika yang disusun dalam notasi postfix.

Contoh : 2,5,* (postfix) Hasil : 10

Algoritma

• Misal : P adalah ekspresi matematika yang ditulis dalam notasi postfix. variable value sebagai penampung hasil akhir.

Algoritma

1. Tambahkan tanda “)” pada sentinel di P 2. Scan P dari kiri ke kanan, ulangi langkah c dan d untuk setiap elemen P sampai ditemukan sentinel. 3. Jika yang discan adalah operand, maka push ke stack. 4. Jika yang discan adalah operator (sebut opr1), maka  Pop 1 buah elemen teratas dari stack, simpan dalam variable var1.  Pop 1 buah elemen teratas dari stack, simpan dalam variable var2.  Hitung variable (var2 opr1 var1), simpan hasil di variable hitung.  Push variable hitung ke stack. 5. Pop isi stack dan simpan di variable value. 6. Keluar.

Contoh Kasus

• P = 5, 2, 6, +, *, 12, 4, /, -

Penyelesaian

• • P = 5, 2, 6, +, *, 12, 4, /, Tambahkan tanda “)”pada sentinel P sehingga P = 5, 2, 6, +, *, 12, 4, /, -, ) Didapatkan 10 simbol yaitu :

Penyelesaian

Hasil : Didapatkan Bilangan 37

Operator Priority

^

/, * +, Pangkat, akar Pembagian, perkalian Penambahan, pengurangan

Latihan

1. Ubah notasi infix berikut ke dalam bentuk notasi postfix : A+((B*C/D)-(E^F)) M*(N^O)/P-(Q+R) (R*S+T)^U/(V-W+X)

Latihan

2. Hitung ekspresi matematika berikut yang disusun dalam bentuk postfix : • • 2,2,3,+,*,3,2,-,* B,2,^, 4, –, a, *, c, *, 2, a, *, /, p, q, *, a, b, +, /, +

QUEUE (Antrian)

Definisi

• • • • Urutan element yang mengikuti konsep FIFO.

FIFO(First In First Out) Front  digunakan untuk menunjuk pada element yang paling atas. Nilai front selalu 0.

Rear  digunakan untuk menunjuk pada element yang paling belakang. Nilai awal rear adalah -1. Nilai rear akan berubah setiap kali ada operasi penambahan dan pengurangan element.

Gambaran Proses

Front Rear : Depan : Belakang

Operasi Queue

Operasi

• • • Enqueue/put (operasi penambahan elemen) Dequeue/remove (operasi penghapusan elemen) Get front (operasi pengaksesan elemen terdepan)

Operasi Enqueue (add)

• • • Operasi penambahan elemen pada antrian.

Dilakukan di ujung belakang pada antrian.

Terjadi penambahan nilai (increment rear).

Operasi Dequeue (remove)

• • • Operasi penghapusan elemen pada antrian.

Dilakukan di ujung depan pada antrian.

Terjadi pengurangan nilai (Decrement rear).

Operasi Get Front

• • Pengaksesan elemen yang paling depan.

Elemen yang ditunjuk oleh front.

Contoh Penerapan Queue

• • • Antrian printer Antrian tiket bioskop Antrian nasabah bank

Queue dalam Program

The Interface Queue

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

Class ArrayQueue

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) { 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 LinkedQueue

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());

QUEUE DENGAN CIRCULAR ARRAY

Circular Array

• Mampu melakukan proses penghapusan elemen tanda melakukan pergeseran.

Aturan Circular Array

1. Proses penghapusan dilakukan dengan cara nilai depan (front) ditambah 1 : depan=depan + 1.

2. Proses penambahan elemen sama dengan queue linear array yaitu nilai belakang ditambah 1 : belakang=belakang + 1.

3. Jika depan = maks dan ada elemen yang akan dihapus, maka nilai depan = 1.

4. Jika belakang = maks dan depan tidak 1 maka jika ada elemen yang akan ditambahkan, nilai belakang=1.

5. Jika hanya tinggal 1 elemen di queue (depan = belakang), dan akan dihapus maka depan diisi 0 dan belakang diisi dengan 0 (queue kosong).

Circular Array

Proses Circular Array

Proses Circular Array

Class CircularQueue

Inisialisasi Awal

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

Constructor

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

Method isEmpty()

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

Method peekFront

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

Method insert()

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

Method remove()

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

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); theQueue.remove(); 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(" ");

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