Contoh Program Java Link list

3/04/2014 Unknown 0 Comments

Share nih, soure code dan screenshoot output Program Java Link List, disini saya menggunakan Blue-J. check this!

Source Code

class Node
{
int dataInt;
long dataLong;
double dataDouble;

String data;
char dataChar;
Node next;
}



class LinkedList
{
private static Node node;
static void insert(Node newNode, int n)
{
if(n == 1)
{
newNode.next = node;
node = newNode;
}
else
{
Node copy = node;
for(int i = 1; i < n - 1; i++)
node = node.next;
newNode.next = node.next;
node.next = newNode;
node = copy;
}
}
static void remove(int n)
{
if(n == 1)
{
node = node.next; 
}
else
{
Node copy = node; 
for(int i = 1; i < n - 1; i++)
node = node.next;
node.next = node.next.next;
node = copy; 
}
}
static void find(int data)
{
Node ketemu = null, copy = node;
int counter = 1, i = 0;
while(true)
{
try
{
if(node.dataInt == data)
{
Node neo = new Node();
neo.dataInt = counter;
ketemu = insert(ketemu, neo, i + 1);
i++;
}
node = node.next;
counter++;
}
catch(NullPointerException accept)
{
break;
}
}
node = copy;
foundInfo(ketemu);
}
static void printLinkedList()
{
Node copy = node;
while(true)
{
try
{
System.out.print(node.dataInt + "-");
node = node.next;
}
catch(NullPointerException accept)
{
System.out.println(",..");
break;
}
}
node = copy; 
}
private static void foundInfo(Node current)
{
if(current == null)
System.out.println("data tidak ditemukan!!!");
else
{
System.out.print("temukan data  : ");
while(true)
{
try
{
System.out.print(current.dataInt + ", ");
current = current.next;
}
catch(NullPointerException accept)
{
System.out.println("............");
break;
}
}
}
}
private static Node insert(Node node, Node newNode, int n)
{
if(n == 1)
{
newNode.next = node;
node = newNode;
}
else
{
Node copy = node;
for(int i = 1; i < n - 1; i++)
node = node.next;
newNode.next = node.next;
node.next = newNode;
node = copy;
}
return node;
}
}



import java.util.Scanner;
class ll
{
public static void main(String [] args)
{
LinkedList list = new LinkedList();
Scanner kure = new Scanner(System.in);
System.out.print("jumlah data yang ingin dimasukkan : ");
int nomor = kure.nextInt();
System.out.println();
for(int i = 0; i < nomor; i++)
{
System.out.print(i + 1 + "  : ");
Node xxx = new Node();
int data = kure.nextInt();
xxx.dataInt = data;
list.insert(xxx, i + 1);
}
System.out.println();
list.printLinkedList();
System.out.println("\nmenyipkan data ");
System.out.print("data yang ingin disisipkan : ");
int datas = kure.nextInt();
System.out.print("posisi nomor yang akan disisipkan: ");
int n = kure.nextInt();
Node newNode = new Node();
newNode.dataInt = datas;
list.insert(newNode, n);
System.out.println("\nLinked List data baru setelah disisipkan : ");
list.printLinkedList();
System.out.println("\nmenghapus data ");
System.out.print("Posisi nomor yang akan dihapus : ");
n = kure.nextInt();
list.remove(n);
System.out.println("\nLinked List setelah data dihapus : ");
list.printLinkedList();
System.out.println("\nMenemukan da");
System.out.print("data yang ingin ditemukan : ");
int datax = kure.nextInt();
list.find(datax);
}

}




Output Program
Link List



Insert




Remove/hapus


Find/cari





Coba di praktekin gan!!!


You Might Also Like

0 komentar: