package pkgLinkedList;
public class cNode {
private String nama;
cNode next;
cNode(String n){
nama=n;
next=null;
System.out.println("Object node "+n+" dibuat...");
}
public String getNama(){
return nama;
}
}
package pkgLinkedList;
public class cLinkedList {
cNode header, tail;
int js; //jumlah simpul
cLinkedList(cNode h){
header=h; tail=h;
js=0;
System.out.println("Object Linked List dibuat...");
}
public void addNode(cNode baru){
if(header==null){
header=tail=baru;
}else{
//penambahan simpul itu selalu di depan
baru.next = header;
header = baru;
}
System.out.println("Penambahan OK...");
}
public void delNode(){
//selalu menghapus simpul terdepan
if(header==null){
System.out.println("Linked List kosong!");
}
else{
cNode t = header.next;
header = null;
header = t;
System.out.println("Penghapusan OK...");
}
}
public void print(){
System.out.print("Isi LL: ");
for (cNode t=header; t!=null; t=t.next) {
System.out.print(t.getNama()+" ");
}
System.out.println("");
}
}
package pkgLinkedList;
import java.util.Scanner;
public class appLinkedList {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
cNode n;
cLinkedList LL = new cLinkedList(null);
int pilih=0;
do{
System.out.println("");
System.out.println("MENU 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 = ");
String nm = sc.next();
n = new cNode(nm);
LL.addNode(n);
break;
case 2:
LL.delNode();
break;
case 3:
LL.print();
break;
case 4:
System.out.println("Terima kasih...");
break;
}
}while(pilih!=4);
}
}
No comments:
Post a Comment