Queue
Source Code Main
package queue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
byte pilih, no=1;
String npm;
cQueue queue = new cQueue();
do{
System.out.println("\nREGISTRASI MAHASISWA");
System.out.println("1. Buat Antrian (enqueue)");
System.out.println("2. Hapus Antrian (dequeue)");
System.out.println("3. Lihat Antrian (print)");
System.out.println("4. Keluar");
System.out.print("Pilih : ");
pilih = s.nextByte();
switch(pilih){
case 1 :
System.out.println(" ");
System.out.println("DATA MAHASISWA URUTAN "+no);
System.out.print("NPM : ");
npm = s.next();
cObject nim = new cObject(npm);
queue.enqueue(nim);
no++;
break;
case 2 :
queue.dequeue();
no--;
break;
case 3 :
queue.print();
break;
case 4 : System.out.println("Terima Kasih");
}
}while(pilih!=4);
}
}
Source Code cObject
package queue;
public class cObject {
private String npm;
cObject next, prev;
cObject(String n){
npm = n;
System.out.println("Object Data "+npm+" Berhasil Dibuat!");
}
public String getNPM(){
return npm;
}
}
Source Code cQueue
package queue;
public class cQueue {
cObject front, back, temp;
byte no;
cQueue(){
no=1;
front = back = null;
}
public void enqueue(cObject baru){
if(front == null) front=back=baru;
else{
back.next=baru;
baru.prev=back;
back=baru;
}
}
public cObject dequeue(){
if(front==null){
System.out.println("Antrian Kosong!");
return null;
}
else if(back.prev==null){
temp = front;
front=back=null;
System.out.println("Data "+temp.getNPM()+" Berhasil Dikeluarkan!");
return temp;
}
else{
temp = front;
//temp.prev=null;
//temp.next=null;
front=front.next;
System.out.println("Data "+temp.getNPM()+" Berhasil Dikeluarkan!");
return temp;
}
}
public void print(){
byte i=1;
if(front!=null){
System.out.println("ANTRIAN DATA MAHASISWA");
for (cObject t = front; t!=null; t=t.next) {
System.out.println(i+". NPM : "+t.getNPM());
i++;
}
}
else System.out.println("Antrian Data Mahasiswa Kosong!");
}
}
0 Comments