1 / 89

Linear Data Structures (Stack)

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.

jenis
Télécharger la présentation

Linear Data Structures (Stack)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Sub Topik • Stack • Operasi Stack • Implementasi stack • Latihan

  3. STACK(Tumpukan)

  4. Definisi • Urutan elemen yang mengikuti konsep LIFO. • LIFO (Last In First Out) • Add & remove dilakukan dari atas (top)

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

  6. Operasi Stack

  7. 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.

  8. 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 : 1 1 0 0 0 0 0 -1 -1

  9. Latihan • Push(A), push(B), push(c), pop, pop, pop, push(D), push(E), pop, push(F), pop, pop. • Push(13), pop, push(14), push(15), pop, pop, push(16), push(17), pop, push(18), pop, push(19), pop, pop, push(20).

  10. Contoh Penerapan Stack • Konversi Desimal ke Biner • Notasi Polish

  11. Class ArrayStack

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

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

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

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

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

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

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

  19. 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()); } } }

  20. Class LinkedStack

  21. 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;} }

  22. Inisialisasi Awal public class LinkedStack implements Stack { protected ChainNode topNode;

  23. Method isEmpty() public boolean empty() { return topNode == null; }

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

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

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

  27. 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()); } } }

  28. Implementasi Stack(Notasi Polish)

  29. Notasi Polish (1) • Menggubah notasi infix menjadi notasi postfix. • Contoh : A+B (infix) AB+ (postfix)

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

  31. Algoritma • Push tanda “(“ ke stack dantambahkantanda “)” di sentinel di Q. • Scan Q darikirikekanan, kemudianulangilangkah c s.d f untuksetiapelemen Q sampai stack Q kosong. • Jika yang discanadalah operand, makatambahkanke P • Jika yang discanadalah “(“ maka push ke stack • Jika yang discanadalah “)” maka pop isi stack sampaiditemukantanda “(“, kemudiantambahkanke P sedangkantanda “(“ tidakdisertakanke P. • Jika yang discanadalah operator, maka : - Jikaelemen paling atasdari stack adalah operator yang mempunyai tingkatansamaataulebihtinggidari operator yang discan, maka pop operator tsb dantambahkanke P. - Push operator tersebutke stack. • Keluar

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

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

  34. Penyelesaian

  35. Penyelesaian • Hasil akhir : Dari proses di atas didapatkan notasi postfixQ = ABC*DEF^/G*-H*+

  36. Notasi Polish (2) • Menghitung ekspresi matematika yang disusun dalam notasi postfix. • Contoh : 2,5,* (postfix) Hasil : 10

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

  38. Algoritma • Tambahkantanda “)” pada sentinel di P • Scan P darikirikekanan, ulangilangkah c dan d untuksetiapelemen P sampaiditemukan sentinel. • Jika yang discanadalah operand, maka push ke stack. • Jika yang discanadalah operator (sebut opr1), maka • Pop 1 buahelementeratasdari stack, simpandalam variable var1. • Pop 1 buahelementeratasdari stack, simpandalam variable var2. • Hitung variable (var2 opr1 var1), simpanhasildi variable hitung. • Push variable hitungke stack. • Pop isi stack dansimpandi variable value. • Keluar.

  39. Contoh Kasus • P = 5, 2, 6, +, *, 12, 4, /, -

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

  41. Penyelesaian Hasil : Didapatkan Bilangan 37

  42. Operator Priority

  43. 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)

  44. 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, +, /, +

  45. QUEUE(Antrian)

  46. 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.

  47. Gambaran Proses Front : Depan Rear : Belakang

  48. Operasi Queue

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

More Related