Linear Data Structures Stack Oleh Nur Hayatin S








































![Method main() public static void main(String [] args) { int x; Array. Stack s Method main() public static void main(String [] args) { int x; Array. Stack s](https://slidetodoc.com/presentation_image_h/dbf39456c0b8f38400d936cc19b81788/image-41.jpg)







![Method main() public static void main(String [] args) { Linked. Stack s = new Method main() public static void main(String [] args) { Linked. Stack s = new](https://slidetodoc.com/presentation_image_h/dbf39456c0b8f38400d936cc19b81788/image-49.jpg)

- Slides: 50
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)
Gambaran top bottom F E E D D C C B B A Top : elemen paling atas Bottom : elemen paling bawah bottom A
Operasi Stack
Operasi • Pop (operasi pengambilan elemen) • Push (operasi penambahan elemen) • Peek (operasi pengaksesan elemen)
Operasi POP • Operasi pop pada stack dilakukan pada elemen paling atas
Operasi Push • Operasi push pada stack dilakukan pada elemen yang paling atas
Operasi peek • Pengambilan/pengaksesan elemen paling atas (top) pada stack.
Proses Operasi Stack • Contoh lain adalah ada sekumpulan perintah stack yaitu push(5), push(7), pop, push(3), pop. Jika dijalankan, maka yang akan terjadi adalah : -1 -1
Stack dengan Array ? ? -1 0 1 2 1
Stack dengan Linked List
Contoh Penerapan Stack • • Konversi Desimal ke Biner Notasi Polish Menara Hanoi Rat In a Maze
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 opr 1), maka § § Pop 1 buah elemen teratas dari stack, simpan dalam variable var 1. Pop 1 buah elemen teratas dari stack, simpan dalam variable var 2. Hitung variable (var 2 opr 1 var 1), 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 +, - Penambahan, pengurangan Pembagian, perkalian
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, +, /, +
Class Array. Stack
The Interface Stack public interface Stack { public boolean empty(); public Object peek(); public void push(Object the. Object); public Object pop(); }
Inisialisasi Awal public class Array. Stack implements Stack { int top; // current top of stack Object [] stack; // element array
Constructor public Array. Stack(int initial. Capacity) { if (initial. Capacity < 1) throw new Illegal. Argument. Exception("initial. Capacity must be >= 1"); stack = new Object [initial. Capacity]; top = -1; } public Array. Stack() {this(10); }
Method empty() public boolean empty() { return top == -1; }
Method peek() public Object peek() { if (empty()) throw new Empty. Stack. Exception(); return stack[top]; }
Method push() public void push(Object the. Element) { if (top == stack. length - 1) stack = Change. Array. Length. change. Length 1 D(stack, 2 * stack. length); stack[++top] = the. Element; }
Method pop() public Object pop() { if (empty()) throw new Empty. Stack. Exception(); Object top. Element = stack[top]; stack[top--] = null; // enable garbage collection return top. Element; }
Method main() public static void main(String [] args) { int x; Array. Stack s = new Array. Stack(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 Linked. Stack
Class Chain. Node class Chain. Node { // package visible data members Object element; Chain. Node next; // package visible constructors Chain. Node() {} Chain. Node(Object element) {this. element = element; } Chain. Node(Object element, Chain. Node next) {this. element = element; this. next = next; } }
Inisialisasi Awal public class Linked. Stack implements Stack { protected Chain. Node top. Node;
Method is. Empty() public boolean empty() { return top. Node == null; }
Method peek() public Object peek() { if (empty()) throw new Empty. Stack. Exception(); return top. Node. element; }
Method push() public void push(Object the. Element) { top. Node = new Chain. Node(the. Element, top. Node); }
Method pop() public Object pop() { if (empty()) throw new Empty. Stack. Exception(); Object top. Element = top. Node. element; top. Node = top. Node. next; return top. Element; }
Method main() public static void main(String [] args) { Linked. Stack s = new Linked. Stack(); 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()); } } }
Pustaka • Sartaj Sahni, Presentation L 5 & L 10 • 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