- STACK berarti tumpukan
- Konsep STACK digunakan dalam struktur data.
Dalam Struktur Data Stack digunakan istilah :
1. PUSH : Simpan,Masuk,Insert,Tulis
2. POP :
Ambil,Keluar,Delete,Baca
STACK ada 2 jenis :
1. Single Stack
2. Double Stack
1. Single
Stack
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
|
Posisi
TOP
|
KOSONG
|
Top = -1
|
PENUH
|
Top = n-1
|
BISA DIISI
|
Top < n-1
|
ADA ISINYA
|
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
PUSH Stack sampai penuh kemudian POP isi Stack sampai kosong
1.Buat program untuk menyiapkan Stack S berbentuk array
satu dimensi dengan jumlah elemen 5, bertipe integer.
2.Input data dan PUSH ke Stack S. Proses input akan
selesai setelah Stack penuh.
POP semua isi Stack kemudian cetak ke layar.
Contoh Program Single Stack :
#include<stdio.h>#include<stdlib.h>
#include<conio>
typedef int ItemType;
typedef struct simpul node;
struct simpul
{
ItemType item;
node *next;
};
struct Stack
{
node *TOS;
};
node *baru;
void allocate_node(ItemType x)
{
baru = (node *) malloc (sizeof(node));
if(baru==NULL)
{
printf(“Alokasi Gagal\n”);
exit(1);
}
else
{
baru->item=x;
baru->next=NULL;
}
}
void inisialisasi(Stack *s)
{
s->TOS = NULL;
}
int kosong(Stack *s)
{
return s->TOS==NULL;
}
void push(Stack *s)
{
baru->next = s->TOS;
s->TOS = baru;
}
ItemType pop(Stack *s)
{
node *temp;
if(kosong(s))
{
printf(“Data Kosong\n”);
return ‘ ‘;
}
else
{
temp = s->TOS;
s->TOS = s->TOS->next;
return temp->item;
free(temp);
temp=NULL;
}
}
void tampil(Stack *s)
{
Stack bantu;
bantu = *s;
printf(“\nData Simpul ==> “);
while(bantu.TOS!=NULL)
{
printf(“%d “, bantu.TOS->item
bantu.TOS = bantu.TOS->next
}
printf(“\n\n”);
}
void main()
{
int pilih, data;
char lagi=’y';
Stack ujung;
inisialisasi(&ujung);
while(lagi==’y’)
{
system(“CLS”);
//tampil(&ujung);
printf(“Menu Pilihan : \n”);
printf(“1. Push\n”);
printf(“2. Pop\n”);
printf(“3. Tampilkan Stack\n”);
printf(“\nPilih No : “);
scanf(“%d”, &pilih);
switch(pilih)
{
case 1:
printf(“Masukkan data : “);
scanf(“%d”, &data);
allocate_node(data);
push(&ujung);
break;
case 2:
pop(&ujung);
break;
case 3:
tampil(&ujung);
break;
}
fflush(stdin);
printf(“Lagi (y/t) ? “);
scanf(“%c”, &lagi);
clrscr();
}
}
2. Double Stack
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
|
Posisi
TOP
|
Stack1 KOSONG
|
Top1 = -1
|
Stack2 KOSONG
|
Top2 = n
|
Stack PENUH
(baik Stack1 maupun Stack2 tidak BISA DIISI)
|
Top2 – Top1 =
1
|
Stack BISA
DIISI (baik Stack1 maupun Stack2 BISA DIISI)
|
Top2 – Top1
> 1
|
Stack1 ADA
ISINYA
|
Top1 > -1
|
Stack2 ADA
ISINYA
|
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
PUSH1 (mengisi Stack1)
l Periksa apakah Stack1 BISA DIISI
if (Top2 – Top1 > 1)
{ Top1 = Top1 + 1;
S[Top1]
= x;
}
else
cout<<“Stack Penuh”;
Algoritma
POP1
(mengambil isi Stack1)
(mengambil isi Stack1)
l 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)
(mengambil isi Stack2)
l Periksa apakah Stack2 ADA ISINYA
if (Top2 < n)
{ x = S[Top2];
Top2 = Top2 + 1;
}
else
cout<<“Stack Kosong”;
Contoh program double stack :
#include “stdio.h”
#include “conio.h”
#define MAX 10
#define true 1
#define false 0
char stack[MAX];
int top1, top2;
void init(void);
void push(char data, int nomorstack);
char pop(int nomorstack);
void clear(int nomorstack);
int full(void);
int empty(int nomorstack);
void baca();
main(){
char data;
int pilih, nomorstack;
init();
do{
clrscr();
printf(“Contoh program double stack”);
printf(“\n1. Push”);
printf(“\n2. Pop”);
printf(“\n3. Clear”);
printf(“\n4. Baca”);
printf(“\n5. Selesai…”);
printf(“\nPilihan anda : \n”);
scanf(“%i”,&pilih);
switch(pilih){
case 1: printf(“Push\n”);
printf(“Masukkan datanya :\n”); scanf(“%s”,&data);
printf(“Mau dimasukkan ke stack berapa ? 1 atau 2 ? :\n”);
scanf(“%i”,&nomorstack);
push(data, nomorstack);
break;
case 2: printf(“Pop\n”);
printf(“Masukkan nomor stack\n”);
scanf(“%i”,&nomorstack);
data=pop(nomorstack);
printf(“\nData yang dikeluarkan adalah %s”, data);
break;
case 3: printf(“Clear\n”);
printf(“Nomor Stack yang akan dikosongkan \n”);
scanf(“%i”,&nomorstack);
clear(nomorstack);
break;
case 4: printf(“Baca\n”);
baca();
break;
case 5: printf(“Exit”);
break;
default: printf(“Pilihan yang anda masukkan tidak ada”);
break;
}
}while(pilih!=5);
getch();
}
//init
void init(){
top1=0;
top2=MAX+1;
}
//PUSH
void push(char data, int nomorstack){
if(full()!=true){
switch(nomorstack){
case 1: top1++;
stack[top1]=data;
break;
case 2: top2–;
stack[top2]=data;
break;
default: printf(“\nNomor stack salah”);
break;
}
}
else
printf(“\nStack penuh”);
getch();
}
//POP
char pop(int nomorstack){
char data;
if(empty(nomorstack)!=true){
switch(nomorstack){
case 1: data=stack[top1];
top1–;
return data;
break;
case 2: data=stack[top2];
top2++;
return data;
break;
default: printf(“\nNomor stack salah”);
break;
}
}
else printf(“\nStack masih kosong”);
getch();
return 0;
}
//cek full
int full(void){
if(top1+1>=top2){
return true;
}
else return false;
}
//cek empty
int empty(int nomorstack){
switch(nomorstack){
case 1: if(top1==0) return true;
else return false;
break;
case 2: if(top2==MAX+1) return true;
else return false;
break;
default: printf(“nomor stack salah”);
break;
}
}
//clearing
void clear(int nomorstack){
switch(nomorstack){
case 1: top1=0;
break;
case 2: top2=MAX+1;
break;
default: printf(“Nomor stack salah”);
break;
}
}
void baca(){
int i;
printf(“Baca isi stack pertama \n”);
for(i=1; i<=top1; i++){
printf(” %c “,stack[i]);
printf(“\n”);
}
printf(“Isi stack kedua\n”);
for(i=MAX; i>=top2; i–){
printf(” %c “,stack[i]);
printf(“\n”);
}
getch();
}
Referensi :
Anonim. 2010. Stack/ Tumpukan. Diakses tanggal 5 Juni 2015 pada
http://sisfo.binadarma.ac.id/upload/materi/8868_SD4-STACK%20%28Tumpukan%29.pdf
Anonim. 2010. Materi Kuliah Struktur Data. Diakses tanggal 5 Juni 2015 pada
http://materikuliahstrukturdata.blogspot.com/2012/02/stack.html
Tidak ada komentar:
Posting Komentar