LINKED LIST
PROGRAM LINKED LIST
NAMA : ELI NURHAYATI
NPM : 20082010115
SOAL :
Buatlah
program linked list dengan menu seperti yang di Modul 22 untuk mengelola data
barang yang terdiri dari Kode, Nama, dan Harga.
SOURCE CODE
Class cNode
package linkedlist; public class cNode {
private String kode, nama;
private double harga;
cNode next;
cNode(String kd, String nm, double hrg){ kode=kd; nama=nm; harga=hrg;
next=null;
}
//Method Getter
public String getKode(){ return kode;
}
public String getNama(){ return nama;
}
public double getHarga(){ return harga;
} } |
Class
cLinkedList
package linkedlist; public class cLinkedList {
cNode kepala;
cNode ekor;
private int jNode;
cLinkedList(cNode n){ kepala=ekor=n; jNode=0;
}
public void addNode(cNode neww){ if(kepala==null){ kepala=ekor=neww; }else{ neww.next=kepala; kepala=neww; } System.out.println(">>
Berhasil Menambahkan Simpul... <<");
}
public void removeNode(){ if(kepala==null){ System.out.println(">>
Oops Linked List Masih Kosong <<"); }else{ cNode a=kepala.next; kepala=null; kepala=a; } System.out.println(">>
Berhasil Menghapus Simpul <<");
}
public void printNode(){ System.out.println(" DAFTAR LINKED LIST ");
System.out.println("=========================================================");
System.out.println("|\tKODE\t|\tNAMA\t|\tHARGA\t\t|");
System.out.println("========================================================="); for(cNode a=kepala ; a!=null ;
a=a.next){
System.out.println("|\t"+a.getKode()+"\t|\t"+a.getNama()+"\t|\t"+a.getHarga()+"\t\t|"); } System.out.println("");
}
public void searchNode(String b){ boolean found = false; for(cNode a=kepala ; a!=null ;
a=a.next){
if(a.getNama().equalsIgnoreCase(b)){ found=true; break; } } if(found==true){ System.out.println(">>
Yeey Produk Yang Anda Cari Berhasil Ditemukan <<"); } else { System.out.println(">>
Maaf Produk Yang Anda Cari Tidak Ditemukan <<"); } System.out.println("");
}
} |
Main Class
ProgramLinkedList
package linkedlist; import java.util.Scanner; public class ProgramLinkedList {
public static void main(String[] args) { cLinkedList ll = new
cLinkedList(null); Scanner s = new Scanner(System.in); int pilih; String kode, nama, cari; double harga; do{
System.out.println("\n======================"); System.out.println(" MENU LINKED LIST "); System.out.println("======================"); System.out.println("1.
Tambah Simpul\n2. Hapus Simpul\n3. Cetak Linked List\n4. Cari Produk\n5.
Exit"); System.out.print("Pilih :
"); pilih = s.nextInt(); switch(pilih){ case 1 :
System.out.print("Input Kode Produk : "); kode = s.next();
System.out.print("Input Nama Produk : "); nama = s.next(); System.out.print("Input Harga Produk :
"); harga = s.nextDouble(); cNode n = new cNode(kode,
nama, harga); ll.addNode(n); break; case 2 : ll.removeNode(); break; case 3 : ll.printNode(); break; case 4 :
System.out.print("Input Nama Produk Yang Dicari : "); cari = s.next(); ll.searchNode(cari); break; case 5 :
System.out.println(">>> THANK YOU <<<"); break; } }while(pilih!=5);
}
} |
SCREENSHOT
RUNNING PROGRAM
TAMBAH SIMPUL
CETAK LINKED
LIST
HAPUS SIMPUL
CARI PRODUK
EXIT
Komentar
Posting Komentar