如何删除链表中的结点用JAVA语言

解决方案 »

  1.   

    给你一个JAVA语言写的整个链表的添加和删除过程.//Class ListNode definitionclass ListNode{
    // friendly data so class list can access it directly
    Object data;
    ListNode next;

    ListNode(Object o)
    {
    data=o;
    next=null;
    }

    // Constructor: Create a ListNode that refers to Object o and to the
    // next ListNode in the List.
    ListNode(Object o,ListNode nextNode)
    {
    data=o;
    next=nextNode;
    }

    // Return the Object in this node
    Object getObject()
    {
    return data;
    }

    ListNode getnext()
    {
    return next;
    }
    }
    // Class List definition
    class List{
    private ListNode firstNode;
    private ListNode lastNode;
    private String name;         //    String like "list" used in printing

    public List(String s)
    {
    name=s;
    firstNode=lastNode=null;
    }

    //Constructor: Constructor an empty List with "List" as the name
    public List(){
    this("list");
    }

    //Insert an Object at the front of the List If List is empty, firstNode
    //and lastNode refer to same Object. Otherwise,firstNode refers to new node.
    public void insertAtFront(Object insertItem)
    {
    if(isEmpty())//如果链表为空,返回true,否则返回false.
    firstNode=lastNode=new ListNode(insertItem);
    else
    firstNode=new ListNode(insertItem,firstNode);
    }

    //Insert an Object at the end of the List If List is empty, firstNode and
    //lastNode refer to same Object. Otherwise, lastNode's next instance variable refers to new node.
    public void insertAtBack(Object insertItem)
    {
    if(isEmpty())
    firstNode=lastNode=new ListNode(insertItem);
    else
    lastNode=lastNode.next=new ListNode(insertItem);
    }

    // Remove the first node from the List.
    public Object removeFromFront() throws EmptyListException
    {
    Object removeItem=null;
    if(isEmpty())
    throw new EmptyListException(name);

    removeItem=firstNode.data;   //retrieve the data reset the firstNode and lastNode references

    if(firstNode.equals(lastNode))
    firstNode=lastNode=null;
    else
    firstNode=firstNode.next;

    return removeItem;
    }

    //Remove the last node from the List
    public Object removeFromBack() throws EmptyListException
    {
    Object removeItem=null;

    if(isEmpty())
    throw new EmptyListException(name);

    removeItem=lastNode.data; //retrieve the data reset the firstNode and lastNode references
    if(firstNode.equals(lastNode))
    firstNode=lastNode=null;
    else
    {
    ListNode current=firstNode;

    while(current.next !=lastNode){
    current=current.next;
    }

    lastNode=current;
    current.next=null;
    }
    return removeItem;
    }

    //Return true if the List is empty.
    public boolean isEmpty(){
    return firstNode==null;
    }

    //Output the List contents
    public void print()
    {
    if(isEmpty()){
    System.out.println("Empty " + name);
    return;
    }
    System.out.print("The " + name + " is: ");
    ListNode current=firstNode;

    while(current !=null){
    System.out.print(current.data.toString() + " ");
    current=current.next;
    }

    System.out.println();
    System.out.println();
    }
    }
       
       // Class EmptyListException definition
    class EmptyListException extends RuntimeException{
    public EmptyListException(String name)
    {
    super("The " + name + " is empty");
    }
    }//         Class ListTestpublic class ListTest{
    public static void main(String[] args){
    List objList=new List();

    //Create object to store in the List
    Boolean b=new Boolean( true );
    Character c=new Character( '$' );
    Integer i=new Integer( 34567 );
    String s=new String( "hello" );

    //Use the List insert methods
    objList.insertAtFront( b );
    objList.print();
    objList.insertAtFront( c );
    objList.print();
    objList.insertAtBack( i );
    objList.print();
    objList.insertAtBack( s );
    objList.print();

    //Use the List remove methods
    Object removedObj;

    try{
    removedObj=objList.removeFromFront();
    System.out.println(removedObj.toString() + " removed");

    objList.print();
    removedObj=objList.removeFromFront();
    System.out.println(removedObj.toString() + " removed");

    objList.print();
    removedObj=objList.removeFromBack();
    System.out.println(removedObj.toString() + " removed");

    objList.print();
    removedObj=objList.removeFromBack();
    System.out.println(removedObj.toString() + " removed");

    objList.print();
    }
    catch(EmptyListException e){
    System.err.println("\n" + e.toString());
    }
    }
    }