免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1664 | 回复: 4
打印 上一主题 下一主题

link list 问题求教! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-02-10 00:14 |只看该作者 |倒序浏览
我现有一link list class,每个结点里含一个数,现在我想每个记录里包含两个实数,怎么修改?



  1. class ListNode
  2. {
  3.    // package access members; List can access these directly
  4.    Object data;   
  5.    ListNode nextNode;

  6.    // constructor creates a ListNode that refers to object
  7.    ListNode( Object object )
  8.    {
  9.       this( object, null );
  10.    } // end ListNode one-argument constructor

  11.    // constructor creates ListNode that refers to
  12.    // Object and to next ListNode
  13.    ListNode( Object object, ListNode node )
  14.    {
  15.       data = object;   
  16.       nextNode = node;  
  17.    } // end ListNode two-argument constructor

  18.    // return reference to data in node
  19.    Object getObject()
  20.    {
  21.       return data; // return Object in this node
  22.    } // end method getObject

  23.    // return reference to next node in list
  24.    ListNode getNext()
  25.    {
  26.       return nextNode; // get next node
  27.    } // end method getNext
  28. } // end class ListNode

  29. // class List definition
  30. public class List
  31. {
  32.    private ListNode firstNode;
  33.    private ListNode lastNode;
  34.    private String name; // string like "list" used in printing

  35.    // constructor creates empty List with "list" as the name
  36.    public List()
  37.    {
  38.       this( "list" );
  39.    } // end List no-argument constructor

  40.    // constructor creates an empty List with a name
  41.    public List( String listName )
  42.    {
  43.       name = listName;
  44.       firstNode = lastNode = null;
  45.    } // end List one-argument constructor

  46.    // insert Object at front of List
  47.    public void insertAtFront( Object insertItem )
  48.    {
  49.       if ( isEmpty() ) // firstNode and lastNode refer to same object
  50.          firstNode = lastNode = new ListNode( insertItem );
  51.       else // firstNode refers to new node
  52.          firstNode = new ListNode( insertItem, firstNode );
  53.    } // end method insertAtFront
  54. ...

  55. public static void main( String args[])
  56.       {
  57.              List list1 = new List();
  58.       
  59.              int array1[] = { 160, 591, 114, 229, 230, 270, 128, 1657, 624, 1503 };
  60.                                      //insert numbers in list
  61.              for (int c1 = 0; c1 < array1.length; c1++ )
  62.                  list1.insertAtFront(array1[ c1 ]);
  63.              }
  64. ...

复制代码

论坛徽章:
0
2 [报告]
发表于 2007-02-10 11:46 |只看该作者
我改了一下,增加了一个class.

  1. public class Data
  2. {
  3.     private double x;
  4.     private double y;
  5.     public Data(double x,double y)
  6.     {
  7.     x=this.x;
  8.     y=this.y;
  9.     }
  10. }
复制代码


然后在主程序中这样调用

  1. public static void main( String args[] )
  2.    {
  3.       List list = new List(); // create the List container
  4.       Data x1 =new Data(9,-9);
  5.       
  6.       list.insertAtFront( x1 );
  7.       list.print();
  8.       Data x2 =new Data(8,-8);
  9.       list.insertAtFront( x2 );
  10.       list.print();
  11.       Data x3 =new Data(7,-7);
  12.       list.insertAtBack( x3 );
  13.       list.print();
  14.       Data x4 =new Data(6,-6);
  15.       list.insertAtBack( x4 );
  16.       list.print();
复制代码

现在的问题是怎么样才能输出结果,原程序的print 是这样的

  1. public void print()
  2.    {
  3.       if ( isEmpty() )
  4.       {
  5.          System.out.printf( "Empty %s\n", name );
  6.          return;
  7.       } // end if

  8.       System.out.printf( "The %s is: ", name );
  9.       ListNode current = firstNode;

  10.       // while not at end of list, output current node's data
  11.       while ( current != null )
  12.       {
  13.          System.out.printf( "%.2f ", current.Data);
  14.          current = current.nextNode;
  15.       } // end while

  16.       System.out.println( "\n" );
  17.    } // end method print
复制代码

当每个节点只存有一个数据时,可以。
但像这样存两个数据,就不行了。原因是

  1. System.out.printf( "%.2f ", current.Data);
复制代码

Data 是个object, 请问怎么样才能将Data 里的数据输出来。

焦急等待中。谢谢

论坛徽章:
0
3 [报告]
发表于 2007-02-10 12:51 |只看该作者
linked lists

论坛徽章:
0
4 [报告]
发表于 2007-02-10 23:53 |只看该作者
没人回答,我到CSDN去了,那里的人气好厉害呀。

论坛徽章:
0
5 [报告]
发表于 2007-02-12 09:46 |只看该作者
public class Data
{
    private double x;
    private double y;
    public Data(double x,double y)
    {
    x=this.x;
    y=this.y;
    }
   
    public getX()
    { return this.x; }

    public getY()
    { return this.y; }
   

}

System.out.printf( "%.2f ", current.Data.getX());
System.out.printf( "%.2f ", current.Data.getY());

这就是为什么要先学点编码基础再学算法的原因

[ 本帖最后由 perryhg 于 2007-2-12 09:48 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP