Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan...

27
Struktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo 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

Transcript of Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan...

Page 1: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

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

Page 2: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

OutlineStack

Intro Operasi Penggunaan

Page 3: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

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

Page 4: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,
Page 5: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

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

Page 6: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

STACK LIFO, data baru diletakkan di atas data

yang terakhir

Data 1

Data 2

Data 3

Data 4

Page 7: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Stack Data masuk/keluar dari 1 pintu Penyajian

Dgn Array (jml elemen statis) Dgn Linked List

Page 8: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

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.

Page 9: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

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.

Page 10: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Push

Page 11: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Pop

Page 12: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

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

Page 13: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Class in Java Class memiliki

Atribut / variabel Methode

Fungsi Prosedur / void

Page 14: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Stack – contoh penyajian

Page 15: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Operasi Push

Page 16: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Operasi Pop

Page 17: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Beberapa kegunaan stack Mengecek ekspresi matematis Notasi Polish Mencatat address dalam operasi

memory

Page 18: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Mencek ekspresi numerik.34 * 10

Operand

Operator

Page 19: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Operator PrecedenceTingkat presedensi operator, dari rendah

ke tinggi Or Xor And + - * / %

Page 20: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Operator Precedence 1 + 5 * 3 = ?? (1 + 5) * 3 = ??

Page 21: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

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

Page 22: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Ekspresi Numerik Infix/penulisan standar

contoh: ( 3 + 5 ) * 7 – (34 / 54))4 – 5

Notasi polish Prefix

- 4 5 Postfix/suffix

4 5 -

Operand

Operator

Page 23: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

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

Page 24: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Konversi? Infix : (6+7)* ((6-23)/10)+4 Postfix? Prefix? Infix: 8+6-3/89 Postfix? Prefix?

Page 25: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

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

Page 26: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

Latihan Notasi Polish1. 5 + 6 * 22. 8 – ( 8 * 2 )3. ( 8 + 3 ) + [ ( 2 + 3 + 2) – 2 ]4. ( 4 – 3 / 2 * 2 ) + 2

Page 27: Struktur Data Stack - Belajar dan Berguru.. · PDF fileStruktur Data Stack M. Kautsar Sophan T. Informatika Unijoyo Ref: Algorithms and Data Structures in C++, Allan Parker, CRC Press,

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