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

Contoh Program Linked List dalam Bahasa Java

class Link
{
public int iData; // data item (key)
public double dData; // data item
public Link next; // next link in list
public Link(int id, double dd) // constructor
{
iData = id; // initialize data
dData = dd; // ('next' is automatically
} // set to null)

public void displayLink() // display ourself
{
System.out.print("{" + iData + ", " + dData + "} ");
}
} // end class Link
class LinkList
{
private Link first; // ref to first link on list
public LinkList() // constructor
{
first = null; // no items on list yet
}
public boolean isEmpty() // true if list is empty
{
return (first==null);
}
public void insertFirst(int id, double dd)
{ // make new link
Link newLink = new Link(id, dd);
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
public Link deleteFirst() // delete first item
{ // (assumes list not empty)
Link temp = first; // save reference to link
first = first.next; // delete it: first-->old next
return temp; // return deleted link
}
public void displayList()
{
System.out.print("List (first-->last): ");
Link current = first; // 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("");
}
} // end class LinkList
class LinkListApp
{
public static void main(String[] args)
{
LinkList theList = new LinkList(); // make new list
theList.insertFirst(22, 2.99); // insert four items
theList.insertFirst(44, 4.99);
theList.insertFirst(66, 6.99);
theList.insertFirst(88, 8.99);
theList.displayList(); // display list
while( !theList.isEmpty() ) // until it's empty,
{
Link aLink = theList.deleteFirst(); // delete link
System.out.print("Deleted "); // display it
aLink.displayLink();
System.out.println("");
}
theList.displayList(); // display list
} // end main()
} // end class LinkListApp

// sumber: Data Structures and Algorithm, Sam Publishing

Search Engine Optimizer (SEO)

Sudah hampir satu bulan ini mencoba SEO. Konon katanya bisa menaikkan rating web di www.alexa.com. Awalnya cuma coba-coba saja. Ingin membuktikan. Ternyata ada benarnya. Sebelum memakai jasa SEO, rating web blog ini dulu ada di nomer 5 jutaan. Hari ini saya lihat di Alexa Toolbar sudah mencapai 2.006.310. Luar Biasa! Hanya dalam waktu sekitar satu bulan sudah bisa naik 3.000.000-an peringkat. Memang traffic pengunjung mengalami peningkatan. Dari yang biasanya 20-30an perhari, sekarang menjadi rata-rata di atas 50 pengunjung sehari. Masih sedikit memang, tapi Alhamdulillah. Artikel yang saya post di web blog ini cukup dicari oleh yang membutuhkannya. Semoga bermanfaat :).

Sekarang sedang menunggu-nunggu, kapan bisa tembus 1.000.000-an. Dan syukur-syukur bisa lebih baik lagi. Dukung ya! :)

Contoh Program OOP dengan Inheritance Sederhana dalam Bahasa Java

import java.io.*;
import java.util.Scanner;
class makanan
{
public int porsi;
public double harga;
}
class nasi extends makanan
{
public void setharga()
{
harga = 1000;
}
}
class lauk extends makanan
{
public void setharga()
{
harga = 2000;
}
}
class sayur extends makanan
{
public void setharga(double h) //set harga lewat input user
{
harga = h;
}
public double getharga() //mengambil info harga dr class
{
return(harga);
}
}
public class program //utk menjalankan program utama
{
public static void main(String[] args)
{
double h;
sayur s = new sayur(); //create object utk masing2 class
lauk l = new lauk();
nasi n = new nasi();
Scanner sc = new Scanner(System.in);

System.out.print("Harga : ");
h = sc.nextDouble();
s.setharga(h);
System.out.println("Harga sayur :"+s.getharga());
n.harga = 1000; //akses langsung ke atribut
n.porsi = 2;
biaya = n.harga * n.porsi;
System.out.println("Biaya :"+biaya);

//menu program dan manipulasi object yg lain
//bisa diletakkan di sini
}
}

Contoh Program Queue Array Sederhana dalam Java

import java.io.*;
public class queue_array
{
private int maxsize; //maksimum ukuran queue
private double [] queuearray; //array untuk menyimpan queue
private int front; //indeks elemen terdepan
private int rear; //indeks elemen paling belakang
private int jumlah; //menyimpan jumlah total elemen yang ada

public void inisiasi(int s) //inisialisasi queue
{
maxsize = s;
queuearray = new double [maxsize];
front = rear = jumlah = 0;
}
public void enqueue(double data)
{
if (jumlah==maxsize)
System.out.println("Queue Penuh. "+data+" Tidak Bisa Masuk");
else
{
//System.out.print(rear+" ");
queuearray[rear] = data;
rear = (rear+1)%maxsize;
jumlah++;
System.out.println(data +" Masuk ke Queue");
}
}
public double dequeue()
{
double temp;
if (jumlah==0)
{
System.out.println("Queue Sudah Kosong");
return(-1);
}
else
{
temp = queuearray[front];
front = (front+1)%maxsize;
jumlah--;
System.out.println(temp + " Keluar dari Queue");
return (temp);
}
}
public void view()
{
System.out.print("Isi Queue: ");
if (jumlah > 0 && front < rear) //belum terjadi circular
for(int i=front; i0 && rear <= front) //terjadi circular
{
for(int i=front; i<=maxsize-1; i++) //bagian depan
System.out.print(queuearray[i] + " ");
for(int i=0; i<rear; i++) //bagian belakang
System.out.print(queuearray[i] + " ");
}
System.out.println();
}
public static void main(String[] args)
{
queue_array queue = new queue_array();
queue.inisiasi(3);
queue.enqueue(2);
queue.enqueue(5);
queue.enqueue(3);
queue.enqueue(7);
queue.view();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.view();
queue.enqueue(8);
queue.enqueue(9);
queue.enqueue(7);
queue.enqueue(10);
queue.view();
queue.dequeue();
queue.view();
}
}

//Selamat Mencoba ^_^

Download Source Code