0% found this document useful (0 votes)
59 views

Nama: Danu Arif Rahman NRP: 1209100091: Linked List

The document discusses linked lists and includes code for a Link class that defines the nodes of the linked list, an Llist class that implements a linked list, and a MainLlist class that tests the linked list functionality. The Llist class implements common linked list methods like insert, append, remove, traversing to different positions, and retrieving the current element. The MainLlist class runs a sample program that prompts the user for different linked list operations and uses the Llist class to perform them.

Uploaded by

bimahasto
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Nama: Danu Arif Rahman NRP: 1209100091: Linked List

The document discusses linked lists and includes code for a Link class that defines the nodes of the linked list, an Llist class that implements a linked list, and a MainLlist class that tests the linked list functionality. The Llist class implements common linked list methods like insert, append, remove, traversing to different positions, and retrieving the current element. The MainLlist class runs a sample program that prompts the user for different linked list operations and uses the Llist class to perform them.

Uploaded by

bimahasto
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Nama NRP

: Danu Arif Rahman : 1209100091


Linked List

Link.java package LinkList;

/** * * @author Danoen */ public class Link <E> { private E element; private Link<E> next;

public Link(Link<E> next) { this.next = next; }

public Link(E element, Link<E> next) { this.element = element; this.next = next; }

public E getElement() { return element; }

public void setElement(E element) { this.element = element;

public Link<E> getNext() { return next; }

public void setNext(Link<E> next) { this.next = next; } } Llist.java package LinkList;

import listarray.List;

/** * * @author Danoen */ public class Llist<E> implements List<E> { private Link<E> head; private Link<E> tail; protected Link<E> curr; int cnt;

public Llist() { curr=tail=head=new Link<E> (null); cnt=0; }

public void clear() { //head.setNext(null);

curr=tail=head=new Link<E>(null); cnt=0; }

public void insert(E item) { curr.setNext(new Link<E>(item,curr.getNext())); if (tail==curr)tail=curr.getNext(); cnt++; }

public void append(E item) { tail.setNext(new Link<E>(item,null)); tail=tail.getNext(); cnt++; }

public E remove() { if (curr.getNext()==null)return null; E it =curr.getNext().getElement(); if (tail==curr.getNext())tail=curr; curr.setNext(curr.getNext().getNext()); cnt--; return it; }

public void moveToStart() { curr=head; }

public void moveToEnd() { curr=tail; }

public void prev() { if(curr==head)return; Link<E>temp=head; while(temp.getNext()!=curr) temp=temp.getNext(); curr=temp; }

public void next() { if(curr!=tail){curr=curr.getNext();} }

public int length() { return cnt; }

public int currPos() { Link<E>temp=head; int i; for(i=0;curr!=temp;i++) temp = temp.getNext(); return i; }

public void moveToPos(int pos) { if(pos>=0&&pos<=cnt){ curr = head; for(int i=0;i<pos;i++) curr=curr.getNext(); }else{ System.out.println("posisi di luar hasil");

} }

public E getValue() { if(curr.getNext()==null){ E error=(E)"tidak ada yang diperoleh"; return error; }else{ return curr.getNext().getElement(); } } public void show(){ System.out.println("Link Awal akhir= "); Link<E> lihat = head.getNext(); while(lihat!=null){ System.out.print(lihat.getElement()+""); lihat=lihat.getNext(); } } } MainLlist.java package LinkList;

import java.util.Scanner;

/** * * @author Danoen */ public class mainLlist { /** * @param args the command line arguments

*/ public static void main(String[] args) { int i; Object eval; Scanner scanner = new Scanner(System.in); //i = scanner.nextInt(); Llist list = new Llist (); do{ do{ System.out.println("1. Clear\n" +"2. Insert\n" +"3. Append\n" +"4. Remove\n" +"5. MoveToStart\n" +"6. MoveToEnd\n" +"7. Prev\n" +"8. Next\n" +"9. Length\n" +"10.CurrPos\n" +"11.MoveToPos\n" +"12.GetValue\n" +"13.Show\n" +"14.Keluar\n" +"Pilihan Anda : "); i = scanner.nextInt(); } while ((i<1)||(i>14));

if(i==1) {System.out.println("clear sukses.\nSemua elemen sudah dihapus"); list.clear(); } else if(i==2){

System.out.println("Masukkan elemen yang akan di insert"); eval = scanner.nextInt(); list.insert(eval); } else if(i==3) {System.out.println("masukkan elemen yang akan diappend"); eval=scanner.nextInt(); list.append(eval);

} else if(i==4) {System.out.println("masukkan elemen yang akan diremove"); eval=scanner.nextInt(); list.remove(); } else if(i==5) {System.out.println("masukkan elemen current yang akan dipindah di depan"); eval=scanner.nextInt(); list.moveToStart(); } else if(i==6) {System.out.println("masukkan elemen current yang akan dipindah di belakang"); eval=scanner.nextInt(); list.moveToEnd(); } else if(i==7) {System.out.println("masukkan elemen current yang akan dipindah ke posisi sebelumnya"); eval=scanner.nextInt(); list.prev(); } else if(i==8) {System.out.println("masukkan elemen current yang akan dipindah ke posisi selanjutnya");

eval=scanner.nextInt(); list.next(); } else if(i==9) {System.out.println("panjang list = "+list.length());} else if(i==10) {System.out.println("posisi current ="+list.getValue());} else if(i==11) {System.out.println("masukkan elemen current yang akan dipindah"); System.out.println(""); } else if(i==12) {System.out.println("No current elements"); System.out.println("posisi current ="+list.getValue());} else if(i==13) { list.show(); } else if(i==14){} }while(i!=14);

You might also like