Queue di Java

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

package thequeue;

/**
*
* @author ANGGA
*/
public class thisIsQueue {

static Object[] Queue=new Object[3];
static int front=0;
static int rear=-1;
static int item=0;

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

// peekFront
public static Object peekFront(){
if(front==Queue.length){
front–;
Queue[front]=null;
}
return Queue[front];
}

//peekRear
public static Object peekRear(){
if(front==Queue.length){
Queue=new Object[3];
}
return Queue[rear];
}

// pop
public static Object pop(){
Object pop=0;
if(front<=Queue.length-1){
pop=Queue[front++];
item–;
}
else
System.out.println(“-Queue Sudah Kosong”);
return pop;
}

// cetak
public static void cetak(){
System.out.print(“[ “);
for(int i=front;i<=rear;i++){
System.out.print(Queue[i]+” “);
}
System.out.println(“]”);
}

// jumlahItem
public static int jumlahItem(){
return item;
}

// clear
public static void clear(){
front=0;
rear=-1;
item=0;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

// lakukan push
push(“Antrian 1”);
push(2);
push(“Ketiga”);
// cek jumlah item sekarang
System.out.println(“Jumlah Item : “+thisIsQueue.jumlahItem());
// hapus semua
clear();
// lakukan push
push(‘A’);
push(5);
push(666);
push(“Antrian Belakang”);
// cetak queue
cetak();
// lakukan peek
System.out.println(“Rear : “+peekRear());
System.out.println(“Front : “+peekFront());

// lakukan pop
System.out.println(“pop : “+pop());
System.out.println(“pop : “+pop());
System.out.println(“pop : “+pop());
pop();
// cek jumlah item
System.out.println(“Jumlah Item : “+jumlahItem());
System.out.println(peekRear());
System.out.println(peekFront());

}

}

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