Professional Documents
Culture Documents
5.1 Stack
5.1 Stack
Stack
Ref:
Algorithms and Data Structures in C++, Allan
Parker, CRC Press, 1993 chap 3.3
Beginning Algorithms, Simon Haris, James Ross,
Wiley Publishing, 2006 chap 5 M. Kautsar Sophan
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 Operator
Operand
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,
Prefix Operator
-45
Postfix/suffix
45-
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