Contoh Program Double Linked List dalam Bahasa Java

Berikut ini contoh program sederhana dalam bahasa Java (console) untuk implementasi konsep struktur data Double Linked List. Double Linked List adalah modifikasi Single Linked List sehingga mempunyai dua buah variabel/ atribut untuk mereferensi lokasi data sebelum dan sesudahnya. Biasanya dinamai Next dan Previous. Untuk contoh ini menggunakan konsep InsertBack, dimana penambahan node (simpul) baru selalu berada di belakang. Sedangkan penghapusannya memakai metode DeleteFirst, dimana simpul yang dihapus selalu yang berada di posisi terdepan. Selamat mencoba :)

class Node //atau node/simpul
{
public int data; // data item
public Node next; // next node link in list
public Node prev; // previous node link in list
public Node(int id) // constructor
{
data = id; // initialize data
} // set to null)

public void displayLink() // display ourself
{
System.out.print("{" + data + "} ");
}
} // end class Link
class LinkList
{
private Node head; // ref to first link on list
private Node tail; // ref to last link on list
public LinkList() // constructor
{
head = tail = null; // no items on list yet
}
public boolean isEmpty() // true if list is empty
{
return (head==null);
}
public void insertBack(int id) //node baru selalu di belakang
{ // make new link
Node newNode = new Node(id);
if (tail == null) // the first node created
{
head = tail = newNode; // first --> newLink
}
else // the second node and the next node
{
tail.next = newNode; //next dr tail (awal) diarahkan ke node baru
newNode.prev = tail; //prev dr node baru diarahkan ke tail (awal)
tail = newNode; //tail (baru) diarahkan ke node baru
}
}
public Node deleteFirst() // delete first item
{ // (assumes list not empty)
Node temp = head; // save reference to link
head.prev = null;
head = head.next; // delete it: first-->old next
return temp; // return deleted link
}
public void displayForward()
{
Node current = head; // start at beginning of list
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
public void displayBackward()
{
Node current = tail; // start at beginning of list
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.prev; // move to next link
}
System.out.println("");
}
} // end class LinkList
class LinkListApp
{
public static void main(String[] args)
{
LinkList theList = new LinkList(); // make new list
System.out.println("Initializing Double Linked List...");
theList.insertBack(22); // insert four items
theList.insertBack(44);
theList.insertBack(66);
theList.insertBack(88);
System.out.println("Display Forward :");
theList.displayForward(); // display list
System.out.println("Display Backward :");
theList.displayBackward();
System.out.println("Delete List from Head...");
while( !theList.isEmpty() ) // until it's empty,
{
Node aLink = theList.deleteFirst(); // delete link
System.out.print("Deleted "); // display it
aLink.displayLink();
System.out.println("");
}
theList.displayForward(); // display list
} // end main()
} // end class LinkListApp

2 comments: