You are on page 1of 2

package lat;

public class pNode {


private int data;
private pNode next;

public pNode(){
data = 0;
next = null;
}

public pNode(int x, pNode p){


data = x;
next = p;
}

public void setNext(pNode n){


next = n;
}

public void setData (int x){


data = x;
}

public pNode getNext(){


return next;
}

public int getData (){


return data;
}

###################################################################################
#########################
package lat;

import javax.swing.JOptionPane;
public class SLLint {
private pNode Head;

public SLLint(){
Head = null;
}

public boolean isEmpty(){


return (Head == null);
}

public void insertFirst(pNode p){


p.setNext(Head);
Head= p;
}

public void cetakSLL(){


pNode i;
i = Head;
while(i!=null){
System.out.print(i.getData()+" ");
i = i.getNext();
}
System.out.println();
}

public void insertLast(pNode p){


if(isEmpty())
insertFirst(p);
else {
pNode i = Head;
while(i.getNext()!=null)
i.setNext(p);
p.setNext(null);
}
}

public pNode removeFirst(){


if(isEmpty()){
JOptionPane.showMessageDialog(null, "Linked list kosong", "Error",
JOptionPane.ERROR_MESSAGE);
return null;
}
else{
pNode tmp = Head;
Head = Head.getNext();
return tmp;
}
}
}