Transcript STACK
STACK
Yohana Nugraheni
Pengertian STACK
STACK berarti tumpukan.
Konsep STACK digunakan dalam struktur data.
IN OUT TOP D C B A
Berlaku prinsip LIFO (Last In First Out)
Pengertian STACK
Dalam Struktur Stack digunakan istilah :
PUSH
: Simpan,Masuk,Insert,Tulis
POP
: Ambil,Keluar,Delete,Baca STACK ada 2 jenis :
Single Stack Double Stack
Single Stack
Single Stack dapat direpresentasikan menggunakan array satu dimensi.
S[10] 0 1 2 3 4 25 12 17 15 33 5 6 7 8 9
4
TOP TOP
33
X
Prinsip dan Konsep Proses Single Stack
Prinsip proses Single Stack adalah :
LIFO (Last In First Out)
Proses pada Single Stack :
AWAL (Inisialisasi)
PUSH (Insert, Masuk, Simpan, Tulis)
POP (Delete, Keluar, Ambil, Baca/Hapus)
Kondisi Single Stack
Kondisi Stack ditentukan oleh posisi atau isi TOP.
Kondisi Stack
KOSONG PENUH BISA DIISI ADA ISINYA
Posisi TOP
Top = -1 Top = n-1 Top < n-1 Top > -1
Algoritma PUSH
if (Top < n-1) { Top = Top + 1; S[Top] = x; } else cout<<“Stack Penuh”;
Algoritma POP
if (Top > -1) { x = S[Top]; Top = Top - 1; } else cout<<“Stack Kosong”;
Contoh:
PUSH Stack sampai penuh kemudian POP isi Stack sampai kosong
Buat program untuk menyiapkan array satu dimensi yang akan digunakan untuk mengisi Stack S sebanyak 5 elemen, bertipe integer. Input data dan PUSH ke Stack S. Proses input akan selesai setelah Stack penuh atau data yang diinputkan = 999. POP isi Stack kemudian cetak ke layar.
Double Stack
Disebut juga
Stack Ganda
.
Stack 1
S[12] -1 0 1 2 25 12 17 3 4 5 6 7 8 50
Stack 2
9 44 10 11 6 23 12 TOP 1
2
TOP 1
8
TOP 2 X TOP 2
Prinsip dan Konsep Proses Double Stack
Prinsip proses :
LIFO (Last In First Out)
baik untuk Stack1 maupun untuk Stack2 Proses pada Double Stack :
AWAL (Inisialisasi)
PUSH1 (Push untuk Stack1)
POP1 (Pop untuk Stack1)
PUSH2 (Push untuk Stack2)
POP2 (Pop untuk Stack2)
Kondisi Double Stack
Kondisi Stack
Stack1 KOSONG Stack2 KOSONG Stack PENUH (baik Stack1 maupun Stack2 tidak BISA DIISI) Stack BISA DIISI (baik Stack1 maupun Stack2 BISA DIISI) Stack1 ADA ISINYA Stack2 ADA ISINYA
Posisi TOP
Top1 = -1 Top2 = n Top2 – Top1 = 1 Top2 – Top1 > 1 Top1 > -1 Top2 < n
Algoritma PUSH1 (mengisi Stack1)
Periksa apakah Stack1 BISA DIISI if (Top2 – Top1 > 1) { Top1 = Top1 + 1; S[Top1] = x; } else cout<<“Stack Penuh”;
Algoritma POP1 (mengambil isi Stack1)
Periksa apakah Stack1 ADA ISINYA if (Top1 > -1) { x = S[Top1]; Top1 = Top1 - 1; } else cout<<“Stack Kosong”;
Algoritma PUSH2 (mengisi Stack2)
Periksa apakah Stack2 BISA DIISI if (Top2 – Top1 > 1) { Top2 = Top2 - 1; S[Top2] = x; } else cout<<“Stack Penuh”;
Algoritma POP2 (mengambil isi Stack2)
Periksa apakah Stack2 ADA ISINYA if (Top2 < n) { x = S[Top2]; Top2 = Top2 + 1; } else cout<<“Stack Kosong”;