这是一个课堂实验室,我在其中尝试将添加、删除等添加到双向链表中。我拥有我认为正确的东西。如果对象不是列表的开头或结尾,我无法弄清楚如何删除它。我对 add 方法有类似的问题。非常感谢任何关于从这里去哪里的建议以及对我当前代码的评论。
public class Double<T extends Comparable<T>> implements ListInterface<T> {
protected DLLNode<T> front; //Front of list
protected DLLNode<T> rear; //Rear of list
protected DLLNode<T> curPosition; //Current spot for iteration
protected int numElements; //Number of elements in list
public Double() {
front = null;
rear = null;
curPosition = null;
numElements = 0;
}
protected DLLNode<T> find(T target) {
//While the list is not empty and the target is not equal to the current element
//curr will move down the list. If curr becomes null then return null.
//If it finds the element, return it.
DLLNode<T> curr;
curr = front;
T currInfo = curr.getInfo();
while(curr != null && currInfo.compareTo(target) != 0) {
curr = (DLLNode<T>)curr.getLink();
}
if (curr == null) {
return null;
}
else {
return curr;
}
}
public int size() {
//Return number of elements in the list
return numElements;
}
public boolean contains(T element) {
//Does the list contain the given element?
//Return true if so, false otherwise.
if (find(element) == null) {
return false;
}
else {
return true;
}
}
public boolean remove(T element) {
//While the list is not empty, curr will move down. If the element can not
//be found, then return false. Else remove the element.
DLLNode<T> curr;
curr = front;
T currInfo = curr.getInfo();
while(curr != null) {
curr = (DLLNode<T>)curr.getLink();
}
if (find(element) == null) {
return false;
}
else {
if (curr == null) {
curr = curr.getBack();
curr = rear;
}
else if (curr == front) {
curr = (DLLNode<T>)curr.getLink();
curr = front;
}
else if (curr == rear) {
curr = curr.getBack();
curr = rear;
}
else {
}
return true;
}
}
public T get(T element) {
//Return the info of the find method.
if (find(element) == null) {
return null;
}
else {
return find(element).getInfo();
}
}
//public String toString() {
//}
public void reset() {
//Reset the iteration back to front
curPosition = front;
}
public T getNext() {
//Return the info of the next element in the list
DLLNode<T> curr;
curr = front;
//while (curr != null) {
//curr = (DLLNode<T>)curr.getLink();
//}
if ((DLLNode<T>)curr.getLink() == null) {
return null;
}
else {
curr = (DLLNode<T>)curr.getLink();
return curr.getInfo();
}
}
public void resetBack() {
}
public T getPrevious() {
//Return the previous element in the list
DLLNode<T> curr;
curr = front;
if ((DLLNode<T>)curr.getLink() == null) {
return null;
}
else if((DLLNode<T>)curr.getBack() == null) {
return null;
}
else {
curr = curr.getBack();
return curr.getInfo();
}
}
public void add(T element) {
//PreCondition: Assume the element is NOT already in the list
//AND that the list is not full!
DLLNode<T> curr;
DLLNode<T> newNode = (DLLNode<T>)element;
curr = front;
if (curr == null) {
front = (DLLNode<T>)element;
}
else {
}
}
}
这是最终的目标主要功能:
public static void main(String[] args){
Double<String> d = new Double<String>();
d.add("Hello");
d.add("Arthur");
d.add("Goodbye");
d.add("Zoo");
d.add("Computer Science");
d.add("Mathematics");
d.add("Testing");
System.out.println(d);
System.out.println( "Contains -Hello- " + d.contains("Hello"));
System.out.println("Contains -Spring- " + d.contains("Spring"));
System.out.println("size: " + d.size());
d.resetBack();
for (int i = 0; i < d.size(); i++){
String item = (String)d.getPrevious();
System.out.println(item);
} //good stopping point
d.remove("Zoo");
d.remove("Arthur");
d.remove("Testing");
System.out.println("size: " + d.size());
System.out.println(d);
d.remove("Goodbye");
d.remove("Hello");
System.out.println(d);
d.remove ("Computer Science");
d.remove("Mathematics");
System.out.println(d);}
}