我的主类位于最底部,我正在尝试将节点添加到列表中并像主类中的 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);
}
}