Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node{
- int data;
- Node next;
- Node(int a){
- data = a;
- }
- }
- class LinkedListWorking {
- Node kepala;
- int c;
- void create(int b){
- kepala = new Node(b);
- }
- void add(int b){
- Node current = kepala;
- if (kepala == null)
- create(b);
- else{
- while(current.next != null){
- current = current.next;
- }
- current.next = new Node(b);
- }
- }
- void find(int b){
- Node current = kepala;
- int n = 0;
- if (current == null)
- System.out.println(“Data kosong”);
- else{
- while(current.next != null){
- if (current.data == b){
- System.out.println(“Indeks: “+n);
- return;
- }
- n++;
- current = current.next;
- }
- System.out.println(“Tidak ditemukan”);
- }
- }
- void addIndex(int index, int b){
- Node current = kepala;
- Node after = current.next;
- for (int n = 1;n<(index-1);n++){
- current = current.next;
- after = after.next;
- }
- Node newnode = new Node(b);
- current.next = newnode;
- current.next.next = after;
- }
- public void display(){
- Node current = kepala;
- if (current == null)
- System.out.println(“Data kosong”);
- else{
- while(current.next != null){
- System.out.print(current.data+” “);
- current = current.next;
- }System.out.println(current.data+” “);
- }
- }
- public static void main(String[] args) {
- LinkedListWorking a = new LinkedListWorking();
- a.add(2);
- a.add(3);
- a.add(7);
- a.add(5);
- a.add(1);
- a.add(6);
- a.add(9);
- a.display();
- System.out.println(“mencari nilai 3 terletak pada”);
- a.find(3);
- System.out.println(“menyisipkan nilai 9 pada indeks ke-2”);
- a.addIndex(3,9);
- a.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement