linked list, bubble sort, stack implement to java

package fpstrdt; import java.util.Scanner; /** * * @author Moch Nor Kholis */ public class persewaan { node head, tail; private int size=0; Scanner in = new Scanner(System.in); void push(){ node temp = new node(); temp.input(); if(head==null){ head=temp; tail=temp; } else{ tail.next=temp; tail=temp; } size++; } void sort(){ if(size>=1){ boolean telahdiubah; do{ node curr=head; node prev=null; node nextto = head.next; telahdiubah = false; while(nextto !=null){ if(curr.lama>nextto.lama){ telahdiubah=true; if(prev != null){ node sig = nextto.next; prev.next=curr; curr.next=sig; } else{ node sig=nextto.next; head=nextto; nextto.next=curr; curr.next=sig; } prev=nextto; nextto=curr.next; } else{ prev=curr; curr=nextto; nextto=nextto.next; } } }while(telahdiubah); } } void view(){ if(head==null){ System.out.println("Data kosong. Tidak ada data untuk ditampilkan"); } else{ node ptr=head; while(ptr!=null){ ptr.view(); System.out.println(); ptr=ptr.next; } } } void edit(String nama){ if(head==null){ System.out.println("Data belum ada"); } node ptr =head; while(!ptr.nama.equals(nama)){ if(ptr==null){ System.out.println("Data yang anda cari untuk diedit tidak ada."); } ptr=ptr.next; } ptr.input(); } void pop(){ if(head==null){ System.out.println("Data kosong. Tidak ada data untuk di pop"); } else if(head==tail){ head=null; } else{ node ptr=head; node prevPtr=head; while(ptr!=tail){ prevPtr=ptr; ptr=ptr.next; } prevPtr.next=null; tail=prevPtr; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); boolean isRun=true; persewaan p = new persewaan(); while(isRun){ System.out.println("==============Rental Mobil Maju-Maju=================="); System.out.println("1.push/input\n2.view\n3.sort\n4.Edit\n5.pop/delete\n6.exit"); int menu = Integer.parseInt(in.nextLine()); switch(menu){ case 1: p.push(); break; case 2: p.view(); break; case 3: p.sort(); break; case 4: System.out.println("Masukkan nama penyewa yang datanya ingin diedit :"); String nama=in.nextLine(); p.edit(nama); break; case 5: p.pop(); break; case 6: isRun=false; System.out.println("Bye"); break; default: System.out.println("Menu yang anda input belum tersedia."); break; } } } } class node{ node next; String nama, jmob, alamat; int lama, harga, tot; Scanner in = new Scanner(System.in); void input(){ System.out.println("Masukkan nama :"); nama = in.nextLine(); System.out.println("Masukkan jenis mobil yang ingin disewa :"); jmob = in.nextLine(); System.out.println("Masukkan alamat :"); alamat = in.nextLine(); System.out.println("Masukkan lama sewa (jam) :"); lama = Integer.parseInt(in.nextLine()); System.out.println("Masukkan harga sewa per jam :"); harga = Integer.parseInt(in.nextLine()); tot=harga*lama; } void view(){ System.out.println("Nama : "+nama); System.out.println("Jenis mobi yang disewa : "+jmob); System.out.println("Alamat penyewa : "+alamat); System.out.println("Lama sewa (jam) : "+lama); System.out.println("Harga sewa per jam : "+harga); System.out.println("Total yang harus dibayar : "+tot); } }
ini pakai buble sort

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.