Link List 的节点数据输出问题
我建立了一个链表,每个节点保存了两个实数。
用的是object 方法。
[code]
class ListNode
{
// package access members; List can access these directly
Object Data;
ListNode nextNode;
// constructor creates a ListNode that refers to object
ListNode( Object object )
{
this( object, null );
} // end ListNode one-argument constructor
// constructor creates ListNode that refers to
// Object and to next ListNode
ListNode( Object object, ListNode node )
{
Data = object;
nextNode = node;
} // end ListNode two-argument constructor
[/code]
这两个实数,我用了个class Data
[code]
public class Data
{
private double x;
private double y;
public Data(double x,double y)
{
x=this.x;
y=this.y;
}
[/code]
主程序是这样的
[code]
public class ListTest
{
public static void main( String args[] )
{
List list = new List(); // create the List container
Data x1 =new Data(9,-9);
list.insertAtFront( x1 );
list.print();
Data x2 =new Data(8,-8);
list.insertAtFront( x2 );
list.print();
...
}
[/code]
我的问题是,怎么把链表里的数据输出来。
在传统的链表操作中,通常是保存一个数据,这样非常好输出,只用一个语句就可。如
[code]
public void print()
{
if ( isEmpty() )
{
System.out.printf( "Empty %s\n ", name );
return;
} // end if
System.out.printf( "The %s is: ", name );
ListNode current = firstNode;