Queue (2) di Java

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

package javaapplication21;
import java.util.Scanner;
import java.util.Stack;

/**
*
* @author ANGGA
*/
public class Main {
static Scanner input=new Scanner(System.in);
static int rear=-1;
static int front=0;
static Object[] dataQueue;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.print(“Masukkan Panjang Queue : “);
dataQueue=new Object[input.nextInt()];

push(1);
push(2);
push(3);
push(4);
push(5);
push(6);
System.out.println(“rear : “+rear);
cetak();
System.out.println(“\npeekRear : “+peekRear());
System.out.println(“peekFront : “+peekFront());
System.out.println(“pop : “+pop());
System.out.println(“pop : “+pop());
System.out.println(“rear : “+rear);
cetak();

System.out.println(“\npeekRear : “+peekRear());
System.out.println(“peekFront : “+peekFront());
clear();
System.out.println(“\npeekRear : “+peekRear());
System.out.println(“peekFront : “+peekFront());
cetak();
System.out.println(front);
}

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

public static Object pop(){
Object sementara;
if(rear>=front){
sementara=dataQueue[front];
for(int i=front;i<rear;i++){
dataQueue[i]=dataQueue[i+1];
}
rear–;
}
else
sementara=”Data Habis”;

return sementara;
}

public static Object peekRear(){
if(front<=rear){
return dataQueue[rear];
}
else
return “item Sudah Habis”;
}

public static Object peekFront(){
if(front<=rear){
return dataQueue[front];
}
else
return “item Sudah Habis”;
}

public static void clear(){
rear=-1;
front=0;
}

public static void cetak(){
if(front<=rear){
for(int i=0;i<=rear;i++){
System.out.print(dataQueue[i]+” “);
}
}
else{
System.out.println(“Stack Kosong untuk diCetak”);
}
}

}

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