📄 list.java
字号:
package com.deitel.jhtp5.ch20;
// class to represent one node in a list
class ListNode{
// package access members; List can access these directly
Object data;
int[] information = new int[5];
ListNode nextNode;
// create a LisNode that refrs to object
ListNode( Object object , int[] info )
{
data = object;
nextNode = null;
for( int i = 0; i<=4; i++ )
information[i] = info[i];
}
// create ListNode that refers to Object and to next ListNode
ListNode( Object object, ListNode node, int[] info )
{
data = object;
nextNode = node;
for( int i = 0 ; i<=4 ; i++ )
information[i] = info[i];
}
// return reference to data in node
Object getObject()
{
return data; // retrun object in this node
}
int[] getInfo()
{
return information;
}
// return reference to next node in list
ListNode getNext()
{
return nextNode; // get next node
}
} // end of ListNode
public class List{
private ListNode firstNode;
private ListNode lastNode;
private String name; // string like "list" used in printing
// construct empty List with "list" as the name
public List()
{
this( "list" ) ;
}
// construct an empty List with a name
public List( String listName )
{
name = listName;
firstNode = lastNode = null;
}
// insert Object at front of List
public synchronized void insertAtFront( Object insertItem, int[] info )
{
if ( isEmpty() ) // firstNode and lastNode refer to same object
firstNode = lastNode = new ListNode( insertItem, info );
else // firstNode refers to new node
firstNode = new ListNode( insertItem, firstNode, info );
}
// insert Object at end of List
public synchronized void insertAtBack( Object insertItem, int[] info )
{
if ( isEmpty() ) // firstNode and lastNode refer to same object
firstNode = lastNode = new ListNode( insertItem, info );
else // lastNode's nextNode refers to new node
lastNode = lastNode.nextNode = new ListNode( insertItem, info );
}
//remove first node from List
public synchronized int[] removeFromFront() throws EmptyListException
{
if ( isEmpty() ) // throw exception if List is empty
throw new EmptyListException( name );
int[] removedItem = firstNode.getInfo(); // retrieve data being removed
// update references firstNode and lastNode
if( firstNode == lastNode )
firstNode = lastNode = null;
else
firstNode = firstNode.nextNode;
return removedItem; // retrun removed node data
} // end method removeFromFront
//remove last node from List
public synchronized Object removeFromBack() throws EmptyListException
{
if ( isEmpty() ) // throw exception if List is empty
throw new EmptyListException( name );
Object removedItem = lastNode.data; // retrieve data being removed
// update references firstNode and lastNode
if ( firstNode == lastNode )
firstNode = lastNode = null;
else{ // locate new last node
ListNode current = firstNode;
//loop while current node !-> lastnode
while( current.nextNode != lastNode )
current = current.nextNode;
lastNode = current; // current is new lastNode
current.nextNode = null;
}
return removedItem; // return removed node data
} // end method removeFromBack
// determine whether list is empty
public synchronized boolean isEmpty()
{
return firstNode == null; // return true if list is empty
}
// output List contents
public synchronized void print()
{
if ( isEmpty() ){
System.out.println( "Empty " + name );
return;
}
System.out.print( "The " + name + " is: " );
ListNode current = firstNode;
// while not at end of list, output current node's data
while ( current != null ){
for( int i=0; i<=4; i++ ) System.out.print( current.information[i] + " " );
System.out.println( "\n" );
current = current.nextNode;
}
System.out.println( "\n" );
}
public synchronized int[] getInfo()
{
if ( isEmpty() ){
System.out.println( "Empty " + name );
return null;
}
return lastNode.getInfo();
}
public synchronized Object getName()
{
if ( isEmpty() ){
System.out.println( "Empty " + name );
return null;
}
return firstNode.getObject();
}
} // end class List
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -