Á´±íÊÇÒ»ÖÖÖØÒªµÄÊý¾Ý½á¹¹£¬ÔÚ³ÌÐòÉè¼ÆÖÐÕ¼ÓкÜÖØÒªµÄµØλ¡£CÓïÑÔºÍC++ÓïÑÔÖÐÊÇÓÃÖ¸ÕëÀ´ÊµÏÖÁ´±í½á¹¹µÄ£¬ÓÉÓÚJAVAÓïÑÔ²»ÌṩָÕ룬ËùÒÔÓÐÈËÈÏΪÔÚJAVAÓïÑÔÖв»ÄÜʵÏÖÁ´±í£¬Æäʵ²»È»£¬JAVAÓïÑÔ±ÈCºÍC++¸üÈÝÒ×ʵÏÖÁ´±í½á¹¹¡£JAVAÓïÑÔÖеĶÔÏóÒýÓÃʵ¼ÊÉÏÊÇÒ»¸öÖ¸Õ루±¾ÎÄÖеÄÖ¸Õë¾ùΪ¸ÅÄîÉϵÄÒâÒ壬¶ø·ÇÓïÑÔÌṩµÄÊý¾ÝÀàÐÍ£©£¬ËùÒÔÎÒÃÇ¿ÉÒÔ±àдÕâÑùµÄÀàÀ´ÊµÏÖÁ´±íÖеĽáµã¡£
class Node
{
Object data;
Node next; // Ö¸ÏòÏÂÒ»¸ö½áµã
}
¡¡¡¡½«Êý¾ÝÓò¶¨Òå³ÉObjectÀàÊÇÒòΪObjectÀàÊǹãÒ峬ÀࣨËùÓÐÀàµÄ×æÏÈ£©£¬ÈκÎÀà¶ÔÏ󶼿ÉÒÔ¸øÆ丳ֵ£¬Ôö¼ÓÁË´úÂëµÄͨÓÃÐÔ¡£ÎªÁËʹÁ´±í¿ÉÒÔ±»·ÃÎÊ»¹ÐèÒª¶¨ÒåÒ»¸ö±íÍ·£¬±íÍ·±ØÐë°üº¬Ö¸ÏòµÚÒ»¸ö½áµãµÄÖ¸ÕëºÍÖ¸Ïòµ±Ç°½áµãµÄÖ¸Õ롣ΪÁ˱ãÓÚÔÚÁ´±íβ²¿Ôö¼Ó½áµã£¬»¹¿ÉÒÔÔö¼ÓÒ»Ö¸ÏòÁ´±íβ²¿µÄÖ¸Õ룬ÁíÍ⻹¿ÉÒÔÓÃÒ»¸öÓòÀ´±íʾÁ´±íµÄ´óС£¬µ±µ÷ÓÃÕßÏëµÃµ½Á´±íµÄ´óСʱ£¬²»±Ø±éÀúÕû¸öÁ´±í£¬ÏÂͼÊÇÕâÖÖÁ´±íµÄʾÒâͼ¡£
¡¡
ͼһ Á´±íµÄÊý¾Ý½á¹¹
¡¡
ÎÒÃÇ¿ÉÒÔÓÃÀàListÀ´ÊµÏÖÁ´±í½á¹¹£¬ÓñäÁ¿Head¡¢Tail¡¢Length¡¢PointerÀ´ÊµÏÖ±íÍ·¡£´æ´¢µ±Ç°½áµãµÄÖ¸ÕëʱÓÐÒ»¶¨µÄ¼¼ÇÉ£¬Pointer²¢·Ç´æ´¢Ö¸Ïòµ±Ç°½áµãµÄÖ¸Õ룬¶øÊÇ´æ´¢Ö¸ÏòËüµÄÇ°Ç÷½áµãµÄÖ¸Õë,µ±ÆäֵΪnullʱ±íʾµ±Ç°½áµãÊǵÚÒ»¸ö½áµã¡£ÄÇôÎÒÃÇΪʲôҪÕâÑù×öÄØ£¿ÕâÊÇÒòΪµ±ÎÒÃÇɾ³ýµ±Ç°½áµãºóÈÔÐ豣֤ʣϵĽáµã¹¹³ÉÁ´±í£¬Èç¹ûPointerÖ¸Ïòµ±Ç°½áµã£¬Ôò»á¸ø²Ù×÷´øÀ´ºÜ´óÀ§ÄÑ¡£ÄÇôÈçºÎµÃµ½µ±Ç°½áµãÄØ£¬ÎÒÃǶ¨ÒåÁËÒ»¸ö·½·¨cursor(),·µ»ØÖµÊÇÖ¸Ïòµ±Ç°½áµãµÄÖ¸Õë¡£ÀàList»¹¶¨ÒåÁËһЩ·½·¨À´ÊµÏÖ¶ÔÁ´±íµÄ»ù±¾²Ù×÷£¬Í¨¹ýÔËÓÃÕâЩ»ù±¾²Ù×÷ÎÒÃÇ¿ÉÒÔ¶ÔÁ´±í½øÐи÷ÖÖ²Ù×÷¡£ÀýÈçreset()·½·¨Ê¹µÚÒ»¸ö½áµã³ÉΪµ±Ç°½áµã¡£insert( Object d )·½·¨ÔÚµ±Ç°½áµãÇ°²åÈëÒ»¸ö½áµã£¬²¢Ê¹Æä³ÉΪµ±Ç°½áµã¡£remove()·½·¨É¾³ýµ±Ç°½áµãͬʱ·µ»ØÆäÄÚÈÝ£¬²¢Ê¹Æäºó¼Ì½áµã³ÉΪµ±Ç°½áµã£¬Èç¹ûɾ³ýµÄÊÇ×îºóÒ»¸ö½áµã£¬ÔòµÚÒ»¸ö½áµã±äΪµ±Ç°½áµã¡£
Á´±íÀàListµÄÔ´´úÂëÈçÏ£º
import java.io.*;
public class List
{
/* ÓñäÁ¿À´ÊµÏÖ±íÍ· */
private Node Head=null;
private Node Tail=null;
private Node Pointer=null;
private int Length = 0;
public void deleteAll()
¡¡
/* Çå¿ÕÕû¸öÁ´±í */
{
Head = null;
Tail = null;
Pointer = null;
Length = 0;
}
public void reset()
¡¡
/* Á´±í¸´Î»£¬Ê¹µÚÒ»¸ö½áµã³ÉΪµ±Ç°½áµã */
{
Pointer = null;
}
public boolean isEmpty( )
¡¡
/* ÅжÏÁ´±íÊÇ·ñΪ¿Õ */
{
return( Length == 0 );
}
public boolean isEnd()
¡¡
/* Åжϵ±Ç°½áµãÊÇ·ñΪ×îºóÒ»¸ö½áµã */
{
if ( Length == 0 )
throw new java.lang.NullPointerException();
else if ( Length == 1 )
return true;
else
return( cursor() == Tail );
}
public Object nextNode()
/* ·µ»Øµ±Ç°½áµãµÄÏÂÒ»¸ö½áµãµÄÖµ£¬²¢Ê¹Æä³ÉΪµ±Ç°½áµã */
{
if ( Length == 1 )
throw new java.util.NoSuchElementException();
else if ( Length == 0 )
throw new java.lang.NullPointerException();
else
{
Node temp = cursor();
Pointer = temp;
if ( temp != Tail )
return( temp.next.data );
else
throw new java.util.NoSuchElementException();
}
}
public Object currentNode()
/* ·µ»Øµ±Ç°½áµãµÄÖµ */
{
Node temp = cursor();
return temp.data;
}
¡¡
public void insert( Object d )
/* ÔÚµ±Ç°½áµãÇ°²åÈëÒ»¸ö½áµã£¬²¢Ê¹Æä³ÉΪµ±Ç°½áµã */
{
Node e = new Node( d );
if ( Length == 0 )
{
Tail = e;
Head = e;
}
else
{
Node temp = cursor();
e.next = temp;
if ( Pointer == null )
Head = e;
else
Pointer.next = e;
}
Length++;
}
public int size()
/* ·µ»ØÁ´±íµÄ´óС */
{
return ( Length );
}
Ííµã ÓÚ 2006-10-06 01:33:44·¢±í:
public Object remove()
/* ½«µ±Ç°½áµãÒƳöÁ´±í£¬ÏÂÒ»¸ö½áµã³ÉΪµ±Ç°½áµã£¬ Èç¹ûÒƳö
µÄ½áµãÊÇ×îºóÒ»¸ö½áµã£¬ÔòµÚÒ»¸ö½áµã³ÉΪµ±Ç°½áµã */
{
Object temp ;
if ( Length == 0 )
throw new java.util.NoSuchElementException();
else if ( Length == 1 )
{
temp = Head.data;
deleteAll();
}
else
{
Node cur = cursor();
temp = cur.data;
if ( cur == Head )
Head = cur.next;
else if ( cur == Tail )
{
Pointer.next = null;
Tail = Pointer;
reset();
}
else
Pointer.next = cur.next;
Length--;
}
return temp;
}
private Node cursor()
/* ·µ»Øµ±Ç°½áµãµÄÖ¸Õë */
{
if ( Head == null )
throw new java.lang.NullPointerException();
else if ( Pointer == null )
return Head;
else
return Pointer.next;
}
¡¡
public static void main( String[] args )
/* Á´±íµÄ¼òµ¥Ó¦ÓþÙÀý */
{
List a = new List();
for ( int i = 1; i <= 10; i++ )
a.insert( new Integer( i ) );
System.out.println( a.currentNode() );
while ( !a.isEnd() )
System.out.println( a.nextNode() );
a.reset();
while ( !a.isEnd() )
{
a.remove();
}
a.remove();
a.reset();
if ( a.isEmpty() )
System.out.println("There is no Node in List \n");
System.in.println( " You can press return to quit\n" );
try
{
System.in.read(); // È·±£Óû§¿´Çå³ÌÐòÔËÐнá¹û
}
catch( IOException e )
{}
}
}
class Node
/* ¹¹³ÉÁ´±íµÄ½áµã¶¨Òå */
{
Object data;
Node next;
Node( Object d )
{
data = d;
next = null;
}
}
¶ÁÕß»¹¿ÉÒÔ¸ù¾Ýʵ¼ÊÐèÒª¶¨Òåеķ½·¨À´¶ÔÁ´±í½øÐвÙ×÷¡£Ë«ÏòÁ´±í¿ÉÒÔÓÃÀàËƵķ½·¨ÊµÏÖÖ»ÊǽáµãµÄÀàÔö¼ÓÁËÒ»¸öÖ¸ÏòÇ°Ç÷½áµãµÄÖ¸Õë¡£
ÎÒÃÇ¿ÉÒÔÓÃÕâÑùµÄ´úÂëÀ´ÊµÏÖ£º
class Node
{
Object data;
Node next;
Node previous;
¡¡
Node ( Object d )
{
data = d;
next = null;
previous = null;
}
}
µ±È»Ë«ÏòÁ´±í»ù±¾²Ù×÷µÄʵÏÖÂÔÓв»Í¬£¬ÕâÀï¾Í²»ÔÙÏêÊöÁË¡£Á´±íºÍË«ÏòÁ´±íµÄʵÏÖ·½·¨£¬Ò²¿ÉÒÔÓÃÔÚ¶ÑÕ»ºÍ¶ÓÁеÄʵÏÖÖУ¬ÕâÀï¾Í²»ÔÙ¶àдÁË£¬ÓÐÐËȤµÄ¶ÁÕß¿ÉÒÔ½«ListÀàµÄ´úÂëÉԼӸĶ¯¼´¿É¡£