class ke 1
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package khanif_dll;
/**
*
* @author khanif
*/
public class Khanif_DLL {
public long Data;
public Khanif_DLL next;
public Khanif_DLL previous;
//------------------------------------------------------
public Khanif_DLL (long x)
{ Data = x; }
//------------------------------------------------------
public void displayLink()
{ System.out.print("{" + Data + "} "); }
//------------------------------------------------------
}
class ke 2
package khanif_dll;
public class kedua {
private Khanif_DLL head;
private Khanif_DLL tail;
// -----------------------------------------------
public kedua () {
head = null;
tail = null;
}
// -----------------------------------------------
public boolean isEmpty () {
return head==null;
}
// -----------------------------------------------
public void insertdepan (long x) {
Khanif_DLL newLink = new Khanif_DLL(x);
if( isEmpty () )
tail = newLink;
else
head.previous = newLink;
newLink.next = head;
head = newLink;
}
// -----------------------------------------------
public void insertblakang (long x) {
Khanif_DLL newLink = new Khanif_DLL(x);
if ( isEmpty () )
head = newLink;
else
{
tail.next = newLink;
newLink.previous = tail;
}
tail = newLink;
}
// -----------------------------------------------
public Khanif_DLL deletedepan () {
Khanif_DLL temp = head;
if (head.next == null)
tail = null;
else
head.next.previous = null;
head = head.next;
return temp;
}
// -----------------------------------------------
public Khanif_DLL deleteblakang () {
Khanif_DLL temp = tail;
if (head.next == null)
head = null;
else
tail.previous.next = null;
tail = tail.previous;
return temp;
}
// -----------------------------------------------
public boolean insertsesudah (long tengah, long x) {
Khanif_DLL current = head;
while (current.Data != tengah)
{
current = current.next;
if (current == null)
return false;
}
Khanif_DLL newLink = new Khanif_DLL (x);
if (current==tail)
{
newLink.next = null;
tail = newLink;
}
else
{
newLink.next = current.next;
current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;
return true;
}
// -----------------------------------------------
public boolean insertsebelum (long tengah, long x) {
Khanif_DLL current = tail;
while (current.Data != tengah)
{
current = current.previous;
if (current == null)
return false;
}
Khanif_DLL newLink = new Khanif_DLL(x);
if (current==head)
{
newLink.previous = null;
head = newLink;
}
else
{
newLink.previous = current.previous;
current.previous.next = newLink;
}
newLink.next = current;
current.previous = newLink;
return true;
}
// -----------------------------------------------
public Khanif_DLL deletetengah(long tengah) {
Khanif_DLL current = head;
while (current.Data != tengah)
{
current = current.next;
if(current == null)
return null;
}
if (current==head)
head= current.next;
else
current.previous.next = current.next;
if (current==tail)
tail = current.previous;
else
current.next.previous = current.previous;
return current;
}
// -----------------------------------------------
public void tampildepan () {
System.out.print (" ");
Khanif_DLL current = head;
while (current != null)
{
current.displayLink ();
current = current.next;
}
System.out.println("");
}
// -----------------------------------------------
public void tampilblakang () {
Khanif_DLL current = tail;
while (current != null)
{
current.displayLink ();
current = current.previous;
}
System.out.println ("");
}
} class ke 3
package khanif_dll;
public class ketiga {
public static void main (String[] args) {
kedua panggil = new kedua();
panggil.insertdepan(7);
panggil.insertdepan(2);
panggil.insertdepan(5);
panggil.insertblakang(9);
panggil.insertblakang(3);
panggil.insertblakang(6);
// memasukkan data depan dan belakang
//------------------------------------------------------
System.out.println("tampil dr depan (forward):");
panggil.tampildepan();
System.out.println("tampil dr belakang (backward):");
panggil.tampilblakang();
// menampilkan data kedepan dan kebelakang
//------------------------------------------------------
System.out.println("insert tengah (after): ");
panggil.insertsesudah(7, 76);
panggil.tampildepan();
// memasukkan data 76 sesudah data 7
//------------------------------------------------------
System.out.println("insert tengah (before): ");
panggil.insertsebelum(3, 59);
panggil.tampildepan();
// memasukkan data 59 sebelum data 3
//------------------------------------------------------
System.out.println("hasil delete depan : ");
panggil.deletedepan();
panggil.tampildepan();
// menghapus data paling depan
//------------------------------------------------------
System.out.println("hasil delete belakang : ");
panggil.deleteblakang();
panggil.tampildepan();
// menghapus data paling belakang
//------------------------------------------------------
System.out.println("hasil delete tengah : ");
panggil.deletetengah(7);
panggil.tampildepan();
// menghapus data ditengah yaitu data 7
}
}
No comments:
Post a Comment