我正在尝试用 Java 实现我自己的 List 系统。
类List
文件:
package RoutingDemo.List;
/**
* A 2-way Linked-List to store generic elements.
*
*/
public class List {
/*
Instance Variables
---------------------------------------------------------------------------
*/
/**
* Reference to element.
*/
private Object info;
/**
* Reference to previous NodeList instance.
*/
private List prev;
/**
* Reference to next NodeList instance.
*/
private List next;
/*
Constructors
---------------------------------------------------------------------------
*/
/**
* Creates a new empty list.
*/
public List() {
prev = null;
next = null;
info = null;
}
/*
Methods
---------------------------------------------------------------------------
*/
/**
* Adds an element to the list.
*
* @param o Element to be added
*/
public List add(Object o) {
if(info == null) {
info = o;
prev = null;
next = null;
return this;
} else {
List temp = new List();
temp.add(o);
return addList(temp);
}
}
/**
* Appends an existing list to this list.
*
* @param newList List to be appended
*/
public List addList(List newList) {
if(newList.info() == null)
return this;
List ref = this;
ref.last().setNext(newList.first());
newList.first().setPrev(ref.last());
return ref;
}
/**
* Get number of elements in the list.
*
* @return number of elements in list
*/
public int count() {
if(info == null)
return 0;
List ref = this.first();
int count = 0;
while(true) {
count++;
if(!ref.isLast())
ref = ref.next();
else
break;
}
return count;
}
/**
* Deletes an element from the list.
*
* @param o Element to be deleted
* @return List which does NOT
* contain element o
*/
public List delete(Object o) {
if(info == null)
return this;
List ref = this.first();
while(true) {
if(ref.info() == o) {
if(ref.isFirst() && ref.isLast()) {
ref = new List();
break;
} else if(ref.isFirst()) {
ref = ref.next();
ref.killPrev();
break;
} else if(ref.isLast()) {
/* *** THIS IS THE CASE THAT WILL BE CALLED FOR THIS TEST **** */
ref = ref.prev();
ref.killNext();
break;
} else {
ref.prev().setNext(ref.next());
ref.next().setPrev(ref.prev());
ref = ref.prev();
break;
}
} else {
if(!ref.isLast())
ref = ref.next();
else
break;
}
}
return ref;
}
/**
* Moves to first element in List.
*
*
* @return List pointing to first
* element in list
*/
public List first() {
List ref = this;
while(!ref.isFirst()) {
/* *** STUCK HERE *** */
ref = ref.prev();
}
return ref;
}
/**
* Returns current list element.
*
* @return current list element
*/
public Object info() {
return info;
}
/**
* Checks whether list is empty.
*
* @return true, if list is empty
* , false otherwise.
*/
public boolean isEmpty() {
if(count() > 0)
return false;
else
return true;
}
/**
* Checks whether current element is the first element.
*
* @return true, if current element is
* first element, false otherwise.
*/
public boolean isFirst() {
if(prev == null)
return true;
else
return false;
}
/**
* checks whether current element is the last element.
*
* @return true, if current element is
* last element, false otherwise
*/
public boolean isLast() {
if(next == null)
return true;
else
return false;
}
/**
* Cuts the list from next element.
*
*
* @param l new link for current element
*/
public void killNext() {
next = null;
}
/**
* Cuts the list from previous element.
*
*
* @param l new link
*/
public void killPrev() {
prev = null;
}
/**
* Moves to last element in List.
*
*
* @return List pointing to last
* element in list
*/
public List last() {
List ref = this;
while(!ref.isLast()) {
ref = ref.next();
}
return ref;
}
/**
* Moves to next element in List
*
*
* @return List pointing to next
* element in list
*/
public List next() {
if(!isLast())
return next;
else
return this;
}
/**
* Moves to previous element in List
*
*
* @return List pointing to previous
* element in list
*/
public List prev() {
if(!isFirst())
return prev;
else
return this;
}
/**
* Sets the next link
*
*
* @param l new link for current element
*/
public void setNext(List l) {
next = l;
}
/**
* Sets the prev link for current element
*
*
* @param l new link
*/
public void setPrev(List l) {
prev = l;
}
}
我正在像这样测试它:
class Example {
Example() {
List nl = new List();
nl = nl.add(new Node(5,6));
System.out.println("" + nl.count());
Node x = new Node(1,3);
nl = nl.add(x);
System.out.println("" + nl.count());
nl = nl.delete(x);
System.out.println("as" + nl.count());
}
}
public class ListTest {
public static void main(String args[]) {
new Example();
}
}
现在,当我添加前两个节点时,一切都很好。但是,当我在删除节点count()
后调用时,我进入了一个无限循环。
在经历了很多断点之后,我在代码中标记了我卡住的地方。显然delete()
函数中有问题,我无法弄清楚我做错了什么。
目前,我已经用以下代码替换了我的delete()
代码:
public List delete(Object o) {
if(info == null)
return this;
List ref = this.first();
List temp = new List();
while(true) {
if(ref.info() != o)
temp.add(ref.info());
if(!ref.isLast())
ref = ref.next();
else
break;
}
return temp;
}
但这对于巨大的列表来说是不友好的。如果您能发现问题,请告诉我!