(((急!!!!)))JAVA Link List

有誰能幫我解釋(註解) 以下這段藍色程式的每一行!!!!!!!!

拜託~~我很急....

--------------------------------------------------------------------

class ListNode

{

Object data;

ListNode nextNode;

ListNode( Object object )

{

this( object, null );

}

ListNode( Object object, ListNode node )

{

data = object;

nextNode = node;

}

Object getObject()

{

return data;

}

ListNode getNext()

{

return nextNode;

}

}

public class List

{

private ListNode firstNode;

private ListNode lastNode;

private ListNode Node;

private String name;

public List()

{

this( "list" );

}

public List( String listName )

{

name = listName;

firstNode = lastNode = null;

}

// 從任意位置插入

public boolean insertAtAnywhere( Object insertItem, int index )

{

if ( isEmpty() || index < 0 )

return false;

else if ( index == 1 ) //如果輸入的位置為1

{

// 從第一個插入

insertAtFront( insertItem );

return true;

}

ListNode current = firstNode;

int count = 1;

while(current != null )

{

if(count+1 == index)

{

Node = current.nextNode ;

current.nextNode = new ListNode( insertItem );

current.nextNode.nextNode = Node;

}

current=current.nextNode;

count++;

}

return false;

}

// 從任意位置刪除

public Object removeFromAnywhere( int index ) throws EmptyListException

{

if ( isEmpty() || index < 0 )

return false;

else if ( index == 1 ) //如果輸入要刪除的位置為1

{

// 從第一個刪除

removeFromFront();

return true;

}

Object removedItem = lastNode.data;

ListNode current = firstNode;

int count = 1;

while(current != null )

{

if(count+1 == index)

{

current.nextNode = current.nextNode.nextNode ;

}

current = current.nextNode;

count++;

}

return removedItem;

}

public boolean isEmpty()

{

return firstNode == null;

}

public void print()

{

if ( isEmpty() )

{

System.out.printf( "Empty %s\n", name );

return;

}

System.out.printf( "The %s is: ", name );

ListNode current = firstNode; while ( current != null )

{

System.out.printf( "%s ", current.data );

current = current.nextNode;

}

System.out.println( "\n" );

} }

2 Answers

Rating
  • 9 years ago
    Favorite Answer

    public boolean insertAtAnywhere(Object insertItem, int index) { // if the list is empty or invalid index (<0) // insertion failed and return false if ( isEmpty() || index < 0 ) return false; else if (index == 1) { // index=1, insert at front and return true for insertion success insertAtFront( insertItem ); return true; } // if none of the above conditions are true, traverse forward using a // while loop and insert at indexed position ListNode current = firstNode; // set the current pointer at first node int count = 1; // the while loop ends when the node is null, which should be next // to last node while (current != null ) { if (count+1 == index) { // in the beginnning, the list is // ... -> current -> inNode -> ... // the the next to current node is the index requested, do // 1. use a temp node (inNode) hold the next to current node inNode = current.nextNode ; // 2. set the next node of current one as the inserted node current.nextNode = new ListNode( insertItem ); // 3. set the next to the new-inserted node as the temp node current.nextNode.nextNode = inNode; // in the end, the list is altered as // ... -> current -> insertedNode -> inNode -> .... // since the insertion completes here, a return is missing // return true; } // if required index not reached, move forward to the next index current=current.nextNode; count++; } // the requested index is not reached or not found, insertion failed return false; } // end method insertAtAnywhere public Object removeFromAnywhere(int index) throws EmptyListException { // if the list is empty or invalid index (<0) // removal failed and return false if (isEmpty() || index < 0) // should return null instead of 'false' return false; else if (index == 1) { //如果輸入要刪除的位置為1 // 從第一個刪除 removeFromFront(); // should return the removedItem instead of 'true' return true; }

    字數有限, 餘見意見欄

    2011-05-23 12:14:35 補充:

    // removedItem retrieves data being removed, but should not

    // be the last node. Obviously this is copied from other methods

    Object removedItem = lastNode.data;

    2011-05-23 12:14:52 補充:

    // if none of the above conditions are true, traverse forward using a

    // while loop and remove at indexed position

    ListNode current = firstNode;

    int count = 1;

    // the while loop ends when the node is null, which should be next

    // to last node.

    2011-05-23 12:15:25 補充:

    while(current != null ) {

    if(count+1 == index) {

    // set the next to current node to the second next one

    // before: .. -> current -> next1 -> next2 -> ...

    // after: .. -> current -> next2 -> ... (next1 removed)

    2011-05-23 12:15:33 補充:

    // removeItem should hold the reference first

    // removedItem = current.nextNode;

    current.nextNode = current.nextNode.nextNode ;

    }

    current = current.nextNode;

    count++;

    }

    2011-05-23 12:15:40 補充:

    return removedItem;

    } // end method removeFromAnywhere

  • 9 years ago

    這程式寫得很高深莫測 #_#

Still have questions? Get your answers by asking now.