package pkgDaftarBerkait;
public class cSimpul {
private String nama;
cSimpul next;
cSimpul(String n){
nama=n; next=null;
System.out.println("Object "+n+" dibuat...");
}
public String getNama(){
return nama;
}
}
package pkgDaftarBerkait;
public class cDaftarBerkait {
cSimpul header;
cSimpul tail;
int jSimpul;
cDaftarBerkait(cSimpul s){
header=tail=s;
jSimpul=0;
System.out.println("Object Linked List dibuat...");
public void tambahSimpul(cSimpul baru){
if(header==null){ //LL kosong
header=tail=baru;
}
else{ //LL sdh berisi
//penambahan simpul selalu di depan
baru.next = header;
header = baru;
}
System.out.println("Penambahan sukses...");
}
public void hapusSimpul(){
//penghapusan selalu di ujung depan
if(header==null){
System.out.println("Linked List kosong!");
}
else{
cSimpul t = header.next; //calon header baru
header = null;
header = t;
System.out.println("Penghapusan sukses...");
}
}
public void cetak(){
System.out.print("Isi Linked List: ");
for (cSimpul t = header; t != null; t = t.next) {
System.out.print(t.getNama()+" ");
}
System.out.println("");
}
}
package pkgDaftarBerkait;
import java.util.Scanner;
public class appDaftarBerkait {
public static void main(String[] args) {
cSimpul s1 = new cSimpul("Sepatu");
cSimpul s2 = new cSimpul("Sandal");
cSimpul s3 = new cSimpul("Tas");
Scanner sc = new Scanner(System.in);
cDaftarBerkait db = new cDaftarBerkait(null);
int pilih=0;
do{
System.out.println("\nMENU LINKED LIST");
System.out.println("1. Tambah Simpul");
System.out.println("2. Hapus Simpul");
System.out.println("3. Cetak Linked List");
System.out.println("4. Exit");
System.out.print("Pilih = ");
pilih=sc.nextInt();
switch(pilih){
case 1:
System.out.print("Masukkan Nama Barang = ");
String nm = sc.next();
cSimpul b = new cSimpul(nm);
db.tambahSimpul(b);
break;
case 2:
db.hapusSimpul();
break;
case 3:
db.cetak();
break;
case 4:
System.out.println("Terima kasih...");
break;
}
}while(pilih!=4);
}
}
Ayo pakai software kontrol stok "MiniMart", GRATIS SEPENUHNYA & FULL OPEN-SOURCE LHOO .....
ReplyDeleteSilahkan download software "MiniMart" di sini :
https://helloyud.blogspot.com/2016/12/aplikasi-pertokoan-minimart.html