Node.java
public class Node {
public int angka;
public Node next;
public Node(int a){
this.angka = a;
}
public void printNode(){
System.out.println(angka);
}
}
LinkApp.java
public class LinkApp {
int menu;
Link L = new Link();
for(int i=1;i<9;i++){
if(i<=6){
int data = Integer.parseInt(JOptionPane.showInputDialog("Masukkan data ke "+i));
L.insertTail(data);
}else{
menu = Integer.parseInt(JOptionPane.showInputDialog("\nApa anda ingin menginput data lagi?"
+ "\n1. Ya"
+ "\n2. Tidak"));
if(menu==1){
int data = Integer.parseInt(JOptionPane.showInputDialog("Masukkan data ke "+i));
L.insert(data,i%6);
}else{
break;
}
}
}
L.Print();
}
}
Link.java
public class Link {
private Node F;
private Node L;
public Link()
{
F = null;
L = null;
}
public boolean EmptyValue()
{
return (F==null);
}
public void insertTail(int a)
{
Node newNode = new Node(a);
if( EmptyValue() )
F = newNode;
else
L.next = newNode;
L = newNode;
}
public void Print()
{
Node pertama = F;
while(pertama != null)
{
pertama.printNode();
pertama = pertama.next;
}
}
public void insert(int satu,int dua)
{
if(dua==1){
F = F.next;
Node newNode = new Node(satu);
newNode.next = F;
F = newNode;
}else if(dua==2){
F.next = F.next.next;
Node newNode = new Node(satu);
newNode.next = F.next;
F.next = newNode;
}
}
}
Tidak ada komentar:
Posting Komentar