public class Link
{
public int data;
public int key;
public Link next;
public void setdata(int data)
{
this.data=data;
}
public int getdata()
{
return this.data;
}
public void setkey(int key)
{
this.key=key;
}
public int getkey()
{
return this.key;
}
public void setnext(Link linknew)
{
this.next=linknew;
}
public Link getnext()
{
return this.next;
}
}
////////////////////////////
public class LinkList
{
public static Link head;
public LinkList()
{
head=null;
}
public void createlink(int[] a)
{
Link Linknew;
Link Linktail;
Linktail=new Link();
head=Linktail;
for(int j=0;j<a.length;j++)
{
Linknew=new Link();
Linknew.setdata(a[j]);
Linknew.setkey(j);
Linktail.setnext(Linknew);
Linknew.setnext(null);
Linktail=Linknew;
}
}
public void insertlink(int i,int data)
{
Link temp;
temp=head.next;
Link Linknew;
while(temp!=null)
{
if(i-1==temp.getkey())
{
Linknew=new Link();
Linknew.setdata(data);
Linknew.setnext(temp.getnext());
temp.setnext(Linknew);
Linknew.setkey(i);
Link a=Linknew;
Link b=Linknew.next;
while(b!=null)
{
b.setkey(i+1);
i++;
a=b;
b=b.next;
}
System.out.println("insert success!");
break;
}
else
{
temp=temp.next ;
}
}
if(temp==null)
{
System.out.println("insert failed!");
}
}
public void deletelink(int data)
{
Link temp=head.next;
Link previoustemp=head;
while(temp!=null)
{
if(data==temp.getdata())
{
int x;
x=temp.getdata();
previoustemp.setnext(temp.getnext());
System.out.println("delete success!");
break;
}
else
{
previoustemp=temp;
temp=temp.getnext();
}
}
if(temp==null)
{
System.out.println("no delete data ");
}
}
public void display()
{
Link p=head.next;
while(p!=null)
{
System.out.print( p.getdata()+" ");
p=p.getnext();
}
System.out.println( "");
}
}
/////////////////////////////////////
public class LinkListAPP
{
public static void main(String[] args)
{
int[] a=new int[10];
for(int i=0;i<10;i++)
{
a[i]=i;
}
LinkList thelist=new LinkList();
thelist.createlink(a);
thelist.display();
thelist.insertlink(2,5);
thelist.display();
thelist.insertlink(3,0);
thelist.display();
thelist.insertlink(7,0);
thelist.display();
thelist.deletelink(9);
thelist.display();
thelist.deletelink(29);
thelist.display();
}
}
2./////////////////////////////////
没有set和get方法的实现
package datestructurelink;
public class Node
{
public int key;
public int data;
public Node next;
public Node()
{
key=0;
data=0;
next=null;
}
public Node(int ikey,int idata,Node next )
{
key=ikey;
data=idata;
next=null;
}
}
////////////////////////////
package datestructurelink;
public class NodeList
{
public Node head;
public NodeList()
{
head=null;
}
public void createlink(int[] a)
{
Node Linknew;
Node Linktail;
Linktail=new Node();
head=Linktail;
for(int j=0;j<a.length;j++)
{
Linknew=new Node();
Linknew.data=a[j];
Linknew.key=j;
Linktail.next=Linknew;
//Linknew.next=null;
Linktail=Linknew;
}
}
public void insertlink(int i,int data)
{
Node temp;
temp=head.next;
Node Linknew;
while(temp!=null)
{
if(i-1==temp.key)
{
Linknew=new Node();
Linknew.data=data;
Linknew.next=temp.next;
temp.next=Linknew;
Linknew.key=i;
Node a=Linknew;
Node b=Linknew.next;
while(b!=null)
{
b.key=i+1;
i++;
a=b;
b=b.next;
}
System.out.println("insert success!");
break;
}
else
{
temp=temp.next ;
}
}
if(temp==null)
{
System.out.println("insert failed!");
}
}
public void deletelink(int data)
{
Node temp=head.next;
Node previoustemp=head;
while(temp!=null)
{
if(data==temp.data)
{
int x;
x=temp.data;
previoustemp.next=temp.next;
System.out.println("delete success!");
break;
}
else
{
previoustemp=temp;
temp=temp.next;
}
}
if(temp==null)
{
System.out.println("no delete data ");
}
}
public void display()
{
Node p=head.next;
while(p!=null)
{
System.out.print( p.data+" ");
p=p.next;
}
System.out.println( "");
}
}
///////////////////////////
package datestructurelink;
public class NodeListAPP
{
public static void main(String[] args)
{
int[] a=new int[10];
for(int i=0;i<10;i++)
{
a[i]=i;
}
NodeList thelist=new NodeList();
thelist.createlink(a);
thelist.display();
thelist.insertlink(2,5);
thelist.display();
thelist.insertlink(3,0);
thelist.display();
thelist.insertlink(7,0);
thelist.display();
thelist.deletelink(9);
thelist.display();
thelist.deletelink(29);
thelist.display();
}
}