Transcript 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.
• LIFO (Last In First Out)
• Add & remove dilakukan dari atas (top)
Gambaran
top
top
bottom
F
E
E
D
D
C
C
B
B
A
Top
: elemen paling atas
Bottom : elemen paling bawah
bottom
A
Operasi Stack
Operasi
• Pop (operasi pengambilan elemen)
• Push (operasi penambahan elemen)
• Peek (operasi pengaksesan elemen)
Operasi POP
• Operasi pop pada stack dilakukan pada
elemen paling atas
Operasi Push
• Operasi push pada stack dilakukan pada
elemen yang paling atas
Operasi peek
• Pengambilan/pengaksesan elemen paling atas
(top) pada stack.
Proses Operasi Stack
• Contoh lain adalah ada sekumpulan perintah stack yaitu
push(5), push(7), pop, push(3), pop. Jika dijalankan, maka
yang akan terjadi adalah :
-1
-1
Stack dengan Array
?
?
-1
0
1
0
1
2
1
Stack dengan Linked List
Contoh Penerapan Stack
•
•
•
•
Konversi Desimal ke Biner
Notasi Polish
Menara Hanoi
Rat In a Maze
Implementasi Stack
(Notasi Polish)
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
1. Push tanda “(“ ke stack dan tambahkan tanda “)” di sentinel di Q.
2. Scan Q dari kiri ke kanan, kemudian ulangi langkah c s.d f untuk setiap
elemen Q sampai stack Q kosong.
3. Jika yang discan adalah operand, maka tambahkan ke P
4. Jika yang discan adalah “(“ maka push ke stack
5. Jika yang discan adalah “)” maka pop isi stack sampai ditemukan tanda “(“,
kemudian tambahkan ke P sedangkan tanda “(“ tidak disertakanke P.
6. Jika yang discan adalah operator, maka :
- Jika elemen paling atas dari stack adalah operator yang mempunyai tingkatan
sama atau lebih tinggi dari operator yang discan, maka pop operator tsb dan
tambahkan ke P.
- Push operator tersebut ke stack.
7. 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
Penyelesaian
• Hasil akhir :
Dari proses di atas didapatkan notasi postfix Q
= 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
1. Tambahkan tanda “)” pada sentinel di P
2. Scan P dari kiri ke kanan, ulangi langkah c dan d untuk setiap
elemen P sampai ditemukan sentinel.
3. Jika yang discan adalah operand, maka push ke stack.
4. Jika yang discan adalah operator (sebut opr1), maka




Pop 1 buah elemen teratas dari stack, simpan dalam variable var1.
Pop 1 buah elemen teratas dari stack, simpan dalam variable var2.
Hitung variable (var2 opr1 var1), simpan hasil di variable hitung.
Push variable hitung ke stack.
5. Pop isi stack dan simpan di variable value.
6. 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
Operator Priority
^
/, *
Pangkat, akar
+, -
Penambahan,
pengurangan
Pembagian,
perkalian
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, +, /, +
Class ArrayStack
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;
// 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];
top = -1;
}
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 = ChangeArrayLength.changeLength1D(stack, 2 * stack.length);
stack[++top] = theElement;
}
Method pop()
public Object pop()
{
if (empty())
throw new EmptyStackException();
Object topElement = stack[top];
stack[top--] = null; // enable garbage collection
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 LinkedStack
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());
}
}
}
Pustaka
• Sartaj Sahni, Presentation L5 & L10
• Jokonowo, Bambang S.Si, “Pemrograman Berorientasi Obyek”,
Pusat pengembangan bahan ajar UMB, 2006.
• Noviyanto, “Pemrograman Berorientasi Obyek (PBO) – Array”,
2005
• Nugroho, Adi, “Algoritma dan Struktur Data Dalam Bahasa
Java”, ANDI Yogyakarta, 2008.
• Michaell Waite, ”Data Structures and Algorithms in Java”,
SAMS, 2001