
How to fix the error?
import java.util.*;
import java.util.Iterator;
import java.util.NoSuchElementException;
class LinkedQueue<Item> implements Iterable<Item> {
private int n;
private Node first;
private Node last;
private class Node {
private Item item;
private Node next;
}
public LinkedQueue() {
first = null;
last = null;
n = 0;
assert check();
}
public boolean isEmpty() {
return first == null;
}
public int size() {
return n;
}
public Item peek() {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
return first.item;
}
public void enqueue(Item item) {
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else oldlast.next = last;
n++;
assert check();
}
public Item dequeue() {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
Item item = first.item;
first = first.next;
n--;
if (isEmpty()) last = null;
assert check();
return item;
}
public String toString() {
StringBuilder s = new StringBuilder();
for (Item item : this)
s.append(item + " ");
return s.toString();
}
private boolean check() {
if (n < 0) {
return false;
}
else if (n == 0) {
if (first != null) return false;
if (last != null) return false;
}
else if (n == 1) {
if (first == null || last == null) return false;
if (first != last) return false;
if (first.next != null) return false;
}
else {
if (first == null || last == null) return false;
if (first == last) return false;
if (first.next == null) return false;
if (last.next != null) return false;
int numberOfNodes = 0;
for (Node x = first; x != null && numberOfNodes <= n; x = x.next) {
numberOfNodes++;
}
if (numberOfNodes != n) return false;
Node lastNode = first;
while (lastNode.next != null) {
lastNode = lastNode.next;
}
if (last != lastNode) return false;
}
return true;
}
public Iterator<Item> iterator() {
return new LinkedIterator();
}
private class LinkedIterator implements Iterator<Item> {
private Node current = first;
public boolean hasNext() { return current != null; }
public void remove() { throw new UnsupportedOperationException(); }
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
}
class Customer {
private static int custId = 0;
private String name;
public Customer() {
name = "Customer " + custId;
custId++;
}
public String getName() {
return name;
}
public String toString() {
return name;
}
}
public class BankServise{
public static void main(String[] args) {
LinkedQueue<Customer> customerQueue = new LinkedQueue<Customer>();
int counter = 0;
while (counter < 10) {
int newCustomers = (int) (Math.random() * 10) + 1;
for (int i = 0; i < newCustomers; i++) {
Customer c = new Customer();
customerQueue.enqueue(c);
System.out.println(c + " is waiting in line");
}
int servingCustomers = (int) (Math.random() * 10) + 1;
for (int i = 0; i < servingCustomers; i++) {
if (customerQueue.isEmpty()) {
break;
}
System.out.println(customerQueue.dequeue() + " at the teller window");
}
counter++;
}
while (!customerQueue.isEmpty()) {
System.out.println(customerQueue.dequeue() + " is being served");
}
}
}


Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

- package hw5; public class LinkedIntSet {private static class Node {private int data;private Node next; public Node(int data, Node next) {this.data = data;this.next = next;}} private Node first; // Always points to the first node of the list.// THE LIST IS ALWAYS IN SORTED ORDER!private int size; // Always equal to the number of elements in the set. /*** Construts an empty set.*/public LinkedIntSet() {throw new RuntimeException("Not implemented");} /*** Returns the number of elements in the set.* * @return the number of elements in the set.*/public int size() {throw new RuntimeException("Not implemented");} /*** Tests if the set contains a number* * @param i the number to check* @return <code>true</code> if the number is in the set and <code>false</code>* otherwise.*/public boolean contains(int i) {throw new RuntimeException("Not implemented");} /*** Adds <code>element</code> to this set if it is not already present and* returns…arrow_forwardAssume the Queue is initially empty. class Queue { public: int size () ; bool empty (); chará front () throw (Exception); void enqueue (char& e); void dequeue () throw (Exception); Function call Output Queue Contents (back -> front) enqueue ('A') enqueue ('B') front ( ) size ( ) dequeue () enqueue ('C') empty( ) dequeue ( ) size( ) dequeue () empty ( ) front ( ) enqueue ('D') front () dequeue () dequeue ( )arrow_forwardGiven the code: import java.util.HashMap; import java.util.LinkedList; import java.util.Scanner; public class Controller { privateHashMap<String,LinkedList<Stock>>stockMap; publicController(){ stockMap=newHashMap<>(); Scannerinput=newScanner(System.in); do{ // Prompt for stock name or option to quit System.out.print("Enter stock name or 3 to quit: "); StringstockName=input.next(); if(stockName.equals("3")){ break;// Exit if user inputs '3' } // Get or create a list for the specified stock LinkedList<Stock>stockList=stockMap.computeIfAbsent(stockName,k->newLinkedList<>()); // Prompt to buy or sell System.out.print("Input 1 to buy, 2 to sell: "); intcontrolNum=input.nextInt(); System.out.print("How many stocks: "); intquantity=input.nextInt(); if(controlNum==1){ // Buying stocks System.out.print("At what price: "); doubleprice=input.nextDouble(); buyStock(stockList,stockName,quantity,price); }else{ // Selling stocks System.out.print("Press…arrow_forward
- import java.util.HashSet; import java.util.Set; // Define a class named LinearSearchSet public class LinearSearchSet { // Define a method named linearSearch that takes in a Set and an integer target // as parameters public static boolean linearSearch(Set<Integer> set, int target) { // Iterate over all elements in the Set for () { // Check if the current value is equal to the target if () { // If so, return true } } // If the target was not found, return false } // Define the main method public static void main(String[] args) { // Create a HashSet of integers and populate integer values Set<Integer> numbers = new HashSet<>(); // Define the target to search for numbers.add(3); numbers.add(6); numbers.add(2); numbers.add(9); numbers.add(11); // Call the linearSearch method with the set…arrow_forwardimport java.util.*;public class MyLinkedListQueue{ class Node { public Object data; public Node next; private Node first; public MyLinkedListQueue() { first = null; } public Object peek() { if(first==null) { throw new NoSuchElementException(); } return first.data; } public void enqueue(Object element) { Node newNode = new newNode(); newNode.data = element; newNode.next = first; first = newNode; } public boolean isEmpty() { return(first==null); } public int size() { int count = 0; Node p = first; while(p ==! null) { count++; p = p.next; } return count; } } } Hello, I am getting an error message when I execute this program. This is what I get: MyLinkedListQueue.java:11: error: invalid method declaration; return type required public MyLinkedListQueue() ^1 error I also need to have a dequeue method in my program. It is very similar to the enqueue method. I'll…arrow_forwardJava - class IntBTNode { private int data; private IntBTNode left; private IntBTNode right; } Write a new static method of the IntBTNode class to meet the following specification. public static boolean all10(IntBTNode root) // Precondition: root is the root reference of a binary tree (but // NOT NECESSARILY a search tree). // Postcondition: The return value is true if every node in the tree // contains 10. NOTE: If the tree is empty, the method returns true.arrow_forward
- VERY EASY RPN PROBLEM. read the directions please answer thank you public class Node <T>{ private T data; private Node<T> next; public Node(T data, Node<T> next) { this.data = data; this.next = next; } public T getData() { return data; } public Node<T> getNext() { return next; } } import java.util.Stack; public class StackArray<T> implements Stack<T> { private T [] elements; private int top; private int size; public StackArray (int size) { elements = (T[]) new Object[size]; top = -1; this.size = size; } public boolean empty() { return top == -1; } public boolean full() { return top == size - 1; } public boolean push(T el) { if (full()) return false; else { top++; elements[top] = el; return true; } }…arrow_forwardCan someone explain it to me.arrow_forwardcomplete TODO's using Javaarrow_forward
- Draw a UML class diagram for the following code: import java.util.*; public class QueueOperationsDemo { public static void main(String[] args) { Queue<String> linkedListQueue = new LinkedList<>(); linkedListQueue.add("Apple"); linkedListQueue.add("Banana"); linkedListQueue.add("Cherry"); System.out.println("Is linkedListQueue empty? " + linkedListQueue.isEmpty()); System.out.println("Front element: " + linkedListQueue.peek()); System.out.println("Removed element: " + linkedListQueue.remove()); System.out.println("Front element after removal: " + linkedListQueue.peek()); Queue<Integer> arrayDequeQueue = new ArrayDeque<>(); arrayDequeQueue.add(10); arrayDequeQueue.add(20); arrayDequeQueue.add(30); System.out.println("Is arrayDequeQueue empty? " + arrayDequeQueue.isEmpty()); System.out.println("Front element: " + arrayDequeQueue.peek());…arrow_forwardimport java.util.*;import java.io.*; public class HuffmanCode { private Queue<HuffmanNode> queue; private HuffmanNode overallRoot; public HuffmanCode(int[] frequencies) { queue = new PriorityQueue<HuffmanNode>(); for (int i = 0; i < frequencies.length; i++) { if (frequencies[i] > 0) { HuffmanNode node = new HuffmanNode(frequencies[i]); node.ascii = i; queue.add(node); } } overallRoot = buildTree(); } public HuffmanCode(Scanner input) { overallRoot = new HuffmanNode(-1); while (input.hasNext()) { int asciiValue = Integer.parseInt(input.nextLine()); String code = input.nextLine(); overallRoot =…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





