我正在实现一个带有嵌套有序ListNode
类的自定义有序LinkedList
类。 一切正常,但我正在尝试通过访问已删除的元素来扩展它。
这不是必需的,但我很好奇这将如何工作,因为我只能使用我被指示创建的方法,即boolean add()
、boolean remove()
和clear()
。
我还在跟踪每个修改,每次成功添加、删除或调用clear()
都会增加。我可以简单地创建另一个OrderedLinkedList
,并将已删除的元素添加到其中,但我觉得我正在添加不必要的修改计数。
同样,这部分只是为了好玩,不是必需的。我觉得这将使我对创建自定义类有更深入的了解。
我将展示删除和主要方法。无法更改删除方法签名。
public boolean remove(Comparable obj) {
for(OrderedListNode element = head.next; element != tail; element = element.next) {
if(obj.equals(element.dataItem)) { //if element being removed is at the cursor
OrderedListNode previousNode = element.before;
OrderedListNode nextNode = element.next;
nextNode.before = previousNode; //places next element that's after before to the element after current element [prev -> current -> next]
previousNode.next = nextNode; //places prev of next element to the element before current
element.dataItem = (Comparable)NOT_FOUND; //removed element is now null
modCount++; //another modification
theSize--; //reduce the size by 1
return true; //if remove is successful
}
}
return false; //otherwise, not successful removal
}
主要方法:
public static void main(String[] args) {
OrderedLinkedList list = new OrderedLinkedList();
OrderedLinkedList removedList = new OrderedLinkedList();
modCount = 0;
list.add("Dog");
list.add("Bird");
list.add("dog");
list.add("bird");
list.add("Cat");
System.out.println("Before removal of element");
System.out.println(list);
list.remove("Dog");
removedList.add("Dog"); //not what I'm wanting to do
System.out.println("Removed " + removedList);
System.out.println("After removal of element");
System.out.println(list);
System.out.println("Total modifications = " + modCount);
System.out.println();
}
输出:
Before removal of element
Bird, Cat, Dog, bird, dog
Removed Dog //not actually accessing the element originally removed. just printing a new list
After removal of element
Bird, Cat, bird, dog
Total modifications = 7 //unnecessary modification due to additional add
如果您只想存储已删除的元素而不增加修改计数,则可以使用 ArrayList 并将已删除的元素放入其中。这样,您的修改计数就不会受到影响。
可以通过实现其他 pop 方法来存储已删除的值。返回类型应为 Comparable ,当找到要删除的对象时,将其存储在临时对象中并返回该对象,而不是返回布尔值true。如果未找到对象,只需返回 null。
如果找到要删除的 Comparable 对象,该方法将返回该对象,以便您可以存储它。如果没有,将返回 null,以便您可以使用 if-check for pop 方法来获取删除是否成功。
这是我刚刚为您编写的示例方法;
示例弹出方法
public Comparable pop(Comparable obj) {
for (OrderedListNode element = head.next; element != tail; element = element.next) {
Comparable temp = null; // declaration of the temporary object
if (obj.equals(element.dataItem)) { // if element being removed is
// at the cursor
temp = obj; // store obj in temp
OrderedListNode previousNode = element.before;
OrderedListNode nextNode = element.next;
nextNode.before = previousNode; // places next element that's
// after before to the element
// after current element [prev
// -> current -> next]
previousNode.next = nextNode; // places prev of next element to
// the element before current
element.dataItem = (Comparable) NOT_FOUND; // removed element is
// now null
modCount++; // another modification
theSize--; // reduce the size by 1
return temp; // if remove is successful
}
}
return null; // otherwise, not successful removal
}
测试演示
你的测试代码应该是这样的;
public static void main(String[] args) {
OrderedLinkedList list = new OrderedLinkedList();
OrderedLinkedList removedList = new OrderedLinkedList();
modCount = 0;
list.add("Dog");
list.add("Bird");
list.add("dog");
list.add("bird");
list.add("Cat");
System.out.println("Before removal of element");
System.out.println(list);
// list.remove("Dog"); // not needed anymore
// removedList.add("Dog"); //not what I'm wanting to do
// pop returns the removed object
removedList.add(list.pop("Dog"));
System.out.println("Removed " + removedList);
System.out.println("After removal of element");
System.out.println(list);
System.out.println("Total modifications = " + modCount);
System.out.println();
}