public class OneLinkNode {
public int data;
public OneLinkNode next; public OneLinkNode(int k) {
data = k;
next = null;
} public OneLinkNode() {
this(0); }
public static void main(String args[]) {
int n=5;
OneLinkNode head=create(n);

insert(head,3,30);

OneLinkNode p=head;
for(int i=0;i<n+1;i++)
{
System.out.print(p.data+" ");
p=p.next;
}

System.out.println();
delete(head,4);

p=head;
for(int i=0;i<n;i++)
{
System.out.print(p.data+" ");
p=p.next;
}


} public static OneLinkNode create(int n) { //建立单链表
OneLinkNode rear, q, head = null;
if (n > 0) { int k = (int) (Math.random() * 100);
head = new OneLinkNode(k);
rear = head;
for (int i = 0; i < n-1; i++) {
k = (int) (Math.random() * 100);
q = new OneLinkNode(k);
rear.next = q;
rear = q; }
}
return head;
}
public static boolean insert(OneLinkNode head,int n,int data){//在第n个后插入data
if(head==null)
return false;
OneLinkNode p=head;
for(int i=0;i<n-1;i++)
{
p=p.next;
}
OneLinkNode newNode=new OneLinkNode();
newNode.data=data;
newNode.next=p.next;
p.next=newNode;
return true;
}


public static boolean delete(OneLinkNode head,int n){//把第n个删除
if(head==null)
return false;
OneLinkNode p1=head;
for(int i=0;i<n-2;i++)
{
p1=p1.next;
}
p1.next=p1.next.next;
return true;
}



}