How do you delete a node in a doubly linked list in Java?

I'm trying to understand how Linked List data structures works, but in the past week after researching it and watching tutorials, I'm still confused. I'm trying to delete a node, when a node is not the head or the tail.

I'm working with a doubly linked list

My Linked List: [A]=[B]=[C]=[D]=[E]=[F]

I'm trying to delete Node B

This is my delete method

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

By doing some debugging I think I may not be initializing my variables correctly or looping through the list. Would anybody be able to point me to the right direction? And see what I may be doing wrong?

This is the entire code I'm working with

import java.io.*; import java.util.*; public class GDList implements Cloneable { private static class GNode { private E data; private GNode previous; private GNode 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 iterator[] { // TODO Auto-generated method stub return null; } } private GNode head; private GNode 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 names = new GDList[]; 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[]; } }

Video liên quan

Chủ Đề