Use a SinglyLinked List to implement a Queue a. Define a Queue interface. b. Define a LinkedQueue class (Node class).. c. Define a Test class to test all methods help me make a test class  first code package LinkedQueue;   import linkedstack.Node;   public class SLinkedList { private Node head = null; private Node tail = null; private int size = 0;     public SLinkedList() { head = null; tail = null; size = 0;   }   public int getSize() { return size;   } public boolean isEmpty() { return size == 0;   }   public E getFirst() { if(isEmpty()) { return null; } return head.getElement(); }  public E getLast() { if(isEmpty()) { return null; } return tail.getElement();  }  public void addFirst(E e) { Node newest = new Node<>(e, null); newest.setNext(head); head = newest; if(getSize()==0) { tail = head; } size++;    }    public void addLast(E e) { Node newest = new Node<>(e, null); if(isEmpty()) { head = newest; } else { tail.setNext(newest); } tail = newest; size++;  }  public E removeFirst() { if (isEmpty()) { return null; } E firstE = head.getElement(); head = head.getNext(); size--; if(getSize()==0) { tail = null; } return firstE;    }  public void display() { if (isEmpty()) { System.out.println("Singly linked list is empty."); } else { Node temp = head;   System.out.println("=============== begining of lists================"); do { System.out.println( "element =" + temp.getElement()); temp = temp.getNext(); }while (temp != null); System.out.println("============== Ending of lists================="); }  } } Secound code package LinkedQueue;   public class LinkedQueue implements Queue { private SLinkedList list = new SLinkedList<>();   public LinkedQueue ( ) {}   public int size( ) { return list.getSize(); } public boolean isEmpty( ) { return list.isEmpty( ); }   public void enqueue (E element) { list.addLast(element); } public E first() { return list.getFirst( ); } public E dequeue() { return list.removeFirst( ); } } third code package LinkedQueue;   public interface Queue {   int size();   boolean isEmpty( );   void enqueue(E e);   E first( );   E dequeue();   }

icon
Related questions
Question

Use a SinglyLinked List to implement a Queue
a. Define a Queue interface.
b. Define a LinkedQueue class (Node class)..
c. Define a Test class to test all methods

help me make a test class 

first code

package LinkedQueue;
 
import linkedstack.Node;
 
public class SLinkedList<E> {
private Node <E> head = null;
private Node <E> tail = null;
private int size = 0;
 
 
public SLinkedList() {
head = null;
tail = null;
size = 0;
 
}
 
public int getSize() {
return size;
 
}
public boolean isEmpty() {
return size == 0;
 
}
 
public E getFirst() {
if(isEmpty()) {
return null;
}
return head.getElement();
}
 public E getLast() {
if(isEmpty()) {
return null;
}
return tail.getElement();
 }
 public void addFirst(E e) {
Node<E> newest = new Node<>(e, null);
newest.setNext(head);
head = newest;
if(getSize()==0) {
tail = head;
}
size++;
 
 }
 
 public void addLast(E e) {
Node<E> newest = new Node<>(e, null);
if(isEmpty()) {
head = newest;
} else {
tail.setNext(newest);
}
tail = newest;
size++;
 }
 public E removeFirst() {
if (isEmpty()) {
return null;
}
E firstE = head.getElement();
head = head.getNext();
size--;
if(getSize()==0) {
tail = null;
}
return firstE;
 
 }
 public void display() {
if (isEmpty()) {
System.out.println("Singly linked list is empty.");
}
else {
Node<E> temp = head;
 
System.out.println("=============== begining of lists================");
do {
System.out.println( "element =" + temp.getElement());
temp = temp.getNext();
}while (temp != null);
System.out.println("============== Ending of lists=================");
}
 }
}
Secound code
package LinkedQueue;
 
public class LinkedQueue<E> implements Queue<E> {
private SLinkedList<E> list = new SLinkedList<>();
 
public LinkedQueue ( ) {}
 
public int size( ) {
return list.getSize();
}
public boolean isEmpty( ) {
return list.isEmpty( );
}
 
public void enqueue (E element) {
list.addLast(element);
}
public E first() {
return list.getFirst( );
}
public E dequeue() {
return list.removeFirst( );
}
}

third code

package LinkedQueue;
 
public interface Queue<E> {
 
int size();
 
boolean isEmpty( );
 
void enqueue(E e);
 
E first( );
 
E dequeue();
 
}
1 package LinkedQueue;
3 public class Node<E> {
4
private E element;
private Node <E> next;
5
6
70
8
9
00
LO
11
120
13
14
150
16
17
180
19
20
210
22
а ш
23
24
25
26
27
28
public Node (E e, Node<E> n) {
element = e;
next = n;
}
public E getElement () {
return element;
}
public Node<E> getNext () {
}
}
public void setElement (E e) {
element = e;
}
return next;
public void setNext (Node<E> n) {
next = n;
}
Transcribed Image Text:1 package LinkedQueue; 3 public class Node<E> { 4 private E element; private Node <E> next; 5 6 70 8 9 00 LO 11 120 13 14 150 16 17 180 19 20 210 22 а ш 23 24 25 26 27 28 public Node (E e, Node<E> n) { element = e; next = n; } public E getElement () { return element; } public Node<E> getNext () { } } public void setElement (E e) { element = e; } return next; public void setNext (Node<E> n) { next = n; }
Expert Solution
Step 1: Program Approach

The code you've provided is a Java implementation of a queue using a singly-linked list. Here's a breakdown of the code:

  1. Queue interface: It defines the methods that a queue should have, including enqueue, dequeue, and isEmpty.

  2. Node class: This is a simple class representing a node in the singly-linked list. Each node holds an integer value (data) and a reference to the next node in the list (next).

  3. LinkedQueue class: It implements the Queue interface. This class maintains a reference to the head and tail nodes of the linked list.

    • enqueue(int data): Adds a new node with the specified data to the end of the queue. If the queue is empty, both head and tail are set to the new node.

    • dequeue(): Removes and returns the data from the front of the queue. If the queue is empty, it returns -1. If the queue becomes empty after dequeue, both head and tail are set to null.

    • isEmpty(): Checks if the queue is empty by inspecting the head reference.

  4. Main class: The test class demonstrates how to use the LinkedQueue. It creates an instance of Queue using the LinkedQueue class, enqueues three elements, and then dequeues and prints them in the order they were added.

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer