public class MyLink {
private Node header ;
private int size ;
public MyLink( )
{
header = null ;
size = 0 ;
}
public MyLink( Node newNode )
{
header = newNode ;
size = 1 ;
}
public int getData( int pos )
{
Node p = header ;
int i=0;
while( i<pos )
{
p = p.next;
i++ ;
}
return p.data ;
}
public int getSize( )
{
return size ;
}
public boolean isEmpty()
{
return (header == null) ;
}
public int find( int value )
{
Node p = header ;
int pos = -1;
int i = 0;
while(p != null)
{
if(p.data == value)
{
pos = i;
break;
}
i++;
p = p.next;
}
return pos;
}
public void insertFirst( int data )
{ Node newNode = new Node( data ) ;
Node p = header ;
header = newNode ;
newNode.next = p ;
size++ ;
}
public void insertLast( int data )
{
Node newNode = new Node(data) ;
Node p = header ;
if(p==null)
{
header = newNode;
size++;
}
else
{
header.next = newNode ;
size++;
}
}
public void insert( int pos , int data )
{
}
public void delete( int pos )
{
Node p = header ;
int i=0;
while(i == pos)
{
p = p.next;
header = null;
i++;
p = p.next;
}
}
public void clear( )
{
header = null ;
size = 0;
}
public void print( )
{
Node p = header ;
if(p!=null)
System.out.print("|") ;
while( p != null )
{
System.out.print( p.data +"|") ;
p = p.next ;
}
System.out.println() ;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
อันนี้เป็นคราส Test
public class Test {
public static void main(String[] args) {
MyLink link1 = new MyLink();
link1.print();
link1.insertLast(1);
link1.insertLast(3);
link1.insertLast(5);
link1.insertLast(7);
link1.print();
System.out.println(link1.find(5));
System.out.println(link1.getData(1));
System.out.println(link1.getSize());
link1.delete(1);
link1.print();
}
}