LinkedList

Posted by Bl4ckB0y On 04.37 0 comments

LinkedList merupakan sebuah struktur data yang mirip dengan Array akan tetapi Linkedlist memiliki perbedaan. Array berupa sebuah larik yang statis, yang mana jika kita memesan 10 lokasi memori kita akan diberi 10 lokasi memori, namun pada kenyataanya kadang-kadang kita membutuhkan lebih dari 10 lokasi memori, sehingga kita harus mengubah source code untuk menambah jumlah memori pada pesanan array source code kita...berbeda dengan linkedlist, walaupun tidak berurutan, linkedlist memilki kelebihan sifatnya yang dinamis membuat kita bebas dalam mengalokasikan memori, sebagai contoh saat kita bisa memesan 10 memori atau 1 memori saja tanpa merubah source code kita. kelebihan array dibanding linkedllist, array memilki data yang terurut sehingga mudah diakses, dan pengaturanya, namun memilki kekurangan pada jumlah memoriya yang statis, sedangkan linkedlist sulit dalam pengaksesanya namun fleksible dalam mengalokasikan memori....

berikut ini adalh contoh linkedlist dalam pemrograman java....

/*
This program Implement about Linked List
Include program
RemoveFirst RemoveLast AddFirst Addlast
InsertAfter InsertBefore Display Find
Author : huda
e-mail : huda890@gmail.com
*/
/*this class implement Node from a linked list*/
public class Node {
int Volume;
Node next;
public Node()
{
Volume=0;
Node next=null;
}
public void displayNode() {
System.out.println(Volume);
}
}
/*this class handle of clas Node*/
import java.io.*;
public class LinkedList {
public Node head;
private Node tail;
private Node cursor;
private Node Help;
/*this methode used to display volume of Node*/
public void Display()
{
cursor=head;
while(cursor!=null)
{
System.out.println("("+cursor.Volume+")");
cursor=cursor.next;
}
}
/*this is constructor*/
public LinkedList() {
head=null;
tail=null;
cursor=null;
Help=null;
}
/*this methode used for check the LinkedList Empty or Not*/
public boolean Empty()
{
if(head==null)
return(true);
return(false);
}
/*this methode used for find the volume of Node*/
public int find(int object)
{
int flag=1;
cursor=head;
while(cursor.Volume!=object && cursor.next!=null)
{
cursor=cursor.next;
flag++;
}
if(cursor.Volume!=object && cursor.next==null)
{
System.out.println("Not Found !!");
return(1);
}
else
{
System.out.println("Found In Index "+flag);
flag=-1;
}
return(flag);
}
/*this methode used for Add Node to the last Node*/
public Node AddLast(int object)
{
Node New_Node=new Node();
New_Node.Volume=object;
if(head==null)
{
head=New_Node;
tail=New_Node;
}
else
{
tail.next=New_Node;
tail=New_Node;
}
return(head);
}
/*this methode used to Add to first element of Linked List*/
public Node AddFirst(int object)
{
Node New_Node=new Node();
New_Node.Volume=object;
if(head==null)
{
head=New_Node;
tail=New_Node;
}
else
{
Help=head;
head=New_Node;
New_Node.next=Help;
}
return(head);
}
/*this methode used to remove first element of Linked List*/
public Node RemoveFirst()
{
if(Empty())
{
System.out.println("Empty!! !!");
return(null);
}
else
{
Help=head;
head=head.next;
Help.next=null;
}
return(head);
}
/*this methode used for remove the Last Element of LinkedList*/
public Node RemoveLast()
{
cursor=head;
if(Empty())
{
System.out.println("Empty!! !!");
return(null);
}
else
{
if(cursor.next==null)
RemoveFirst();
else
{
while(cursor.next.next!=null)
{
cursor=cursor.next;
}
cursor.next=null;
tail=null;
tail=cursor;
}
}
return(head);
}
/*this methode used to Remove the specific Node from the LinkedList*/
public Node RemoveKey(int object)
{
cursor=head;
if(Empty())
{
System.out.println("Empty!!");
return(null);
}
else
{
if(cursor.Volume==object)
RemoveFirst();
else
{
while(cursor.next.Volume!=object)
{
cursor=cursor.next;
}
if(cursor.next.next==null)
RemoveLast();
else
{
Help=cursor.next.next;
cursor.next.next=null;
cursor.next=Help;
}
}
}
return(Help);
}
/*this methode used to Insert a Node between two Node after specific Node*/
public Node InsertAfter(int Sort) throws IOException
{
cursor=head;
if(head==null)
{
System.out.println("Empty!!!");
return(null);
}
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Insert Volume :");
String temporary=input.readLine();
int object=Integer.parseInt(temporary);
int y=find(Sort);
if(y!=-1)
{
System.out.println("Not Found");
}
else
{
while(cursor.Volume!=Sort && cursor.next!=null)
{
cursor=cursor.next;
}
if(cursor.next==null)
{
AddLast(object);
}
else
{
Node New_Node=new Node();
New_Node.Volume=object;
Help=cursor.next;
cursor.next=New_Node;
New_Node.next=Help;
}
}
return(null);
}
/*this methode used to Insert a Node between two Node after specific Node*/
public Node InsertBefore(int Sort) throws IOException
{
if(head==null)
{
System.out.println("Empty!!!");
return(null);
}
int y=find(Sort);
if(y!=-1)
{
System.out.println("Not Found");
return(null);
}
cursor=head;
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Insert Volume :");
String temporary=input.readLine();
int object=Integer.parseInt(temporary);
Node New_Node=new Node();
New_Node.Volume=object;
if(head.Volume = = Sort)
{
AddFirst(object);
}
else
{
while(cursor.next.Volume!=Sort && cursor.next.next!=null)
{
cursor=cursor.next;
}
Help=cursor.next;
cursor.next=New_Node;
New_Node.next=Help;
}
return(null);
}
}
/*this clas the main class to handle second class*/
import java.io.*;
class main
{
public static void main(String[] args) throws IOException
{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
Node node = new Node();
LinkedList list=new LinkedList();
int Answer=1;
while(Answer = =1)
{
System.out.println("Choose the methode :");
System.out.println("1.AddFirst");
System.out.println("2.AddLast");
System.out.println("3.Find");
System.out.println("4.RemoveFirst");
System.out.println("5.RemoveLast");
System.out.println("6.RemoveKey");
System.out.println("7.InsertAfter");
System.out.println("8.InsertBefore");
System.out.println("9.Display");
System.out.println("10.exit");
System.out.print("insert your choose :");
String temporary=input.readLine();
int choosen=Integer.parseInt(temporary);
switch(choosen)
{
case 1 : System.out.print("Insert Number :");
temporary=input.readLine();
choosen=Integer.parseInt(temporary);
list.AddFirst(choosen);break;
case 2 : System.out.print("Insert Number :");
temporary=input.readLine();
choosen=Integer.parseInt(temporary);
list.AddLast(choosen);break;
case 3 : list.Display();
System.out.print("Insert the Volume you want display:");
temporary=input.readLine();
choosen=Integer.parseInt(temporary);
list.find(choosen);break;
case 4 : list.RemoveFirst();break;
case 5 : list.RemoveLast();break;
case 6 : System.out.print("Insert Key :");
temporary=input.readLine();
choosen=Integer.parseInt(temporary);
list.RemoveKey(choosen);break;
case 7 : System.out.print("Insert Setelah :");
temporary=input.readLine();
choosen=Integer.parseInt(temporary);
list.InsertAfter(choosen);break;
case 8 : System.out.print("Insert Number :");
temporary=input.readLine();
choosen=Integer.parseInt(temporary);
list.InsertBefore(choosen);break;
case 9 : list.Display();break;
case 10: Answer=2;break;
default : System.out.println("Astaghfirullah!!!");break;
}
}
}
}

Categories:

0 Response for the "LinkedList"

Posting Komentar

your Ads Here

Your Ad Here FellowEquality.com