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
third code
The code you've provided is a Java implementation of a queue using a singly-linked list. Here's a breakdown of the code:
Queue
interface: It defines the methods that a queue should have, includingenqueue
,dequeue
, andisEmpty
.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
).LinkedQueue
class: It implements theQueue
interface. This class maintains a reference to thehead
andtail
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, bothhead
andtail
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, bothhead
andtail
are set to null.isEmpty()
: Checks if the queue is empty by inspecting thehead
reference.
Main
class: The test class demonstrates how to use theLinkedQueue
. It creates an instance ofQueue
using theLinkedQueue
class, enqueues three elements, and then dequeues and prints them in the order they were added.
Step by step
Solved in 5 steps with 2 images