# data[].attributes.body
import java.util.Scanner;

class NODE{
public int data;
public NODE link;

//constructor
public NODE(int d, NODE L){
this.data =d;
this.link = L; }

}

class LinkedList{
public NODE headptr;

// public LinkedList(){
// headptr = null;
// }

public static void main(String args[]){
LinkedList nod = new LinkedList();

nod.insertdata(19);
nod.insertdata(2);
nod.insertdata(1);
nod.insertdata(10);
nod.display();
nod.delete();
nod.display();
nod.removeall();
nod.display();
}

public void insertdata(int nom){

NODE currptr, prevptr;

NODE newptr = new NODE(nom, null);
// newptr.data = nom;
//newptr.link = null;

if (headptr == null)
headptr = newptr;

else{

prevptr= null;
currptr=headptr;

while((currptr!=null) && (newptr.data > currptr.data)){
prevptr= currptr;
currptr=currptr.link;

}

if(prevptr==null){// selit dihadapan

newptr.link = headptr;
headptr=newptr;

}

else{ // selit tengah atau belakang

newptr.link = currptr;
prevptr.link = newptr;
}

}

}

public void display(){

NODE currptr;

currptr = headptr;

if (headptr == null)
System.out.println("Senarai kosong");

else{
while (currptr!=null){

System.out.print(currptr.data+ "--->");
currptr = currptr.link;
}

System.out.println("null");

}
}

public void delete(){

Scanner scan = new Scanner(System.in);
int value;

NODE temptr, currptr, prevptr;

if(headptr== null)
System.out.println("Maaf Senarai kosong");
else {
System.out.println("data yang hendak dihapuskan :");
value = scan.nextInt();

currptr= headptr;
prevptr = null;

while ((currptr!=null) && (value!= currptr.data)){
prevptr = currptr;
currptr= currptr.link;
}

if (prevptr== null){// hapus data dihadapan
temptr = currptr;
headptr = currptr.link;
}

else if (currptr!= null){
temptr = currptr;
prevptr.link = currptr.link;
temptr = null;

} else {

System.out.println("data tidak dijumpai"); }

}
}

public void removeall(){

NODE temptr;

temptr = headptr;

if (headptr == null)
System.out.println("Senarai kosong");

else{
while (headptr!=null){

headptr = headptr.link;
temptr = null;
}

}

}

}