**My code compiles and deletes from the head and tail, but I’m not able to delete from the middle. Can someone look at my code and see what I’m missing. Thanks **
public class Queue {
private IntDNode head;
private IntDNode tail;
public Queue() {
head = tail = null;
}
public void insertToSortedlist(int n) {
IntDNode node = head;
while (node != null && n >= node.getData()) {
node = node.getNext();
}
if (node == head) {
addToHead(n);
}
else {
if (node == null) {
addToTail(n);
}
else {
insertBefore(n, node);
}
}
}
private void addToHead(int n) {
head = new IntDNode (n, null, head);
if (head.getNext() != null) {
head.getNext().setPrev(head);
}
if (tail == null) {
tail = head;
}
else {
if (tail.getPrev() == null) {
tail.setPrev(head);
}
}
}
private void addToTail(int n) {
IntDNode temp = new IntDNode(n, tail, null);
tail.setNext(temp);
tail = temp;
}
private void insertBefore(int n, IntDNode node) {
IntDNode temp = new IntDNode(n, node.getPrev(), node);
node.setPrev(temp);
temp.getPrev().setNext(temp);
}
public void addToQueue(int n) {
IntDNode node = head;
while (node != null && n >= node.getData()) {
node = node.getNext();
}
if (node == head) {
addToHead(n);
}
else {
if (node == null) {
addToTail(n);
}
else {
insertBefore(n, node);
}
}
}
private void deleteFromTail() {
tail = tail.getPrev();
if (tail == null) {
head = null;
}
else {
tail.setNext(null);
}
}
private void deleteFromHead() {
head = head.getNext();
if (head == null) {
tail = null;
}
else {
tail.setPrev(head);
}
}
public void deleteFromQueue(int n) {
IntDNode auxNode = head;
while (auxNode != null && auxNode.getData() < n) {
auxNode = auxNode.getNext();
}
if (auxNode != null && auxNode.getData() == n) {
if (auxNode == tail) {
deleteFromTail();
}
else {
if (auxNode == head) {
deleteFromHead();
}
else {
}
}
}
}
public boolean isEmpty() {
return head == null;
}
public void printQueue() {
if (isEmpty()) {
System.out.println("The list is empty");
}
else {
IntDNode node = head;
while (node != null) {
System.out.print(node.getData() + " ");
node = node.getNext();
}
System.out.println();
}
}
}