gpt4 book ai didi

java - 双向链表 - 删除和添加

转载 作者:行者123 更新时间:2023-11-30 04:25:27 25 4
gpt4 key购买 nike

这是一个类实验室,在其中我尝试在双向链表中添加、删除等。我拥有我认为正确的东西。如果对象不是列表的开头或结尾,我在弄清楚如何删除该对象时遇到问题。我在添加方法时遇到类似的问题。非常感谢任何关于从这里开始的建议以及对我当前代码的评论。

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);}
}

最佳答案

我不知道你的 DLLNode 类是什么样子,但它可能看起来像

class DLLNode {
DLLNode next;
DLLNode prev;
}

要删除节点,逻辑是

next.prev = prev;
prev.next = next;

要在节点后面添加节点newNode,逻辑是

newNode.prev = this;
newNode.next = next;
next = newNode;

我还没有进行任何空指针检查,这取决于你。

关于java - 双向链表 - 删除和添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15983511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com