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(); }
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(); }
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();
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 5 steps with 2 images