import java.util.Scanner;
class Peserta {
String nama;
String nrp;
Peserta nextPes,prevPes; // u/ nge-link
public Peserta (String nama, String nrp){ // contructor
this.nama = nama;
this.nrp = nrp;
}
}
class Kelas {
Peserta head;
Peserta tail;
String nmKlas;
int kapasitas; // kapasitas kelas
int jumlah; // kursi yang terisi
Kelas nextKel,prevKel; // u/ nge-link
public Kelas(String namaKlas,int k){ // constructor
nmKlas = namaKlas;
kapasitas = k;
jumlah = 0;
head = tail = null;
}
void edit(int k) {
kapasitas = k;
}
public Peserta find (String nrp){
Peserta search = head;
while (search!=null){
if(search.nrp.equals(nrp)) // jika search = nrp
return search;
search=search.nextPes; // posisi search digeser
}
return null; // jika tidak ada, return null
}
void cetakPeserta(){
System.out.println(“\nKelas “+nmKlas+” (“+jumlah+”/”+kapasitas+”) :”);
Peserta p = head;
while(p != null) {
System.out.println(p.nrp+” – “+p.nama);
p = p.nextPes;
}
}
void addPeserta(Peserta input){
if(head == null) { // jika belum ada isinya,
head = input; // input diposisikan sbg head dan tail
tail = input;
jumlah++; // kursi yang terisi bertambah
}
else if (head.nrp.compareTo(input.nrp) > 0) { // jika head.nrp > input.nrp
input.nextPes = head;
head.prevPes = input;
head = input;
jumlah++;
}
else if (tail.nrp.compareTo(input.nrp) < 0) { // jika tail.nrp < input.nrp
tail.nextPes = input;
input.prevPes = tail;
tail = input;
jumlah++;
}
else {
Peserta temp, temp2;
temp = head;
while(temp.nrp.compareTo(input.nrp) < 0) { // jika temp.nrp < input.nrp
temp = temp.nextPes;
}
temp.prevPes.nextPes = input;
input.prevPes = temp.prevPes;
temp.prevPes = input;
input.nextPes = temp;
jumlah++;
}
}
void removeFirst(){
if(head!=null){
if(head==tail){ // jika hanya ada 1 node
head=tail=null;
jumlah–;
}
else{ // jika ada >1 node
head=head.nextPes; // posisi head digeser ke next
jumlah–; // shg otomatis head awal hilang
}
}
}
void removeLast(){
if(head!=null){
if(head==tail){ // jika hanya ada 1 node
head=tail=null;
jumlah–;
} // jika ada >1 node
else{
Peserta temp = head;
while(temp.nextPes!=tail){ // di loop hingga bertemu tail
temp=temp.nextPes;
}
tail=temp;
temp.nextPes=null;
jumlah–;
}
}
}
void removePeserta(String nrpPes){
Peserta pointer = find(nrpPes); // pointer di posisi nrp yg ingin di-del
if(pointer==null)return;
else if(pointer==head)removeFirst();
else if(pointer==tail)removeLast();
else{
Peserta beforeData = head;
while(beforeData.nextPes!=pointer){ // beforeData digeser hingga bertemu pointer
beforeData=beforeData.nextPes;
}
Peserta afterData = pointer.nextPes;
beforeData.nextPes = afterData;
jumlah–;
}
}
}
class MK { // sebagian besar method di MK sama dengan method di Kelas
Kelas head; // hanya berbeda atributnya
Kelas tail;
public MK(){}
public Kelas find (String kelasApa){
Kelas search = head;
while (search!=null){
if(search.nmKlas.equals(kelasApa))
return search;
search=search.nextKel;
}
return null;
}
Kelas cariPeserta(String nrp) {
Kelas temp = head;
while(temp != null) {
if(temp.find(nrp) != null) {
return temp;
}
temp = temp.nextKel;
}
return null;
}
void cetakKelas(){
Kelas temp;
temp=head;
while (temp!=null){
System.out.print(” “+ temp.nmKlas + ” ” + temp.kapasitas);
temp = temp.nextKel;
}
}
void cetakSemua () {
Kelas temp;
temp = head;
while(temp != null) {
temp.cetakPeserta();
temp = temp.nextKel;
}
}
void addKelas(){
Scanner input = new Scanner(System.in);
System.out.print(“Banyak kelas : “);
String in = input.nextLine();
int bnyk = Integer.parseInt(in);
for(int i=1;i<=bnyk;i++) {
Kelas data;
System.out.print(“Nama kelas : “);
String nama = input.nextLine();
System.out.print(“Kapasitas : “);
in = input.nextLine();
int k = Integer.parseInt(in);
data = new Kelas(nama,k);
if(head == null) {
head = data;
tail = data;
}
else if (head.nmKlas.compareTo(data.nmKlas) > 0) {
data.nextKel = head;
head.prevKel = data;
head = data;
}
else if (tail.nmKlas.compareTo(data.nmKlas) < 0) {
tail.nextKel = data;
data.prevKel = tail;
tail =data;
}
else {
Kelas temp;
temp = head;
while(temp.nmKlas.compareTo(data.nmKlas) < 0) {
temp = temp.nextKel;
}
temp.prevKel.nextKel = data;
data.prevKel = temp.prevKel;
temp.prevKel = data;
data.nextKel = temp;
}
data = null;
System.out.println(“Kelas “+nama+” telah ditambahkan”);
}
}
void cariKelas() {
Scanner input = new Scanner(System.in);
System.out.print(“Masukkan nama kelas : “);
String nama = input.nextLine();
if(find(nama) == null) {
System.out.println(“Kelas tidak ada”);
}
else {
find(nama).cetakPeserta();
}
}
void editKap() {
Scanner input = new Scanner(System.in);
Kelas temp = null;
System.out.print(“Pilih kelas : “);
String kel = input.nextLine();
temp = find(kel);
if(temp == null) {
System.out.println(“Kelas tidak ada”);
}
else {
System.out.println(“Kapasitas sekarang = “+temp.kapasitas);
System.out.println(“Kapasitas baru = “);
kel = input.nextLine();
int k = Integer.parseInt(kel);
temp.kapasitas = k;
}
}
}
public class prakShift2mataKuliah {
public static void main(String[] args) {
System.out.println(“** PRAKTIKUM MODUL 1 SHIFT 2 PSD 2008 **”);
System.out.println(“ LINKED LIST”);
System.out.println(“ by Kelompok 76″);
System.out.println(“ Rina_74 & Venty_168″);
Scanner input = new Scanner(System.in);
MK matKul = new MK();
int pilihan;
do{
System.out.println(“\nMenu :”);
System.out.println(“1. Menambah kelas”);
System.out.println(“2. Menambah peserta”);
System.out.println(“3. Menghapus peserta”);
System.out.println(“4. Edit kapasitas kelas”);
System.out.println(“5. Cari kelas”);
System.out.println(“6. Cari peserta”);
System.out.println(“7. Tampilkan semua data”);
System.out.println(“8. Keluar”);
System.out.println(“Masukkan pilihan >> “);
String pil = input.nextLine();
pilihan = Integer.parseInt(pil);
switch (pilihan) {
case 1 : matKul.addKelas();
break;
case 2 : tambahPeserta(matKul);
break;
case 3 : hapusPeserta(matKul);
break;
case 4 : matKul.editKap();
break;
case 5 : matKul.cariKelas();
break;
case 6 : cariPeserta(matKul);
break;
case 7 : matKul.cetakSemua();
break;
}
}
while(pilihan != 8);
}
static String inputNRP(){
Scanner input = new Scanner(System.in);
String nrp;
System.out.print(“Masukkan NRP : “);
nrp = input.nextLine();
return nrp;
}
static void cariPeserta(MK matKul) {
String nrp = inputNRP();
if(matKul.cariPeserta(nrp) == null) {
System.out.println(“Peserta tidak ada”);
}
else {
Peserta temp = matKul.cariPeserta(nrp).find(nrp);
System.out.println(temp.nrp+” – “+temp.nama);
System.out.println(“Kelas “+matKul.cariPeserta(nrp).nmKlas);
}
}
static void hapusPeserta(MK matKul) {
String nrp = inputNRP();
if(matKul.cariPeserta(nrp) != null) {
matKul.cariPeserta(nrp).removePeserta(nrp);
}
}
static void tambahPeserta(MK matKul) {
Scanner input = new Scanner(System.in);
String kelas;
Kelas temp = null;
do {
System.out.print(“Pilih kelas : “);
kelas = input.nextLine();
temp = matKul.find(kelas);
if(temp == null){ // cek apakah kelas sudah diciptakan??
System.out.println(“Kelas tidak ada”);
}
else if(temp.jumlah == temp.kapasitas) { // jika jumlah = kapasitas, tidak bisa tambah peserta
System.out.println(“Kapasitas kelas sudah penuh !!”);
temp = null;
}
}while((temp == null));
Peserta p;
System.out.print(“Masukkan nama : “);
String nama = input.nextLine();
String nrp = inputNRP();
if(matKul.cariPeserta(nrp) != null)
System.out.println(“NRP Anda sudah terdaftar”);
else {
p = new Peserta(nama,nrp);
temp.addPeserta(p);
}
}
}