struktur data stack - belajar dan berguru.. · pdf filestruktur data stack m. kautsar sophan...
TRANSCRIPT
Struktur Data
Stack
M. Kautsar SophanT. Informatika Unijoyo
Ref:Algorithms and Data Structures in C++, Allan Parker, CRC Press, 1993 chap 3.3Beginning Algorithms, Simon Haris, James Ross, Wiley Publishing, 2006 chap 5
OutlineStack
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 1
Data 2
Data 3
Data 4
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 willdecrease 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 PrecedenceTingkat 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 operandpop (
Jk stack operator kosong, dan stack operand = 1, stack_kurung kosong, ekspresi benar
Ekspresi Numerik Infix/penulisan standar
contoh: ( 3 + 5 ) * 7 – (34 / 54))4 – 5
Notasi polish Prefix
- 4 5 Postfix/suffix
4 5 -
Operand
Operator
Notasi Polish Infix, dengan susunan operator diantara 2
operandeg: 1*2, (1*2)+3 = 1*2+3
Postfix, dengan susunan operator setelah operand utk menghitung expresi matematiseg: 12*, 12*3+
Prefix, dengan susunan operator sebelum operandeg: *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 Polish1. 5 + 6 * 22. 8 – ( 8 * 2 )3. ( 8 + 3 ) + [ ( 2 + 3 + 2) – 2 ]4. ( 4 – 3 / 2 * 2 ) + 2
Tugas1. 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