Question: Complete the implementation of LinkedQueue class presented in this chapter. Specifically, complete the implementations of the first, isEmpty, size, and toString Methods. /* * To

Complete the implementation of LinkedQueue class presented in this chapter. Specifically, complete the implementations of the first, isEmpty, size, and toString Methods.

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package jsjf;

import jsjf.exceptions.*;

/** * LinkedQueue represents a linked implementation of a queue. * * @author Lewis and Chase * @version 4.0 */ public class LinkedQueue implements QueueADT { private int count; private LinearNode head, tail;

/** * Creates an empty queue. */ public LinkedQueue() { count = 0; head = tail = null; }

/** * Adds the specified element to the tail of this queue. * @param element the element to be added to the tail of the queue */ public void enqueue(T element) { LinearNode node = new LinearNode(element);

if (isEmpty()) head = node; else tail.setNext(node);

tail = node; count++; }

/** * Removes the element at the head of this queue and returns a * reference to it. * @return the element at the head of this queue * @throws EmptyCollectionException if the queue is empty */ public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("queue");

T result = head.getElement(); head = head.getNext(); count--;

if (isEmpty()) tail = null;

return result; } /** * Returns a reference to the element at the head of this queue. * The element is not removed from the queue. * @return a reference to the first element in this queue * @throws EmptyCollectionsException if the queue is empty */ public T first() throws EmptyCollectionException { // To be completed as a Programming Project }

/** * Returns true if this queue is empty and false otherwise. * @return true if this queue is empty */ public boolean isEmpty() { // To be completed as a Programming Project } /** * Returns the number of elements currently in this queue. * @return the number of elements in the queue */ public int size() { // To be completed as a Programming Project }

/** * Returns a string representation of this queue. * @return the string representation of the queue */ public String toString() { // To be completed as a Programming Project } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!