RSS2.0

Looking Something??



[Data Structure] Circularly Linked List - Java Programming Language

Saturday, August 9, 2008

Circularly-Linked List


A variant of a linked list in which the first and final nodes are linked together. This can be done for both singly and doubly linked lists. To traverse a circular linked list, you begin at any node and follow the list in either direction until you return to the original node. Viewed another way, circularly-linked lists can be seen as having no beginning or end. This type of list is most useful for managing buffers for data ingest, and in cases where you have one object in a list and wish to see all other objects in the list. The pointer pointing to the whole list may be called the access pointer.

[ Read More ]


[Data Structure] Doubly Linked List - Java Programming Language

Friday, July 18, 2008

Doubly-Linked List

A variant of a linked list in which each node has two links : one points to the previous node, or points to a null value or empty list if it is the first node; and one points to the next node, or points to a null value or empty list if it is the final node. This allows easily accessing list items backward as well as forward and deleting any item in constant time.

[ Read More ]


[Data Structure] Singly Linked List - Java Programming Language

Thursday, March 6, 2008

Singly-Linked List

Each item called Node in the Singly linked list is stored with an indication of where the next item is. To access the data in the middle of the list, head, which is the first node, is used to know where the first item is. Different with array, a linked list allows addition or deletion of items in the middle of collection with only a constant amount of data movement. To conclude, The list will be a chain of objects of type Node that contain the data and a reference to the next Node in the list.

The implementation of class Singly Linked List in Java Programming Language :

class Node{

int data;

Node next;

}

class LinkedList{

Node head; //posisi awal dari linked list

Node tail; //posisi akhir dari linked list

/**

* Fungsi untuk mengecek apakah linked list masih kosong

*/

boolean isEmpty(){

return (head==null);

}

void addFirst(Node input){

if (isEmpty()){ //Jika linked list masih kosong,

head = input; //maka head dan tail sama dengan node input

tail = input;

}

else {

input.next = head; //Jika linked list sudah berisi,

head = input; //maka input akan di depan dan menjadi head

}

}

void addLast(Node input){

if (isEmpty()){ //Jika linked list masih kosong,

head = input; //maka head dan tail sama dengan node input

tail = input;

}

else {

tail.next = input; //Jika linked list sudah berisi,

tail = input; //maka input akan di belakang dan menjadi tail

}

}

void insertAfter(int key,Node input){

Node temp = head;

do {

if (temp.data == key){ //Jika data sama dengan key, maka input

input.next = temp.next; //disambung diantara temp dan temp.next

temp.next = input;

System.out.println("Insert data is succeed.");

break; //dari temp --> temp.next menjadi :

} //temp --> input --> temp.next

temp = temp.next;

}

while (temp!=null);

}

void insertBefore(int key,Node input){

Node temp = head;

while (temp != null){

if ((temp.data == key)&&(temp == head)){

this.addFirst(input); /* jika insert pada awal linked list

maka call method addFirst */

System.out.println("Insert data is succeed.");

break;

}

else if (temp.next.data == key){

input.next = temp.next; //dari temp --> temp.next menjadi

temp.next = input; //temp --> input --> temp.next

System.out.println("Insert data is succeed.");

break;

}

temp = temp.next;

}

}

void removeFirst(){

Node temp = head;

if (!isEmpty()){

if (head == tail){ //jika element linked list hanya 1,

head = tail = null; //maka head dan tail menjadi null

} //sehingga linked list kosong

else {

temp = temp.next; //memajukan temp ke temp.next

head = temp; //kemudian head dipindah ke temp

temp = null; //kemudian temp di-null (optional)

}

}

else System.out.println("Data is empty!");

}

void removeLast(){

Node temp = head;

if (!isEmpty()){

if (tail == head){ //jika element linked list hanya 1

head = tail = null; //maka head dan tail menjadi null

} //sehingga linked list kosong

else {

while (temp.next != tail){

temp = temp.next; //memajukan temp hingga satu elemen

} //sebelum tail.

temp.next = null; //temp.next di-null,dan jadi akhir LL

tail = temp; //tail dipindah ke temp

temp = null;

}

}

else System.out.println("Data is empty!");

}

void remove(int key){

Node temp = head;

if (!isEmpty()){

while (temp != null){

if (temp.next.data == key){ //mengganti temp.next dengan

temp.next = temp.next.next; //temp.next.next

break; //dari temp --> temp.next -->temp.next.next

} //menjadi temp --> temp.next.next

else if ((temp.data == key)&&(temp == head)){

this.removeFirst();//jika key berada pada awal linked list,

break; //maka call method removeFirst

}

temp = temp.next;

}

} else System.out.println("Data is empty!");

}

void find (int key){

int i = 0;

boolean found = false;

Node temp = head;

while (temp != null){

if (temp.data == key){

found = true;

break;

}

i++;

temp = temp.next;

}

if (found){

System.out.println(key+" is found at index "+i);

}

else System.out.println("Data isn't found");

}

void printNode(){

Node temp;

temp = head;

while (temp != null){

System.out.println(temp.data);

temp = temp.next;

}

}

}

[ Read More ]


[Java] Linked List - Introduction - Data Structures

Linked List - Introduction

A Linked List is a concrete data structure consisting of a sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes.

The principal benefit of a linked list over a conventional array is that the order of the linked items may be different from the order that the data items are stored in memory or on disk, allowing the list of items to be traversed in a different order.

A linked list is a self-referential data type because it contains a pointer or link to another node of the same type.

Linked lists permit insertion and removal of nodes at any point in the list in constant time, but do not allow random access like array.

Several types of linked list :

  1. Singly-Linked List
  2. Doubly-Linked List
  3. Circularly-Linked List

a. Single-Circularly-Linked List

b. Double-Circularly-Linked List

[ Read More ]


[Java] Array - Java Programming Language

Array – Java Programming Language

Array Declaration and Initialization :

a) 1 Dimensional Array :

Declaration : |data type|[ ] |variable name|;

|data type||variable name|[ ];

Example : int x[ ]; /* or */ int [ ] x;

Initialize Array / Instansiate Object with constructor :

|variable name|= new |data type|[sum_of_array];

Example : x = new int[10];

We can also write these together :

|data type||variable name|[ ] = new |data type|[sum_of_array];

Example : int x[ ] = new int [10];

b) 2 Dimensional Array :

Declaration : |data type|[ ][ ] |variable name|;

|data type||variable name|[ ][ ];

Example : int x[ ][ ]; /* or */ int [ ][ ] x;

Initialize Array / Instansiate Object with constructor :

|variable name|= new |data type|[sum_of_array][sum_of_array];

Example : x = new int[10][10];

We can also write these together :

|data type||variable name|[ ] = new |data type|[sum_of_array] [sum_of_array];

Example : int x[ ][ ] = new int [10][10];

[ Read More ]