Struktur Data
Stack Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press, 1993 chap 3.3 Beginning Algorithms, Simon Haris, James Ross, M. Kautsar Sophan Wiley Publishing, 2006 chap 5 T. Informatika Unijoyo
Outline Stack
Intro Operasi Penggunaan
real-world examples of stacks
Plates are usually stacked—you place the first one on the shelf and add to the top.
If you need a plate, you remove the top one first
A stack is like a list with access restricted to one end
Stack
Astack both inserts (pushes) and deletes (pops) from the top data structure used to store and retrieve data supports two operations push and pop.
The push operation places data on the stack and, the pop operation retrieves the data from the stack
STACK
LIFO, data baru diletakkan di atas data yang terakhir Data 4 Data 3 Data 2 Data 1
Stack
Data masuk/keluar dari 1 pintu Penyajian
Dgn Array (jml elemen statis) Dgn Linked List
Operasi Stack
Push, Adds a value to the top of the stack. The size of the stack will increase by one Pop, Deletes and returns the value at the top of the stack. The size of the stack will decrease by one. Throws EmptyStackException when there are no more elements on the stack. Size, Obtains the number of elements in the stack.
Peek, Returns but does not delete the value at the top of the stack. Throws EmptyStackException when there are no elements on the stack. isEmpty, Determines whether a stack is empty Clear, Deletes all elements from a stack. The size of the stack is reset to zero.
Push
Pop
Sekilas tentang Class in Java
In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created
Class in Java
Class memiliki
Atribut / variabel Methode
Fungsi Prosedur / void
Stack – contoh penyajian
Operasi
Push
Operasi Pop
Beberapa kegunaan stack
Mengecek ekspresi matematis Notasi Polish Mencatat address dalam operasi memory
Mencek ekspresi numerik. 34 * 10
Operand
Operator
Operator Precedence Tingkat presedensi operator, dari rendah ke tinggi Or Xor And + * / %
Operator Precedence
1 + 5 * 3 = ?? (1 + 5) * 3 = ??
Mencek ekspresi numerik. Looping sepanjang ekspresi, R = ekspresi[I] If R operand,
If top stack operand kosong, push R ke stack operand Else,
If top stackoperator kosong, ekspresi salah. Else, join pop stack operand + pop stack operator + R
Push ke stack operand
IF R operator push R ke stack operator. Jika (, push ke stack_kurung Jika ), join: pop operand + pop operator + pop operan, push to operand pop (
Jk stack operator kosong, dan stack operand = 1, stack_kurung kosong, ekspresi benar
Ekspresi Numerik Infix/penulisan standar contoh: ( 3 + 5 ) * 7 – (34 / 54)) Operand 4–5 Notasi polish
Prefix -45 Postfix/suffix 45-
Operator
Notasi Polish
Infix, dengan susunan operator diantara 2 operand eg: 1*2, (1*2)+3 = 1*2+3 Postfix, dengan susunan operator setelah operand utk menghitung expresi matematis eg: 12*, 12*3+ Prefix, dengan susunan operator sebelum operand eg: *12, +*123
Konversi?
Infix : (6+7)* ((6-23)/10)+4 Postfix? Prefix? Infix: 8+6-3/89 Postfix? Prefix?
Algoritma Infix Postfix ?? Looping sebanyak ekspresi. R=ekspresi[I] If R operand, langsung tulis If R kurung buka, push ke stack If R kurung tutup, pop until ‘(‘. Tulis semua hasil pop kecuali ‘(‘ If R operator,
If stack kosong atau R lebih tinggi dari top stack, push R ke stack Else pop top stack dan tulis. Ulangi perbandingan R
Tulis sisa stack
Latihan Notasi Polish 1. 2. 3. 4.
5+6*2 8–(8*2) ( 8 + 3 ) + [ ( 2 + 3 + 2) – 2 ] (4–3/2*2)+2
Tugas 1. Buat Program utk membalik kata. Contoh: Budi makan pagi pagi makan Budi 2. Buat implementasi Stack, dengan fungsi push, pop, size, peek, isEmpty, clear, PrintStack 3. Buat program utk konversi 1. Infixe ke prefix 2. Prefix ke infix 3. Postfix ke infix 4. Infix ke postfix