DOUBLE LINKED LIST
PROGRAM DOUBLE LINKED LIST
NAMA : ELI NURHAYATI
NPM : 20082010115
SOAL :
Buatlah program sederhana untuk mengelola data barang yang terdiri dari Kode, Nama, dan Harga dengan menggunakan double linked list.Menu program setidaknya untuk menambah simpul baru, menghapus simpul, menampilkan linked list (maju dan mundur), serta mencari data Nama di dalam linked list.
SOURCE CODE
Class cNode
package DoubleLinkedList; public class cNode { private
String kode, nama; private
double harga; cNode
next, prev; cNode
belakang;
cNode(String kd, String nm, double hrg){
kode=kd; nama=nm; harga=hrg; next=prev=null; } //Method
Getter public
String getKode(){ return
kode; } public
String getNama(){ return
nama; } public
double getHarga(){ return
harga; } } |
Class cDoubleLinkedList
package DoubleLinkedList; public class cDoubleLinkedList { cNode
kepala, ekor; private
int jNode;
cDoubleLinkedList(){
kepala=ekor=null;
jNode=0;
System.out.println("Object Double Linked List Dibuat..."); } public
void addNode(cNode neww){
if(ekor==null){ kepala=ekor=neww; }else{
ekor.next=neww;
neww.prev=ekor;
ekor=neww; }
System.out.println(">> Berhasil Menambahkan Simpul...
<<"); } public
void removeNode(){
if(ekor==null){
System.out.println(">> Oops Linked List Masih Kosong
<<"); }else
if(ekor.prev==null){
kepala=ekor=null; }else{
cNode a=ekor.prev;
ekor.prev=null;
ekor=a;
a.next=null; }
System.out.println(">> Berhasil Menghapus Simpul
<<"); } public
void printNode(){ System.out.println(" DOUBLE LINKED LIST MAJU ");
System.out.println("=========================================================");
System.out.println("|\tKODE\t|\tNAMA\t|\tHARGA\t\t|");
System.out.println("=========================================================");
for(cNode a=ekor ; a!=null ; a=a.prev){
System.out.println("|\t"+a.getKode()+"\t|\t"+a.getNama()+"\t|\t"+a.getHarga()+"\t\t|"); }
System.out.println(""); System.out.println(" DOUBLE LINKED LIST
MUNDUR ");
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("\n"); } public
void searchNode(String b){
boolean found = false;
if(ekor==null){
System.out.println(">> Oops Linked List Masih Kosong
<<"); }else{
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 <<"); } } } |
Main Class ProgramDoubleLinkedList
package DoubleLinkedList; import java.util.Scanner; public class ProgramDoubleLinkedList { public
static void main(String[] args) {
cDoubleLinkedList dll = new cDoubleLinkedList();
Scanner s = new Scanner(System.in); int
pilih=0; 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);
dll.addNode(n); break;
case 2 :
dll.removeNode(); break;
case 3 :
dll.printNode();
break;
case 4 :
System.out.print("Input Nama Produk Yang Dicari : ");
cari = s.next();
dll.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