import java.util.Scanner;
import java.io.*;
class LinkListApp {
public static void main (String args []) throws Exception{
String nama;
byte berat;
short tinggi;
char lagi;
int pilih;
LinkList List = new LinkList();
Scanner in = new Scanner (System.in);
BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
Node hapus;
System.out.println ("Proses: ");
System.out.println ("1. Tambah depan");
System.out.println ("2. Tambah belakang ");
System.out.println ("3. Hapus depan");
System.out.println ("4. Hapus belakang ");
do {
System.out.print ("Pilih: ");
pilih = in.nextInt();
switch (pilih) {
case 1 : System.out.print ("Masukkan n: ");
int n=in.nextInt();
for(int i=1;i<=n;i++){
System.out.print ("Masukkan nama: ");
nama = input.readLine();
System.out.print ("Masukkan berat: ");
berat = in.nextByte();
System.out.print ("Masukkan tinggi: ");
tinggi = in.nextShort();
List.tambahDepan (nama, berat, tinggi);}
break;
case 2 : System.out.print ("Masukkan n: ");
int m=in.nextInt();
for(int i=1;i<=m;i++){
System.out.print ("Masukkan nama: ");
nama = input.readLine();
System.out.print ("Masukkan berat: ");
berat = in.nextByte();
System.out.print ("Masukkan tinggi: ");
tinggi = in.nextShort();
List.tambahBelakang (nama, berat, tinggi);}
break;
case 3 : hapus = List.hapusDepan();
if (hapus != null)
System.out.print ("Nama: "+hapus.nama +" Berat: "+hapus.berat +" Tinggi"+ hapus.tinggi+"\tterhapus.");
break;
case 4 : hapus = List.hapusBelakang();
if (hapus != null)
System.out.print ("Nama: "+hapus.nama +" Berat: "+hapus.berat +" Tinggi"+ hapus.tinggi+"\tterhapus.");
break;
default :
System.out.print ("Tidak ada");
}
List.displayList();
System.out.print ("\nLagi ? ");
lagi = in.next().charAt(0);
}
while (lagi == 'y' || lagi == 'Y');
}
}
********************************************************************************
class LinkList {
private Node head, tail;
public LinkList() {
head = null;
}
public void tambahDepan (String n, byte b, short t) {
Node baru = new Node (n, b, t);
if (head == null) {
head = baru;
tail = baru;
}
else {
baru.next = head;
head = baru;
}
}
public void tambahBelakang (String n, byte b, short t) {
Node baru = new Node (n, b, t);
if (head == null) {
head = baru;
tail = baru;
}
else {
tail.next = baru;
tail = baru;
}
}
public Node hapusDepan () {
if (head != null) {
Node temp = head;
head = head.next;
temp.next = null;
return temp;
}
else {
System.out.println ("List kosong.");
return null;
}
}
public Node hapusBelakang () {
if (head != null) {
Node bantu, temp;
if (head.next == null) {
temp = head;
head = tail = null;
}
else {
bantu = head;
while (bantu.next != tail) {
bantu = bantu.next;
}
temp = tail;
tail = bantu;
tail.next = null;
}
return temp;
}
else {
System.out.println ("List kosong.");
return null;
}
}
public void displayList () {
if (head != null) {
Node bantu = head;
System.out.println ("Data dalam list: ");
while (bantu != null) {
System.out.println ("Nama: "+ bantu.nama + "\tBerat: " + bantu.berat +"\tTinggi: "+ bantu.tinggi);
bantu = bantu.next;
}
System.out.println ("");
}
else
System.out.println ("List kosong.");
}
}
-----------------------------------------------------------------------------------------------------------
class Node {
public String nama;
public byte berat;
public short tinggi;
public Node next;
public Node ( String n, byte b, short t ) {
nama = n;
berat = b;
tinggi = t;
next = null;
}
}
=======================================================================
Langganan:
Posting Komentar (Atom)
1 komentar:
mirip mahasiswa UIN..... tp mungkin sy salah lihat...hehe
Posting Komentar