910 likes | 1.22k Views
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.
E N D
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
Definisi • Urutan elemen yang mengikuti konsep LIFO. • LIFO (Last In First Out) • Add & remove dilakukan dari atas (top)
top F top E E D D C C B B bottom bottom A A Gambaran Top : elemen paling atas Bottom : elemen paling bawah
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 : 1 1 0 0 0 0 0 -1 -1
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).
Contoh Penerapan Stack • Konversi Desimal ke Biner • Notasi Polish
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 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()); } } }
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 • 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
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 • Hasil akhir : Dari proses di atas didapatkan notasi postfixQ = 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 • 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.
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
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, +, /, +
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 : Depan Rear : Belakang
Operasi • Enqueue/put (operasi penambahan elemen) • Dequeue/remove (operasi penghapusan elemen) • Get front (operasi pengaksesan elemen terdepan)