我正在尝试理解Linked List数据结构是如何工作的,但在研究它和观看教程后的过去一周,我仍然感到困惑。当节点不是头部或尾部时,我正在尝试删除节点。
我正在使用双向链表
我的关联列表: [A] = [B] = [C] = [D] = [E] = [F]
我正在尝试删除Node B
这是我的删除方法
public GNode deleteNode(E e) {
GNode temp = new GNode(e); // This gets Node: [B]
GNode tempNext = temp.getNext(); // This gets Node: [C]
GNode tempPrevious = temp.getPrevious(); // This get Node: [A]
GNode current = findNode(temp, e); // This finds Node: [B]
// Deletes Head
if (this.head.getData().equals(e)) {
head = head.next;
head.previous = null;
this.size--;
}
// Delete Tail
if (this.tail.getData().equals(e)) {
tail = tail.previous;
tail.next = null;
this.size--;
}
// Delete Node
else {
if(findNode(temp,e) == e){
System.out.println("VALUE: " + temp.next);// Using this to debug. BUT temp.next returns null !?
tempPrevious.setNext(tempNext);
tempNext.setPrevious(tempPrevious);
size--;
}
}
return temp;
}
通过做一些调试,我想我可能没有正确初始化我的变量或循环遍历列表。有人能指出我正确的方向吗?看看我可能做错了什么?
这是我正在使用的整个代码
import java.io.*;
import java.util.*;
public class GDList<E> implements Cloneable {
private static class GNode<E> {
private E data;
private GNode<E> previous;
private GNode<E> next;
public GNode(E e) {
data = e;
previous = null;
next = null;
}
public E getData() {
return data;
}
public GNode getPrevious() {
return previous;
}
public GNode getNext() {
return next;
}
public void setData(E e) {
data = e;
}
public void setPrevious(GNode p) {
previous = p;
}
public void setNext(GNode p) {
next = p;
}
public Iterator<String> iterator() {
// TODO Auto-generated method stub
return null;
}
}
private GNode<E> head;
private GNode<E> tail;
private int size; // number of nodes in a list
public GDList() {
head = null;
tail = null;
size = 0;
}
public int addToHead(E e) {
GNode temp = new GNode(e);
if (head == null) {
head = temp;
tail = temp;
} else {
if (findNode(head, e) == null) {
temp.setNext(head);
head.setPrevious(temp);
head = temp;
} else
return 1;
}
size++;
return 0;
}
public int addToTail(E e) {
GNode temp = new GNode(e);
if (head == null) {
head = temp;
tail = temp;
} else {
if (findNode(head, e) == null) {
temp.setPrevious(tail);
tail.setNext(temp);
tail = temp;
} else
return 1;
}
size++;
return 0;
}
public int addAfter(GNode n, E e) {
if (n == null)
throw new IllegalArgumentException("The node n cannot be null");
if (findNode(head, e) != null)
return 1;
if (n == tail) {
addToTail(e);
} else {
GNode temp = new GNode(e); // element
GNode tempNext = n.getNext(); // location of element
temp.setNext(tempNext); // set element to the next location where
// the position is null
tempNext.setPrevious(temp); //
temp.setPrevious(n);
n.setNext(temp);
size++;
}
return 0;
}
public int addBefore(GNode n, E e) {
if (n == null)
throw new IllegalArgumentException("The node n c6annot be null");
if (findNode(head, e) != null)
return 1;
if (n == head)
addToHead(e);
else {
GNode temp = new GNode(e);
GNode tempPrevious = n.getPrevious();
temp.setNext(n);
n.setPrevious(temp);
tempPrevious.setNext(temp);
temp.setPrevious(tempPrevious);
size++;
}
return 0;
}
@SuppressWarnings("unchecked")
public GNode deleteNode(E e) {
// Linked List [22]=[3]=[17]=[9]
GNode prevNode = this.head;
GNode temp = new GNode(e); // Node: [17]
GNode tempNext = temp.getNext(); // Node: [9]
GNode tempPrevious = temp.getPrevious(); // Node: [3]
GNode current = findNode(temp, e); // Node: [17]
// Deletes Head
if (this.head.getData().equals(e)) {
head = head.next;
head.previous = null;
this.size--;
}
// Delete Tail
if (this.tail.getData().equals(e)) {
tail = tail.previous;
tail.next = null;
this.size--;
}
else {
if(findNode(temp,e) == e){
System.out.println("VALUE: " + temp.next);// Using this to debug. BUT temp.next returns null !?
tempPrevious.setNext(tempNext);
tempNext.setPrevious(tempPrevious);
size--;
}
}
return temp;
}
public GNode deleteAfter(E e) {
GNode temp = findNode(head, e);
if (temp == tail || temp == null)
return null;
return (deleteNode((E) temp.getNext().data));
}
public GNode deleteBefore(E e) {
GNode temp = findNode(head, e);
if (temp == head || temp == null)
return null;
return (deleteNode((E) temp.getPrevious().data));
}
public GNode findNode(GNode p, E e) {
GNode current = p; // current is the cursor
while (current != null && current.data != e) // while is not what I'm
// looking for
current = current.getNext();
return current;
}
public void printList() {
System.out.print("Number of nodes = " + size + ", List is: ");
if (head != null) {
GNode current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.getNext();
}
} else {
System.out.println("The list is empty");
}
System.out.println();
}
public static void main(String[] args) throws Exception {
GDList<String> names = new GDList<String>();
names.printList();
names.addToTail("A");
names.addToTail("B");
names.addToTail("C");
names.addToTail("D");
names.addToTail("E");
names.addToTail("F");
System.out.println();
// Delete Node
System.out.println();
System.out.println("\nDelete Node");
names.printList();
names.deleteNode("B"); // Delete B
names.printList();
}
}
答案 0 :(得分:2)
创建新对象GNode temp = new GNode(e);
时,temp.next和temp.previous设置为null。你可以在这里查看构造函数。
public GNode(E e) {
data = e;
previous = null;
next = null;
}
你已经有一个名为findNode的方法,它会简化很多方法。
public GNode deleteNode(E e) {
GNode current = findNode(head, e); // This finds Node: [B]
// a node with data e doesn't exist
if (current == null) {
return null;
}
// get the next and previous node
GNode previous = current.previous;
GNode next = current.next;
// current node is head
if (previous == null) {
this.head = current.next;
this.head.previous = null;
}
// current node is tail
if (next == null) {
this.tail = current.previous;
this.tail.next = null;
}
if (previous != null || next != null) {
GNode temp = current.previous;
temp.next = current.next;
temp = current.next;
temp.previous = current.previous;
}
return current;
}