Linked List : Implementasi Linked List dalam Java

21 01 2010

5. Bagaimanakah proses pembuatan Linked List dalam java!

(Jelaskan dengan detail dan contoh code-nya)

Dalam Java ada dua cara untuk menyatakan list, yaitu menggunakan array dinamis dan linked list. Berikut ini adalah contoh implementasi linked list untuk menambahkan dan memindahkan node pada bagian depan dan belakang list :

Sourcecode :

// Fig. 17.5: ListTest.java
// ListTest class to demonstrate List capabilities.
import com.deitel.jhtp6.ch17.List;
import com.deitel.jhtp6.ch17.EmptyListException;
public class ListTest
{
public static void main( String args[] )
{
List list = new List(); // create the List container

// insert integers in list
list.insertAtFront( -1 );
list.print();
list.insertAtFront( 0 );
list.print();
list.insertAtBack( 1 );
list.print();
list.insertAtBack( 5 );
list.print();

// remove objects from list; print after each removal
try
{
Object removedObject = list.removeFromFront();
System.out.printf( "%s removed\n", removedObject );
list.print();

removedObject = list.removeFromFront();
System.out.printf( "%s removed\n", removedObject );
list.print();

removedObject = list.removeFromBack();
System.out.printf( "%s removed\n", removedObject );
list.print();

removedObject = list.removeFromBack();
System.out.printf( "%s removed\n", removedObject );
list.print();
} // end try
catch ( EmptyListException emptyListException )
{
emptyListException.printStackTrace();
} // end catch
} // end main
} // end class ListTest

Output :

The list is: -1

The list is: 0 -1

The list is: 0 -1 1

The list is: 0 -1 1 5

0 removed

The list is: -1 1 5

-1 removed

The list is: 1 5

5 removed

The list is: 1

1 removed

Empty list

Posted By : Evan Yofiyanto @ Evan’s Blog : Kuliah Informatika (kuliahinformatika.wordpress.com)

[FREAX]


Actions

Information

3 responses

21 01 2010
mobil88
21 01 2010
Evan

Just wanna share some projects from my college
Thx 4 walking… Ur blog is a nice one too🙂

19 05 2010
Simon

Just wanna ask, did you try the supermarket queue simulation? You have the source code? Can I get it? Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: