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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started