运行此代码我错过了什么?关于iNodes

时间:2017-02-20 23:59:54

标签: java nodes

我的班级正在学习节点,我们的教授给了我们一个iNode类定义和这些方法。但是,当我运行代码时,它会给我编译错误。可以在不使用链接列表的情况下构建节点列表吗?因为它没有指定。是否iNodes与节点不同?我为这样的新手道歉,请帮忙。

javac iNode.java iNodeTest.java

这是iNode类定义

public class iNode{ 
public int item;
public iNode next;

public iNode(int i, iNode n){
    item =i;
    next = n;
}

public iNode(int i){
    item =i;
    next = null;
}
}

这些是iNode方法

import java.util.*;
public class iNodeTest{ 

public static void removeLast(iNode x){
    if (x!= null){
        iNode y;
        for(y=x; y.next.next!=null; y=y.next);
        y.next=null;
    }
}

public static void printList(iNode x){
    if (x!= null){
        System.out.print(x.item+" ");
        printList(x.next);
    }
  else
     System.out.println(); 
}

public static int length(iNode x){
    if (x== null)
        return 0;
    return 1 + length(x.next);
}

public static int sum(iNode x){
    if (x== null)
        return 0;
    return x.item + sum(x.next);
}

public static iNode randList(int n, int max){
//Builds and returns a list with n random integers between 0 and max-1
  iNode x=null;
  Random generator = new Random();
  for(int i=0;i<n;i++)
     x= new iNode(generator.nextInt(max),x);
    return x;
}

 public static void main(String[] args) {

 // Code from class exercise
 iNode x=null;
    for (int i = 5;i>0;i--)
        x = new iNode(i,x);
    printList(x);   
    iNode y = x.next.next;
    printList(y);   
    y.next = y.next.next;
    printList(x);   
    printList(y);  
    y.next = null;
    printList(x);  


  iNode h=randList(10,100);

    System.out.println("The length of h is "+length(h));

    System.out.print("The contents of h are ");
    printList(h);
    System.out.println();
    System.out.println("The sum of the items in h is "+sum(h));

    removeLast(h);
    System.out.print("The contents of h are ");
    printList(h);
    System.out.println();
 }
}

编辑:对不起,这就是我运行它的方式。那时我只是做错了什么

   javac iNode.java iNodeTest.java
   iNode.java:3: error: illegal start of expression
    public int item;
    ^
   iNode.java:4: error: illegal start of expression
      public iNode next;
      ^
   iNode.java:6: error: illegal start of expression
      public iNode(int i, iNode n){
      ^
   iNode.java:6: error: '.class' expected
      public iNode(int i, iNode n){
                       ^
   iNode.java:6: error: ';' expected
      public iNode(int i, iNode n){
                        ^
   iNode.java:6: error: ';' expected
      public iNode(int i, iNode n){
                                ^
   iNode.java:11: error: illegal start of expression
      public iNode(int i){
      ^
   iNode.java:11: error: '.class' expected
      public iNode(int i){
                       ^
   iNode.java:11: error: ';' expected
      public iNode(int i){
                         ^
   9 errors

0 个答案:

没有答案