STACK

Stack (tumpukkan) adalah sekumpuan elemen-elemen yang di simpan dalam  satu linear . stack memiliki konsep utama yaitu LIFO (Last In First Out) jadi di dalam stack data pertama yang di masukkan data itu akan keluar belakangan, sedangkan data terakhir yang di masukkan data itu akan paling pertama di keluarkan dalam stack. Di dalam stack menggunakan istilah-istilah yaitu :
è PUSH : simpan,masuk,insert, tulis
è POP   : ambil,keluar,delete baca
Stack memiliki 2 jenis yaitu :
·         Single stack
·         Double stack

1. single stack

Single dapat di presentasikan menggunakan array satu dimensi. Kondisi stack di tentukan oleh posisi atau isi top.

Proses pada single satck yaitu :
·         Awal (inisialisasi)
·         PUSH (Insert,Masuk,Simpan,Tulis)
·         POP(Delete,Keluar,Ambil,Baca/Hapus)

Contoh potongan algoritma PUSH
If (top < n-1)
                { top = top + 1;
                    S[top] = x;
                }
Else
    Cout<<”stack Penuh”;


Contoh potongan algoritma POP :
If (top > -1)
                { x = S[top];
                    Top= top-1;
                }
Else
    Cout<<”stack kosong”;

Contoh program single stack pada c++ :
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#define size 5

struct stack
{

int a[size];
int top;
};

typedef struct stack STACK;

void push(STACK *p,int value) /* PUSH OPERATION */
{
if(p->top==size-1)
cout<<"STACK IS FULL ";
else
p->a[++p->top]=value;
}

int pop(STACK *p) /* POP OPERATION */
{
if (p->top==-1)
{
cout<<"STACK IS EMPTY";

return -1;
}
else
return p->a[p->top--];
}


void display (STACK *p) /*DISPLAY OPERATION */
{ int i;
if(p->top==-1)
cout<<"\n STACK IS EMPTY\n";
else
cout<<"\nSTACK is\n";
for (i=p->top;i>=0; --i)
cout<<p->a[i]<<"\n";
}


void main()
{

STACK s ;
int x,c,i;
s.top=-1;
do
{
cout<<"\n1: To PUSH\n";
cout<<"2: To POP\n";
cout<<"3: To Display\n";
cout<<"4: To Destroy\n";
cout<<"5: To Exit\n";
cout<<"\n\n Enter Choice\n";cin>>c;

switch(c)
{
case 1: cout<<"\nEnter Element: ";cin>>x;
push (&s,x);
break;
case 2: x=pop(&s);
if(x!=-1)
cout<<"\nElement deleted="<<x;
break;
case 3: display(&s);
break;
case 4: if(s.top==-1)
printf("\nSTACK IS EMPTY\n");
else
printf("\nSTACK is destroying\n"); /*

STACK DESTROY */
for (i=s.top;i>=0; --i)
printf("element destroyed are %d\n",pop(&s));
s.top=-1;
} printf("\nSTACK DESTROYED\n");
}while(c!=5);
}


2.     Double stack

Double stack disebut juga stack ganda prinsip proses dari double stack yaitu LIFO baik dalam single stack maupun untuk double stack.
Proses pada Double Stack :
·         AWAL (Inisialisasi)
·         PUSH 1 (Push untuk stack 1)
·         POP 1 (pop untukstack 1)
·         PUSH 2 (Push untuk stack 2)
·         POP 2(Pop untuk stack 2)

Kondisi Double stack


Potongan algoritma PUSH 1 :
Periksa apakah stack 1 dapat di isi

                   if (Top2 – Top1 > 1)                        
                   {        Top1 = Top1 + 1;
                              S[Top1] = x;
                    }
                else
                      cout<<“Stack Penuh”;

Potongan algoritma POP 1 :
Periksa apakah stack 1 ada isinya

                 if (Top1 > -1)                     
                        { x = S[Top1];
                            Top1 = Top1 - 1;
                         }
                else
                      cout<<“Stack Kosong”;

Potongan algoritma Push 2 :
Periksa apakah stack 2 dapat di isi

                   if (Top2 – Top1 > 1)                        
                          {      Top2 = Top2 - 1;
                                   S[Top2] = x;
                           }
                else
                         cout<<“Stack Penuh”;

Potongan algoritma POP 2 :
Periksa apakah stack dua ada isi nya

                  if (Top2 < n)                       
                        {     x = S[Top2];
                                Top2 = Top2 + 1;
                         }
                else
                        cout<<“Stack Kosong”;



Tidak ada komentar:

Posting Komentar