Complete the code in Test.java and LinkedQueue.java Sample run No input Output: Given Code Test.java // Test is in defau

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Complete the code in Test.java and LinkedQueue.java Sample run No input Output: Given Code Test.java // Test is in defau

Post by answerhappygod »

Complete the code in Test.java and LinkedQueue.java
Sample run
No input
Output:
Given Code
Test.java
// Test is in default package
// your code starts here
// your code ends here
public class Test {
public static void main(String[] args) {
LinkedQueue<Integer> q = new
LinkedQueue<Integer>();
q.enqueue(0);
q.enqueue(1);
for (int i = 0; i < 6; i++) {
int a = q.dequeue();
int b = q.dequeue();
q.enqueue(b);
q.enqueue(a + b);
System.out.print(a + " ");
}
}
}
QueueInterface.java
package exam;
import java.util.NoSuchElementException;
/**
* Represents a First-in-first-out (FIFO) queue of
objects.
*
*/
public interface QueueInterface<T> {
void enqueue(T element);
/**
* Removes the head element and returns it if the queue
is not empty.
* @return the head if the queue is not empty.
* @throws NoSuchElementException when the queue is
empty.
*/
T dequeue() throws NoSuchElementException;
/**
* Optional
* @return true if the quueue is full, false
otherwise.
*/
default boolean isFull() {
return false;
}
/**
* @return true if the queue is empty, false
otherwise.
*/
boolean isEmpty();
/**
* @return the number of elements in the queue.
*/
int size();
}
// your code starts here
// your code ends here
LinkedQueue.java
package exam;
import java.util.NoSuchElementException;
public class LinkedQueue<T> implements
QueueInterface<T> {
protected LinkedNode<T> front, rear;
protected int numOfElements;
public LinkedQueue() {
front = rear = null;
numOfElements = 0;
}
//Complete the enqueue and dequeue methods
//public void enqueue(T element)
//public T dequeue() throws
NoSuchElementException
// your code starts here
// your code ends here
@Override
public boolean isFull() {
return false;
}
@Override
public boolean isEmpty() {
return rear == null;
}
@Override
public int size() {
return numOfElements;
}
}
LinkedNode.java
package exam;
public class LinkedNode<T> {
T val;//an element
LinkedNode<T> next;//a reference to its
successor
public LinkedNode() {
super(); //val is set to default value and
next = null;
}
public LinkedNode(T val) {
this.val = val;
this.next = null;
}
public LinkedNode(T val, LinkedNode<T> next)
{
this.val = val;
this.next = next;
}
public T getVal() {
return val;
}
public void setVal(T val) {
this.val = val;
}
public LinkedNode<T> getNext() {
return next;
}
public void setNext(LinkedNode<T> next)
{
this.next = next;
}
}
// your code starts here
// your code ends here
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply