Nama : Novia Citra Fadhlilla
NPM : 22082010216
source code :
(appQueue)
package queue;
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;
System.out.println("=============================================");
System.out.println("Nama : Novia Citra Fadhlilla");
System.out.println("NPM : 22082010216");
System.out.println("=============================================");
do{
System.out.println("\n=============================================");
System.out.println(" MENU ANTRIAN ");
System.out.println("=============================================");
System.out.println("1. Tambah Antrian");
System.out.println("2. Proses Antrian");
System.out.println("3. Lihat Antrian");
System.out.println("4. Selesai");
System.out.print("Pilih : ");
pilih=sc.nextInt();
switch(pilih){
case 1:
System.out.println("\n=============================================");
System.out.println(" << Tambah Antrian >> ");
System.out.println("=============================================");
System.out.print("Nama Barang : ");
String brg=sc.next();
cNode baru=new cNode(brg);
antrian.enqueue(baru);
break;
case 2:
cNode t=antrian.dequeue();
boolean cek=false;
if(t==null){
cek=true;
}
if(cek==true){
System.out.println("Antrian Kosong!");
}
else{
System.out.println(t.getBarang()+" keluar");
}
break;
case 3:
antrian.viewQueue();
break;
case 4:
System.out.println("=============================================");
System.out.println(" TERIMA KASIH ");
System.out.println("=============================================");
break;
}
}while(pilih!=4);
}
}
(cNode)
package queue;
public class cNode {
String barang;
cNode next;
cNode(String b){
barang=b;
}
public String getBarang(){
return barang;
}
}
(cQueue)
package queue;
public class cQueue {
cNode front,rear;
int jumlah;
cQueue(){
front=rear=null;
jumlah=0;
System.out.println("Object Antrian dibuat...");
}
public void enqueue(cNode baru){
if(rear==null){
rear=front=baru;
}
else{
rear.next=baru;
rear=baru;
}
System.out.println("Nomor Antrian ke-"+(jumlah+1));
jumlah++;
System.out.println("Tambah Antrian Sukses...");
}
public cNode dequeue(){
cNode temp=front;
if(temp==null){
System.out.println("Antrian Kosong!");
}
else if(front.next==null){
cNode t=front;
front=rear=null;
temp.next=null;
System.out.println("Proses Antrian Sukses...");
jumlah--;
return(t);
}
else{ //antrian berisi >1 simpul
cNode t=front.next;
cNode t2=front;
t2.next=null;
front=t;
front.next=null;
System.out.println("Proses Antrian Sukses...");
jumlah--;
return(t2);
}
return temp;
}
public void viewQueue(){
int count=1;
System.out.println("Panjang Antrian : "+jumlah);
System.out.println("Daftar Antrian :");
for(cNode t=front;t!=null;t=t.next){
System.out.println(count+"."+t.getBarang()+" ");
count++;
}
if(jumlah==0){
System.out.println("Antrian Kosong!");
}
}
}
No comments:
Post a Comment