P. 1
Program Linked List

Program Linked List

|Views: 1|Likes:
Published by Agus Razta Part II

More info:

Published by: Agus Razta Part II on May 31, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less

12/29/2012

pdf

text

original

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 in put tail = input; } else { input.next = head; //Jika linked list sudah berisi, head = input; //maka input akan di depan dan menjadi h ead } } void addLast(Node input){ if (isEmpty()){ //Jika linked list masih kosong, head = input; //maka head dan tail sama dengan node in put tail = input; } else { tail.next = input; //Jika linked list sudah berisi, tail = input; //maka input akan di belakang dan menjad i tail } } void insertAfter(int key,Node input){ Node temp = head; do { if (temp.data == key){ //Jika data sama dengan key, mak a input input.next = temp.next; //disambung diantara tem p 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){

next head = temp.out.if ((temp.").next = temp.addFirst(input). //memajukan temp ke temp . satu elemen } m tail.next. if (!isEmpty()){ if (tail == head){ st hanya 1 head = tail = null.next.next != tail){ temp = temp. jadi akhir LL tail = temp.data == key)&&(temp == head)){ this. //maka head dan tail men jadi null } //sehingga linked list k osong else { temp = temp.next = null.next. /* jika insert pada awal linked list maka call method addFirs t */ System. jadi null } osong else { while (temp.println("Insert data is succeed. break. } temp = temp.println("Insert data is succeed. -> input --> temp.out.next.dan dipindah ke temp .next = input. break. //temp - //jika element linked li //maka head dan tail men //sehingga linked list k //memajukan temp hingga //sebelu //temp. } } void removeFirst(){ Node temp = head. if (!isEmpty()){ if (head == tail){ //jika element linked li st hanya 1. //kemudian head dipindah ke temp temp = null.next. temp. //kemudian temp di-null (optional) } } else System. //dari temp --> temp. } else if (temp.nex t menjadi temp. head = tail = null.next System.out.println("Data is empty!"). //tail temp = null.").next di-null. } void removeLast(){ Node temp = head.data == key){ input.

temp = temp.data == key){ //mengganti temp .data == key){ found = true. break. if (!isEmpty()){ while (temp != null){ if (temp.next dengan temp.out.out.next = temp.//jika key berada pad a awal linked list.next. next break. //dari temp --> temp.data).next. temp = head. } } else System.data == key)&&(temp == head)){ this. } else System. //temp.next -->tem p.next } //menjadi temp --> temp.next. } i++.out.out.next.println("Data is empty!").out. //maka call method remov eFirst } temp = temp. next.next.} } else System.next else if ((temp. break. while (temp != null){ System.println(temp. boolean found = false.removeFirst(). } if (found){ System. temp = temp. } } } .next. Node temp = head.next.println("Data is empty!"). } void printNode(){ Node temp.println(key+" is found at index "+i).next. } void remove(int key){ Node temp = head.println("Data isn't found"). while (temp != null){ if (temp. } void find (int key){ int i = 0.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->