package contoh;
public class Contoh {
public static class t_dll_node {
float data;
t_dll_node prev, next;
}
public static t_dll_node NodeAwal, NodeAkhir;
public static t_dll_node DLL_CariTerdekat (float datacari) {
if (NodeAwal==null) return null;
t_dll_node trav = NodeAwal;
while (trav.data<datacari) {
trav = trav.next;
if(trav== null) return null;
}
return trav;
}
public static void DLL_Insert (float dataisi) {
t_dll_node NodeBaru=new t_dll_node();
NodeBaru.data=dataisi;
NodeBaru.prev= NodeBaru.next=null;
if (NodeAwal==null) NodeAkhir=NodeAwal=NodeBaru;
else {
t_dll_node posisiinsert=DLL_CariTerdekat(dataisi);
if(posisiinsert==null) {
NodeBaru.prev=NodeAkhir;
NodeAkhir.next=NodeBaru;
NodeAkhir=NodeBaru;
}else {
if(posisiinsert==NodeAwal) {
NodeAwal.prev=NodeBaru;
NodeBaru.next=NodeAwal;
NodeAwal=NodeBaru;
}else{
posisiinsert.prev.next=NodeBaru;
NodeBaru.prev=posisiinsert.prev;
NodeBaru.next=posisiinsert;
posisiinsert.prev=NodeBaru;
}
}
}
}
public static void DLL_CetakData() {
t_dll_node trav = NodeAwal;
if(trav==null) System.out.println("DLL Masih Kosong");
else {
System.out.println("Isi Dari DLL : ");
while (trav!=null) {
System.out.println(trav.data);
trav=trav.next;
}
System.out.println("=============");
}
}
public static t_dll_node DLL_Cari(float datacari) {
if(NodeAwal==null) return null;
t_dll_node trav = NodeAwal;
while (trav!=null){
if (trav.data==datacari) return trav;
trav = trav.next;
}
return null;
}
public static boolean DLL_Delete (float datahapus) {
t_dll_node posisidelete = DLL_Cari(datahapus);
if (posisidelete == null) return false;
if (NodeAwal==NodeAkhir) NodeAwal = NodeAkhir =null;
if (posisidelete == NodeAwal){
NodeAwal=NodeAwal.next;
NodeAwal.prev=null;
}else {
if(posisidelete==NodeAkhir){
NodeAkhir=NodeAkhir.prev;
NodeAkhir.next=null;
}else {
posisidelete.next.prev=posisidelete.prev;
posisidelete.prev.next=posisidelete.next;
}
}
return true;
}
public static void DLL_Delete_All(){
NodeAwal=NodeAkhir=null;
}
public static void DLL_jumlah() {
}
public static void main(String[] args) {
DLL_Delete_All();
DLL_CetakData();
DLL_Insert(78f);
DLL_Insert(55f);
DLL_Insert(75f);
DLL_Insert(60f);
DLL_CetakData();
DLL_Delete(60f);
DLL_CetakData();
}
}
public class Contoh {
public static class t_dll_node {
float data;
t_dll_node prev, next;
}
public static t_dll_node NodeAwal, NodeAkhir;
public static t_dll_node DLL_CariTerdekat (float datacari) {
if (NodeAwal==null) return null;
t_dll_node trav = NodeAwal;
while (trav.data<datacari) {
trav = trav.next;
if(trav== null) return null;
}
return trav;
}
public static void DLL_Insert (float dataisi) {
t_dll_node NodeBaru=new t_dll_node();
NodeBaru.data=dataisi;
NodeBaru.prev= NodeBaru.next=null;
if (NodeAwal==null) NodeAkhir=NodeAwal=NodeBaru;
else {
t_dll_node posisiinsert=DLL_CariTerdekat(dataisi);
if(posisiinsert==null) {
NodeBaru.prev=NodeAkhir;
NodeAkhir.next=NodeBaru;
NodeAkhir=NodeBaru;
}else {
if(posisiinsert==NodeAwal) {
NodeAwal.prev=NodeBaru;
NodeBaru.next=NodeAwal;
NodeAwal=NodeBaru;
}else{
posisiinsert.prev.next=NodeBaru;
NodeBaru.prev=posisiinsert.prev;
NodeBaru.next=posisiinsert;
posisiinsert.prev=NodeBaru;
}
}
}
}
public static void DLL_CetakData() {
t_dll_node trav = NodeAwal;
if(trav==null) System.out.println("DLL Masih Kosong");
else {
System.out.println("Isi Dari DLL : ");
while (trav!=null) {
System.out.println(trav.data);
trav=trav.next;
}
System.out.println("=============");
}
}
public static t_dll_node DLL_Cari(float datacari) {
if(NodeAwal==null) return null;
t_dll_node trav = NodeAwal;
while (trav!=null){
if (trav.data==datacari) return trav;
trav = trav.next;
}
return null;
}
public static boolean DLL_Delete (float datahapus) {
t_dll_node posisidelete = DLL_Cari(datahapus);
if (posisidelete == null) return false;
if (NodeAwal==NodeAkhir) NodeAwal = NodeAkhir =null;
if (posisidelete == NodeAwal){
NodeAwal=NodeAwal.next;
NodeAwal.prev=null;
}else {
if(posisidelete==NodeAkhir){
NodeAkhir=NodeAkhir.prev;
NodeAkhir.next=null;
}else {
posisidelete.next.prev=posisidelete.prev;
posisidelete.prev.next=posisidelete.next;
}
}
return true;
}
public static void DLL_Delete_All(){
NodeAwal=NodeAkhir=null;
}
public static void DLL_jumlah() {
}
public static void main(String[] args) {
DLL_Delete_All();
DLL_CetakData();
DLL_Insert(78f);
DLL_Insert(55f);
DLL_Insert(75f);
DLL_Insert(60f);
DLL_CetakData();
DLL_Delete(60f);
DLL_CetakData();
}
}
0 komentar:
Post a Comment
Silahkan Tinggalkan Komentar Anda