Presentation is loading. Please wait.

S T A C K Catatan Kuliah: Algoritma & Struktur Data Ari Cahyono, S.Si., M.T.

Presentation on theme: "S T A C K Catatan Kuliah: Algoritma & Struktur Data Ari Cahyono, S.Si., M.T."— Presentation transcript:

S T A C K Catatan Kuliah: Algoritma & Struktur Data Ari Cahyono, S.Si., M.T

Stack = Tumpukan In = PUSH Aturan: LIFO (Last In First Out)

Stack = Tumpukan Out = POP Aturan: LIFO (Last In First Out)

Stack = Tumpukan Aturan: LIFO (Last In First Out) IN OUT TOP MaxStack = 8 0 1 2 3 4 5 6 7

Deklarasi const MaxStack =..........; type TData =..........; { bebas } TStack = record Entry: array[0..MaxStack-1] of TData; Top: -1..MaxStack-1; { Integer} end; 0 1 2 3 4 5 6 7 4 Top Entry TStack TData

Operasi-operasi pada Stack Initialize Stack Mengosongkan Stack Push Data into Stack Memasukkan data ke dalam Stack Pop Data from Stack Mengambil data dari Stack Copy Data from Stack Menyalin isi salah satu data yang ada pada Stack tanpa mengurangi anggota Stack.

Initialize Stack Mengosongkan Stack procedure InitStack(var S: TStack); begin S.Top := -1 end; 0 1 2 3 4 5 6 7 Top Entry TStack Variable Parameter

PUSH procedure Push(var S: TStack; X: TData); begin if not (S.Top = MaxStack-1) then begin Inc(S.Top); {S.Top := S.Top +1} S.Entry[S.Top] := X; end; 0 1 2 3 4 5 6 7 2 Top Entry TStack 2+1 = 3 7 = MaxStack -1 Catatan: Jk S.Top = MaxStack – 1  Stack Penuh Not (S.Top = MaxStack-1) ~ S.Top < MaxStack-1 X

POP procedure Pop(var S: TStack; var X: TData); begin if not (S.Top = -1) then begin X := S.Entry[S.Top]; Dec(S.Top); {S.Top := S.Top – 1} end; 0 1 2 3 4 5 6 7 Entry TStack 3-1 = 2 Catatan: Jk S.Top = -1  Stack Kosong Not (S.Top = -1) ~ S.Top > -1 3 Top Variabel X digunakan untuk menerima salinan data dari puncak Stack X

Copy Stack Data procedure CopyStack(var S: TStack; N: Integer; var X: TData); begin if N in [0..S.Top] then X := S.Entry[N]; end; 0 1 2 3 4 5 6 7 4 Top Entry TStack N in [0..S.Top] setara dengan N >= 0 and N <= S.Top X Misal: N = 3

Terima Kasih

Download ppt "S T A C K Catatan Kuliah: Algoritma & Struktur Data Ari Cahyono, S.Si., M.T."

Similar presentations

Ads by Google