-2

我的主类位于最底部,我正在尝试将节点添加到列表中并像主类中的 spcfied 一样显示它们

        lList - print linkedlist: 
        lList.size() - print linkedlist size: 1
        lList.size() - print linkedlist size: 1
        lList - print linkedlist: 

我的列表不会显示,谁能指出什么问题

      public interface ListInterface {
       //List operations
        public boolean isEmpty();
        public int size();
        public void add(int index, Object item);
        public void remove(int index);
        public void removeAll();
            }// end interface

    public class Node {
Object item;
Node next;

Node(Object newItem) {
    item = newItem;
    next = null;

}

Node(Object newItem, Node nextNode) {
    item = newItem;
    next = nextNode;
}
public void setItem(Object newItem) {
        item = newItem;
      } // end setItem

public Object getItem() {
        return item;
      } // end getItem

public void setNext(Node nextNode) {
        next = nextNode;
      } // end setNext

public Node getNext() {
        return next;
      } // end getNext
     // end class Node
    }

 public class ListReferencedBased implements ListInterface {

private Node head;
private int numItems;


public ListReferencedBased(){
    numItems = 0;
    head = null;
}


@Override
public boolean isEmpty() {
    // TODO Auto-generated method stub

    return numItems == 0;
}



public int size() {
    // TODO Auto-generated method stub
    return numItems;
}

private Node find(int index){
    Node curr = head;
    for(int skip = 0;skip < index;skip++){
        curr = curr.next;
    }
    return curr;

}

public void add(int index, Object item) {
    // TODO Auto-generated method stub
    if(index == 0 && index < numItems + 1){
        if(index ==0){
            // insert in begning
            Node newNode = new Node(item, head);
            head = newNode;
        }
            else{
                Node prev = find(index - 1);

                Node newNode = new Node(item,prev.next);
                prev.next = newNode;
            }
            numItems++;
        }
    }




@Override
public void remove(int index) {
    // TODO Auto-generated method stub
    if (index == 0){
        head = head.next;
    }
    else{
        Node prev = find(index -1);

        Node curr = prev.next;
        prev.next = curr.next;
    }
    numItems--;
}


@Override
public void removeAll() {
    head = null;
    numItems = 0;



}
public String toString() {
    Node crunchifyCurrent = head.getNext();
    String output = "";
    while (crunchifyCurrent != null) {
        output += "[" + crunchifyCurrent.toString() + "]";
        crunchifyCurrent = crunchifyCurrent.getNext();
    }
    return output;
}
}

public class Nodemain {

     public static void main(String[] args) {
            ListReferencedBased lList = new ListReferencedBased();

            // add elements to LinkedList
            lList.add(0,"1");
            lList.add(1,"2");
            lList.add(2,"3");
            lList.add(3,"4");
            lList.add(4,"5");

            /*
             * Please note that primitive values can not be added into LinkedList
             * directly. They must be converted to their corresponding wrapper
             * class.
             */

            System.out.println("lList - print linkedlist: " + lList);
            System.out.println("lList.size() - print linkedlist size: " + lList.size());
       //   System.out.println("lList.get(3) - get 3rd element: " + lList.get(3));
        //    System.out.println("lList.remove(2) - remove 2nd element: " + lList.remove(2));
    //        System.out.println("lList.get(3) - get 3rd element: " + lList.get(3));
            System.out.println("lList.size() - print linkedlist size: " + lList.size());
            System.out.println("lList - print linkedlist: " + lList);
        }
}
4

1 回答 1

0

首先,您的 print 是在 Node 上调用 toString ,它只会打印 Node 的地址,而不是您想要的。您需要有一个 toString 方法,该方法将返回节点中的值,以便正确显示列表。

其次,您的设计可能是老师指定的,但是指定按位置添加到列表中是愚蠢的。至少,提供 addLast 和 addFirst 方法会让你更容易做你明显想做的事情,那就是构建一个列表。

在这种情况下,我假设您想要 addLast ,因为您想按顺序构建列表。这将是非常低效的,因为您每次都必须扫描整个列表以在最后存放一个新元素,但大概这就是分配。

如果可以的话,首先清理你的 API。编写 addLast 将元素添加到末尾。如果仍有问题,请重新发布代码。您真的不应该跟踪位置以在末尾添加值。

于 2013-09-25T20:30:15.707 回答