3 Contoh Program Linked List Sederhana Pada Java
Contoh Program sederhana Linked List Pada bahasa pemrograman java Java" Di dalam ilmu pengetahuan komputer sering kita mendengar tentang Algoritma dan Struktur data yang secara garis besar membahas terkait dengan struktur sebuah data.
Kemudian didalam Algoritma dan Sruktur data terdapat sebuah metode pengoprasian data yang dikenal dengan sebutan "Linked List".
Lalu apa sih..? Linked list
Linked List merupakan sebuah element data yang bertipe sama, yang memiliki keterurutan tertentu, yang pada setiap elementnnya yang terdiri dari 2 bagian yaitu data dan alamat.
Atau sering di artikan sebagai sebuah tempat penyimpanan data yang terdiri dari node-node atau simpul-simpul yang saling terhubung.
Aturan Linked List:
- Data terhubung dengan data yang lain
- Tata tidak boleh memiliki cabang
Manfaat Linked List :
Linked List memiliki banyak kelebiahan dibandingkan array
- Dapat digunakan untuk menampung banyak data hanya dengan menggunkan pinter
- Pada linked list dapat dilakukan penambah, merubah,dan menghapus data tanpa harus merubah alamat data, sedangkan array tidak.
Permasalahan:
Membuat sebuah program linked list :
#1. Membuat linkedlist statis paling sederhana
Pada contoh ini hanya menggunakan satu class saja yaitu hanya class main. Program berikut sangat sederhana.
Dimana pada contoh ini membuat linkded list dengan memanfaatkan library yang sudah ada di dalam IDE java netbean.
Jadi untuk membuat variabel linkde list, cukup hanya dengan memanggil librarynya saja "import java.util.LinkedList;".
Berikut Scriptnya:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package linked_list; | |
import java.util.LinkedList; | |
public class Linked_list { | |
public static void main(String[] args) { | |
LinkedList<String> linkedList= new LinkedList<>(); | |
linkedList.add("Data 1"); | |
linkedList.add("Data 2"); | |
linkedList.add("Data 3"); | |
linkedList.add("Data 4"); | |
linkedList.add("Data 5"); | |
System.out.println("output :"+linkedList+", Ukuran :" +linkedList.size()); | |
linkedList.addFirst("data 0"); | |
linkedList.addLast("data 6"); | |
System.out.println("output :"+linkedList+", Ukuran :" +linkedList.size()); | |
linkedList.set(0,"data 11"); | |
linkedList.set(6,"data 12"); | |
System.out.println("output :"+linkedList+", Ukuran :" +linkedList.size()); | |
linkedList.removeFirst(); | |
linkedList.removeLast(); | |
System.out.println("output :"+linkedList+", Ukuran :" +linkedList.size()); | |
linkedList.remove(3); | |
System.out.println("output :"+linkedList+", Ukuran :" +linkedList.size()); | |
} | |
} |
Hasil Runing:
#2. Circular Linked list
Pada contoh circular linked list hanya menggunkan satu class saja namun di dalamnya memiliki banyak method.
Berikut Scriptnya:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package circular_linkedlist; | |
public class Circular_linkedlist { | |
static class Node{ | |
int data; | |
Node next; | |
} | |
static Node addtoempty(Node last, int data){ | |
if (last != null) | |
return last; | |
Node temp =new Node(); | |
temp.data=data; | |
last=temp; | |
last.next=last; | |
return last; | |
} | |
static Node addbegin(Node last, int data){ | |
if(last == null) | |
return addtoempty(last,data); | |
Node temp =new Node(); | |
temp.data =data; | |
temp.next=last.next; | |
last.next=temp; | |
return last; | |
} | |
static Node addend(Node last, int data){ | |
if (last== null) | |
return addtoempty(last, data); | |
Node temp =new Node(); | |
temp.data=data; | |
temp.next=last.next; | |
last.next=temp; | |
last=temp; | |
return last; | |
} | |
static Node addafter(Node last, int data, int item){ | |
if (last== null) | |
return null; | |
Node temp, p; | |
p=last.next; | |
do{ | |
if (p.data ==item){ | |
temp=new Node(); | |
temp.data=data; | |
temp.next=p.next; | |
p.next=temp; | |
if (p == last) | |
last=temp; | |
return last; | |
} | |
p=p.next; | |
} | |
while(p !=last.next); | |
System.out.println(item + "tidak ada present dalam list"); | |
return last; | |
} | |
static void traverse(Node last){ | |
Node p; | |
if (last == null){ | |
System.out.println("List is Empty."); | |
return; | |
} | |
p = last.next; | |
do{ | |
System.out.print(p.data + " "); | |
p = p.next; | |
} | |
while (p != last.next); | |
} | |
public static void main(String[] args) { | |
Node last = null; | |
last = addtoempty(last, 6); | |
last = addbegin(last, 4); | |
last = addbegin(last, 3); | |
last = addend(last, 10); | |
last = addend(last, 9); | |
last = addend(last, 50); | |
last = addafter(last, 70, 9); | |
traverse(last); | |
System.out.println(""); | |
} | |
} |
Hasil Runing:
#3. Double Linkded list
Pada program berikut terdapat tiga class yang masing-masing class memiliki method-methode dengan fungsi berbeda-beda.
jika anda telah paham dengan konsep pemrograman berorientasi objek maka anda tidak akan kebingungan jika terdapat banyak class dan methode.
Jika anda belum memahami konsep OOP pada bahasa java maka anda bisa mengunjungi seri belajar oop di blog ini. Baca : Belajar konsep OOP pada java.
Berikut Scriptnya:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package double_linked_list; | |
public class Double_linked_list { | |
public static void main(String[] args) { | |
doublelinkedlist list=new doublelinkedlist(); | |
list.insert_dari_Awal(22); | |
list.insert_dari_Awal(23); | |
list.insert_dari_Awal(27); | |
list.insert_dari_ahir(33); | |
list.insert_dari_ahir(25); | |
list.insert_dari_ahir(28); | |
list.tampil_ahir_awal(); | |
list.tampil_awal_ahir(); | |
list.hapus_data_awal(); | |
list.hapus_data_ahir(); | |
list.hapus_key(33); | |
list.tampil_awal_ahir(); | |
list.inser_data_tengah(22, 88); | |
list.inser_data_tengah(25, 77); | |
list.tampil_ahir_awal(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package double_linked_list; | |
public class doublelinkedlist { | |
Node awal; | |
Node ahir; | |
public doublelinkedlist(){ | |
awal=null; | |
ahir=null; | |
} | |
public boolean periksa_isEmpty(){ | |
return(awal==null) | |
; } | |
public void insert_dari_Awal(int data){ | |
Node node= new Node(data); | |
if(periksa_isEmpty()){ | |
ahir=node; | |
}else{ | |
awal.prev=node; | |
} | |
node.next=awal; | |
awal=node; | |
} | |
public void insert_dari_ahir(int data){ | |
Node node= new Node(data); | |
if(periksa_isEmpty()){ | |
awal=node; | |
}else{ | |
ahir.next=node; | |
node.prev=ahir; | |
} | |
ahir= node; | |
} | |
public Node hapus_data_awal(){ | |
Node temp =awal; | |
if(awal.next == null) | |
ahir=null; | |
else | |
awal.next.prev=null; | |
awal=awal.next; | |
return temp; | |
} | |
public Node hapus_data_ahir(){ | |
Node temp= ahir; | |
if(awal.next==null) | |
awal=null; | |
else | |
ahir.prev.next=null; | |
ahir =ahir.prev; | |
return temp; | |
} | |
public boolean inser_data_tengah(int key, int data){ | |
Node current= awal; | |
while(current.data != key){ | |
current =current.next; | |
if(current==null) | |
return false; | |
} | |
Node node =new Node(data); | |
if(current==ahir){ | |
node.next=null; | |
ahir=node; | |
}else{ | |
node.next=current.next; | |
current.next.prev=node; | |
} | |
node.prev= current; | |
current.next=node; | |
return true; | |
} | |
public Node hapus_key(int key){ | |
Node current =awal; | |
while(current.data != key){ | |
current=current.next; | |
if(current==null) | |
return null; | |
} | |
if (current==awal) | |
awal =current.next; | |
else | |
current.prev.next=current.next; | |
if(current==ahir) | |
ahir=current.next.prev; | |
else | |
current.next.prev=current.prev; | |
return current; | |
} | |
public void tampil_awal_ahir(){ | |
System.out.print("Data (awal-->ahir):"); | |
Node current=awal; | |
while(current != null){ | |
current.tampil(); | |
current=current.next; | |
} | |
System.out.println(""); | |
} | |
public void tampil_ahir_awal(){ | |
System.out.print("Data (ahir-->awal):"); | |
Node current=ahir; | |
while(current != null){ | |
current.tampil(); | |
current=current.prev; | |
} | |
System.out.println(""); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package double_linked_list; | |
public class Node { | |
int data; | |
Node next; | |
Node prev; | |
public Node(int data){ | |
this.data=data; | |
} | |
public void tampil(){ | |
System.out.print("{"+data+"}"); | |
} | |
} |
Hasil Runing:
ccc
#4. Double circular linked list java
Berikut merupakan contoh sederhana dari program double circular linked list pada bahasa java, yang cukup sederhana dan mudah di pahami.
Berikut Scriptnya:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package double_circular_linkedlist; | |
import java.util.Scanner; | |
public class Double_circular_linkedlist { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
/* Creating object of linkedList */ | |
linkedlist list = new linkedlist(); | |
System.out.println("Circular Doubly Linked List Test\n"); | |
char ch; | |
/* Perform list operations */ | |
do | |
{ | |
System.out.println("\nCircular Doubly Linked List Operations\n"); | |
System.out.println("1. insert at begining"); | |
System.out.println("2. insert at end"); | |
System.out.println("3. insert at position"); | |
System.out.println("4. delete at position"); | |
System.out.println("5. check empty"); | |
System.out.println("6. get size"); | |
int choice = scan.nextInt(); | |
switch (choice) | |
{ | |
case 1 : | |
System.out.println("Enter integer element to insert"); | |
list.insertAtstart( scan.nextInt() ); | |
break; | |
case 2 : | |
System.out.println("Enter integer element to insert"); | |
list.insertAtEnd( scan.nextInt() ); | |
break; | |
case 3 : | |
System.out.println("Enter integer element to insert"); | |
int num = scan.nextInt() ; | |
System.out.println("Enter position"); | |
int pos = scan.nextInt() ; | |
if (pos < 1 || pos > list.getsize() ) | |
System.out.println("Invalid position\n"); | |
else | |
list.insertAtPos(num, pos); | |
break; | |
case 4 : | |
System.out.println("Enter position"); | |
int p = scan.nextInt() ; | |
if (p < 1 || p > list.getsize() ) | |
System.out.println("Invalid position\n"); | |
else | |
list.deleteAtPos(p); | |
break; | |
case 5 : | |
System.out.println("Empty status = "+ list.isempty()); | |
break; | |
case 6 : | |
System.out.println("Size = "+ list.getsize() +" \n"); | |
break; | |
default : | |
System.out.println("Wrong Entry \n "); | |
break; | |
} | |
list.display(); | |
System.out.println("\nDo you want to continue (Type y or n) \n"); | |
ch = scan.next().charAt(0); | |
} | |
while (ch == 'Y'|| ch == 'y'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package double_circular_linkedlist; | |
public class linkedlist { | |
protected Node start; | |
protected Node end; | |
public int size; | |
public linkedlist(){ | |
start = null; | |
end = null; | |
size= 0; | |
} | |
public boolean isempty(){ | |
return start == null; | |
} | |
public int getsize(){ | |
return size; | |
} | |
public void insertAtstart(int val){ | |
Node data = new Node (val, null, null); | |
if (start == null){ | |
data.setlinknext(data); | |
data.setlinkprev(data); | |
start =data; | |
end=start; | |
}else{ | |
data.setlinkprev(end); | |
end.setlinknext(data); | |
start.setlinkprev(data); | |
data.setlinknext(start); | |
start =data; | |
} | |
size++; | |
} | |
public void insertAtEnd(int val) | |
{ | |
Node data = new Node(val, null, null); | |
if (start == null) | |
{ | |
data.setlinknext(data); | |
data.setlinkprev(data); | |
start = data; | |
end = start; | |
} | |
else | |
{ | |
data.setlinkprev(end); | |
end.setlinknext(data); | |
start.setlinkprev(data); | |
data.setlinknext(start); | |
end = data; | |
} | |
size++; | |
} | |
public void insertAtPos(int val , int pos) | |
{ | |
Node data = new Node(val, null, null); | |
if (pos == 1) | |
{ | |
insertAtstart(val); | |
return; | |
} | |
Node ptr = start; | |
for (int i = 2; i <= size; i++) | |
{ | |
if (i == pos) | |
{ | |
Node tmp = ptr.getlinknext(); | |
ptr.setlinknext(data); | |
data.setlinkprev(ptr); | |
data.setlinknext(tmp); | |
tmp.setlinkprev(data); | |
} | |
ptr = ptr.getlinknext(); | |
} | |
size++ ; | |
} | |
public void deleteAtPos(int pos) | |
{ | |
if (pos == 1) | |
{ | |
if (size == 1) | |
{ | |
start = null; | |
end = null; | |
size = 0; | |
return; | |
} | |
start = start.getlinknext(); | |
start.setlinkprev(end); | |
end.setlinknext(start); | |
size--; | |
return ; | |
} | |
if (pos == size) | |
{ | |
end = end.getlinkprev(); | |
end.setlinknext(start); | |
start.setlinkprev(end); | |
size-- ; | |
} | |
Node ptr = start.getlinknext(); | |
for (int i = 2; i <= size; i++) | |
{ | |
if (i == pos) | |
{ | |
Node p = ptr.getlinkprev(); | |
Node n = ptr.getlinknext(); | |
p.setlinknext(n); | |
n.setlinkprev(p); | |
size-- ; | |
return; | |
} | |
ptr = ptr.getlinknext(); | |
} | |
} | |
public void display() | |
{ | |
System.out.print("\nCircular Doubly Linked List = "); | |
Node ptr = start; | |
if (size == 0) | |
{ | |
System.out.print("empty\n"); | |
return; | |
} | |
if (start.getlinknext() == start) | |
{ | |
System.out.print(start.getData()+ " <-> "+ptr.getData()+ "\n"); | |
return; | |
} | |
System.out.print(start.getData()+ " <-> "); | |
ptr = start.getlinknext(); | |
while (ptr.getlinknext() != start) | |
{ | |
System.out.print(ptr.getData()+ " <-> "); | |
ptr = ptr.getlinknext(); | |
} | |
System.out.print(ptr.getData()+ " <-> "); | |
ptr = ptr.getlinknext(); | |
System.out.print(ptr.getData()+ "\n"); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package double_circular_linkedlist; | |
public class Node { | |
protected int data; | |
protected Node next,prev; | |
public Node(){ | |
next =null; | |
prev =null; | |
data =0; | |
} | |
public Node(int d, Node n, Node p){ | |
data =d; | |
next =n; | |
prev =p; | |
} | |
public void setlinknext(Node n){ | |
next =n; | |
} | |
public void setlinkprev(Node p){ | |
prev =p; | |
} | |
public Node getlinknext(){ | |
return next; | |
} | |
public Node getlinkprev(){ | |
return prev; | |
} | |
public void setdata(int d){ | |
data =d; | |
} | |
public int getData(){ | |
return data; | |
} | |
} |
Hasil Runing:
Baca juga :
5 Contoh Program java sederna dengan netbeans#2 Belajar PBO Java : Methode dan data