Java经典算法大全 下载本文

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); }

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;

41

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(\ System.in.println(\ try

{

System.in.read();

//确保用户看清程序运行结果 }

catch(IOException e) {} } }

class Node

42

{ Object data; Node next;

Node(Object d)

{

data=d; next=null; } }

43