Java-从双向链接列表中删除节点

手臂

我试图了解链接列表数据结构的工作原理,但是在研究和观看教程后的过去一周中,我仍然感到困惑。当节点不是头或尾时,我正在尝试删除一个节点。

我正在使用双向链表

我的链接列表:[A] = [B] = [C] = [D] = [E] = [F]

我正在尝试删除节点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();

}
}
Hemanth SA

创建新对象时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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章