我的主要课程位于最底层,我正在尝试将节点添加到列表中并将其显示为主类中的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);
}
}
答案 0 :(得分:0)
首先,你的print在Node上调用toString,它只打印Node的地址,而不是你想要的。您需要有一个toString方法,它将返回节点中的值,以便正确显示列表。
其次,您的设计可能由教师指定,但指定按位置添加到列表是愚蠢的。至少,提供addLast和addFirst方法可以更容易地做你明显想做的事情,这就是建立一个列表。
在这种情况下,我假设你想要addLast,因为你想按顺序构建列表。 这将是非常低效的,因为您必须每次扫描整个列表以在结尾处存放新元素,但可能是分配。
如果可以,请先清理您的API。编写addLast,将元素添加到结尾。如果仍有问题,请重新发布代码。你真的不应该跟踪位置来为最后添加一个值。