Question: HELP IN JAVA: I created a circular queue class and starting at the head of the queue i have to delete every nth node until
HELP IN JAVA:
I created a circular queue class and starting at the head of the queue i have to delete every nth node until only one node remains. Output the String in that last node. This is what I have so far:
public class Queue {
private Node head, tail = null; int size = 0;
public Queue() { head = tail = null; size = 0;
}
//enqueue public void enqueue(T item) { Node temp = new Node(item); if (head == null) { head = temp; tail = temp; } else { temp.next = head; tail.next = temp; tail = temp;
} size++;
}
//dequeue public T dequeue(T item) { Node temp = head; head = head.next; tail.next = head; size--; return (T) temp.item; }
//size public int size() { if (size == 0) { } return size; }
//peek public T peek() { return (T) head.item; //return item }
//isEmpty public boolean isEmpty() {
return (size == 0); }
//print in queue class public void print(int i) { Node curr = head; while (curr != null) { System.out.println(curr.item); curr = curr.next; }
}
public void delete(int num, Queue q) { } }
I need help writing the delete method to delete every nth node until only one node remains and output the String in that last node.
Thanks !!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
