Stack dengan Method di Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package thestack;
import java.util.Scanner;
/**
*
* @author unej
*/
public class thisIsStack {
static Scanner input=new Scanner(System.in);
static int top=-1;
static Object[] dataStack=new Object[6];

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

push(36);
push(“test”);
push(“coba”);
push(“test lagi”);
peek();
push(“lho he ditest mane”);
push(“capek ah. . “);
push(“aduh pengen test lagi”);
push(false);

System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
int test =dataStack.length-1;
System.out.println(“panjang : “+test);
System.out.println(“Data diPEEK : “+peek());

}
///////////////////////////////////////////////////////////
public static void push(Object data){
if(top < dataStack.length-1)
dataStack[++top]=data;
else
System.out.println(“Data Sudah Penuh”);
}
///////////////////////////////////////////////////////////

public static Object peek(){
if(top>-1)
return dataStack[top];
else
return “Item Sudah Habis”;
}

///////////////////////////////////////////////////////////

public static Object pop(){
Object sementara;
if(top>-1)
sementara=dataStack[top–];
else
sementara=”Data Habis”;

return sementara;

}
///////////////////////////////////////////////////////////

public static void clear(){
top=-1;
}
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google

You are commenting using your Google account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s