Contoh Program Stack Array Sederhana dalam Java


import java.io.*;
public class stack_array
{
private int maxsize; //penentu batas elemen stack maksimum
private double [] stackarray; //array untuk menyimpan stack
private int top; //indeks array
public void inisiasi(int s) //menentukan ukuran kapasitas stack
{
maxsize = s;
stackarray = new double [maxsize];
top = -1;
}
public void push(double data)
{
if (top>=maxsize-1)
System.out.println(“Stack Penuh. “+data+” Tidak Bisa Masuk”);
else
{
top++;
stackarray[top] = data;
System.out.println(data +” Masuk ke Stack”);
}
}
public double pop()
{
double temp;
if (top>=0)
{
temp = stackarray[top];
System.out.println(temp + ” Keluar dari Stack”);
top–;
return (temp);
}
else
{
System.out.println(“Stack Sudah Kosong”);
return(-1);
}
}
public void view()
{
System.out.print(“Isi Stack: “);
for(int i=0; i<=top; i++)
System.out.print(stackarray[i] + " ");
System.out.println();
}
public static void main(String[] args)
{
stack_array stack = new stack_array();
stack.inisiasi(3);
stack.push(3);
stack.push(4);
stack.push(2);
stack.view();
stack.push(5);
stack.push(1);
stack.pop();
stack.pop();
stack.view();
stack.pop();
stack.pop();
stack.pop();
stack.push(6);
stack.push(8);
stack.push(7);
stack.push(9);
stack.pop();
stack.view();
}
}

//Selamat mencoba ^_^

5 Responses to Contoh Program Stack Array Sederhana dalam Java

  1. Ica says:

    Terima Kasih
    ini sangat membantu saya
    saya masih bingung dengan array stack dan juga linked list stack

  2. kerapolinema says:

    makasih gan. artikelnya manfaat baget, udah nolong ane di ujian tadi….

  3. Mr.G says:

    hatur nuhun kang..!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: