Complete the code in Test.java and LinkedQueue.java Sample run No input Output: Given Code Test.java // Test is in defau
Posted: Thu May 05, 2022 1:21 pm
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
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