Anda di halaman 1dari 16

POLITEKNIK PIKSI GANESHA

BANDUNG
2011
PENGERTIAN STACK
Secara sederhana diartikan dengan :
 sebagai tumpukan dari benda
 sekumpulan data yang seolah-olah
diletakkan di atas data yang lain
 koleksi dari objek-objek homogen

Dengan konsep utamanya adalah LIFO


Last In First Out
PUS ILUSTRASI
H
POP
CTOS (TOP
A of Stack)
T S

TOS (TOP of Stack)

K BOS (Bottom of Stack)


OPERASI PADA STACK
2 operasi dasar yang bisa dilaksanakan
pada sebuah stack, yaitu:
 Operasi Push (menyisipkan data)

memasukkan data ke dalam stack


 Operasi Pop (menghapus data)

menghapus elemen yang terletak pada


posisi paling atas dari sebuah stack
OPERASI PADA STACK
 Operasi Push

void Push (NOD **T, char item)


{
NOD *n;
n=NodBaru (item);
n->next=*T;
*T=n;
}
OPERASI PADA STACK
 Operasi Pop

char Pop (NOD **T)


{
NOD *n; char item;
if (!StackKosong(*T)) {
P=*T;
*T=(*T)->next;
item=P->data;
free(P);
}
return item;
}
Operasi Lainnya
Misal dalam Pascal di deklarasikan :

Const
Max = 4;
Type
TipeData = byte;
Stack = array [1..Max] of TipeData
Var
Top : TipeData;
Maka,
 Buat Stack (Create)
membuat stack baru yang masih kosong
4
Procedure Create;
Begin 3
Top := 0;
End; 2

Konsepnya adalah bahwa Top menunjukkan tingginya


tumpukkan stack. Jika tinggi Top adalah 0, berarti tumpukan
kosong. Prosedur ini digunakan juga dalam penghapusan isi
stack (Clear).
Stack Kosong (empty)
mengecek apakah stack dalam keadaan
kosong atau tidak

Function Empty : Boolean;


Begin
Empty := False;
If Top := 0 then Empty := True;
End;
Stack Penuh (Full)
mengecek apakah stack dalam keadaan
penuh atau tidak

Function Full : Boolean;


Begin
Full := False;
If Top = Max then Full := True;
End;
Implementasi Stack Menggunakan Array (C)
/*Stack menggunakan array*/
#include <stdio.h>
#include <conio.h>
#define max 50
#define True 1
#define False 0
int top=0, stack[max];
void buatstack();
int stackkosong ();
int stackpenuh ();
void push (int IB);
void pop ();
void cetakstack ();
void main () void buatstack () void push (int IB)
{ { stack[top] = 0; } { if (stackpenuh() )
buatstack (); int stackkosong () printf (“stack overflow \n”);
else
push (10); { if (top==0)
{ top++;
push (76); return (True);
stack[top] = IB;
push (12); else stack[0] = top; } }
push (21); return (False); } void pop ()
cetakstack (); int stackpenuh () { int IP;
getche; { if (top==max) if (stackkosong() )
printf (“\n\n”); return (True); printf (“stack underflow \n”);
pop (); else else
pop (); return (False); } { IP = stack[top];
top --;
pop ();
stack[0] = top; } }
cetakstack ();
void cetakstack();
getche(); { int i = 1;
} while ( i <=top )
{printf (“%d\n”, stack [i]);
i++; } }
Catatan
Overflow adalah suatu keadaan di mana
kita melakukan operasi PUSH terhadap
stack dalam keadaan penuh. Underflow
adalah keadaan di mana kita melakukan
operasi POP terhadap stack kosong.
Pada pascal, Eon adalah elemen yang
akan dimasukkan ke dalam stack dan Eoff
adalah elemen yang akan dikeluarkan dari
dalam stack.
Implementasi Stack Menggunakan Record (C)
/*Stack menggunakan record*/
#include <stdio.h>
#include <alloc.h>
#include <conio.h>
#define max 50
#define True 1
#define False 0
Typedef struct typestack { int top;
int elemen [max];
};
void buatstack();
void cetakstack ();
void push (int IB);
void pop ();
int stack kosong ();
int stack penuh ();
void main () void buatstack () void push (int IB)
{ { stack.top = -1; } { if (stackpenuh() )
buatstack (); int stackkosong () printf (“stack overflow \n”);
else
push (10); { if (stack.top==-1)
{ stack.top++;
push (76); return (True);
stack.elemen [stack.top] =
push (12); else IB; } ;
push (1); return (False); } void pop ()
cetakstack (); int stackpenuh () { int IP;
printf (“\n\n”); {if (stack.top==max) if (stackkosong() )
pop (); return (True); printf (“stack underflow \n”);
else
pop (); else
{ IP = stack.elemen [stack.top];
pop (); return (False); }
stack.top--; } ; }
cetakstack ();
void cetakstack();
getche(); { int i = 0;
} while ( i <= stack.top )
{ printf (“%d\n”, stack.elemen[i]);
i++; } }

Anda mungkin juga menyukai