queue - Sulthan Atha Muhammad (21082010126)
package pkgQueue;
public class cNode {
String barang;
cNode next;
cNode(String b){
barang = b;
}
public String getBarang(){
return barang;
}
}
Class cQueue
package pkgQueue;
public class cQueue {
cNode front,rear;
int jumlah;
cQueue(){
front=rear=null;
jumlah=0;
}
public void enqueue(cNode baru){
if(rear==null){
rear=front=baru;
}
else{
rear.next=baru;
rear=baru;
}
System.out.println("Enqueue Sukses....");
}
public cNode dequeue(){
cNode temp=front;
if(temp==null){
System.out.println("Queue Kosong...");
}
else if(front.next==null){
front=rear=null;
temp.next=null;
}
else{ //antrian berisi > 1 simpul
front=front.next;
temp.next=null;
}
return temp;
}
public void viewQueue(){
System.out.println("Isi Antrian : ");
for(cNode t=front; t!=null; t=t.next){
System.out.print("["+t.getBarang()+"]");
}
System.out.println("");
}
}
Class appQueue
package pkgQueue;
import java.util.Scanner;
public class appQueue {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
cQueue antrian = new cQueue();
int pilih=0;
do {
System.out.println("Aplikasi Antrian");
System.out.println("1. Masuk");
System.out.println("2. Keluar");
System.out.println("3. Lihat");
System.out.println("4. Exit");
System.out.print("Pilih = ");
pilih = sc.nextInt();
switch(pilih){
case 1:
System.out.print("Nama Barang = ");
String brg = sc.next();
cNode baru = new cNode(brg);
antrian.enqueue(baru);
break;
case 2:
cNode keluar = antrian.dequeue();
if(keluar!=null)
System.out.println(keluar.getBarang()+" Keluar...");
break;
case 3:
antrian.viewQueue();
break;
case 4:
System.out.println("Terima Kasih");
break;
}
} while (pilih!=4);
}
}
Comments
Post a Comment