public class Queue<T>{ private int size; private Node front, rear; public void offer(T data) { Node tempNode = new Node(data); if(!isEmpty()) { rear.setNextNode (temp Node); rear = tempNode; }else{ front = rear = tempNode; size++; } public T poll(){ if(!isEmpty()){ TX = (T) front.getData(); size--; System.out.println("Poll was: x + ", and is now deleted!"); front = front.getNextNode(); return x; }else{ System.out.println("The queue is now empty!"); return (T) (new Integer (-999)); } } public T peek() { if(!isEmpty()) { TX = (T) front.getData(); size--; System.out.println("Peek was: + ", and is now deleted!"); return x; }else{ System.out.println("The queue is now empty!"); return (T) (now Tntorer 1.000).
SELSE2 System.out.println("The queue is now empty!"); return (T) (new Integer (-999)); } } public boolean isEmpty() { return (size = 0); } public void display(){ Node tempNode = front; if(!isEmpty()) { while(tempNode.getNextNode() != null){ System.out.println(tempNode.getData()); tempNode = tempNode.getNextNode(); } } System.out.println(tempNode.getData()); System.out.println("The queue is empty!"); } }
Modify the queue dass that we implemented in-class on April 18, using Linked List to add another method to access (or search) an element at a certain index from the queue. Hint: it would be a modified version of the display() method. You would be sending the index as an argument and you keep a counter. Instead of printing all elements, you print the element only when your counter matches the index,
public class Queue{ private int size; private Node front, rear; public void offer(T data) { Node tempNode = new Node(
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
public class Queue{ private int size; private Node front, rear; public void offer(T data) { Node tempNode = new Node(
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!