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){ for(int i=0; i<size; i++){ node crr =head; node crrnext =head.next; for(int j=1; j<size-1; j++){ if(crr.lama > crrnext.lama){ node uu = crr; head=crrnext; head.next=uu; } head=crrnext; head.next=crrnext.next; } } } //node ptr=head; //node prevPtr=head; } 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 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.pop/delete\n5.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: p.pop(); break; case 5: 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.