Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In JAVA Implement the Queue ADT using a circular linked list The LinkedQueue.java Implements QueueInterface using a linked list. 1. QueueInterface.java package ch04.queues; public interface

In JAVA

Implement the Queue ADT using a circular linked list

The LinkedQueue.java Implements QueueInterface using a linked list.

1. QueueInterface.java

package ch04.queues;

public interface QueueInterface {

void remove(int count) throws QueueUnderflowException;

boolean swapStart();

boolean swapEnds();

}

2. LLNode.java

package support;

public class LLNode {

private T element; private LLNode next;

public LLNode(T element) { this.element = element; }

public void setNext(LLNode node) { this.next = node;

}

public T getElement() { return element; }

/** * @param element the element to set */ public void setElement(T element) { this.element = element; }

public LLNode getNext() { return next; }

}

3. LinkedQueue.java

package ch04.queues;

import support.LLNode;

public class LinkedQueue implements QueueInterface {

protected LLNode front; // reference to the front of this queue protected LLNode rear; // reference to the rear of this queue protected int numElements = 0; // number of elements in this queue

public LinkedQueue() { front = null; rear = null; }

public void enqueue(T element) { LLNode node = new LLNode(element);

if (isEmpty()) front = node; else rear.setNext(node); rear = node; numElements++; }

/** * The remove method removes the front count elements from the queue * * @param count * @throws QueueUnderflowException */ @Override public void remove(int count) throws QueueUnderflowException { for (int i = 1; i <= count; i++) { dequeue(); } }

/** * The swapStart method returns false if less than two elements are in the * queue, otherwise reverses the order of the front two elements in the * queue and returns true. * * @return * @throws QueueUnderflowException */ public boolean swapStart() { if (numElements < 2) return false; else { // swap T firstElement = front.getElement(); T lastElement = front.getNext().getElement(); front.setElement(lastElement); front.getNext().setElement(firstElement); return true; }

}

/** * The swapEnds method returns false if there are less than two elements in * the queue, otherwise swaps the first and last elements of the queue and * returns true * * @return */ public boolean swapEnds() { if (numElements < 2) return false; else { T firstElement = front.getElement(); T lastElement = rear.getElement(); front.setElement(lastElement); rear.setElement(firstElement); return true;

} }

public T dequeue() throws QueueUnderflowException { if (isEmpty()) throw new QueueUnderflowException("Queue Underflow!");

T result = front.getElement(); front = front.getNext(); numElements--;

if (isEmpty()) rear = null; return result; }

public boolean isEmpty() { return (numElements == 0); }

public String toString() { String result = ""; LLNode current = front;

while (current != null) { result = result + (current.getElement()).toString() + " "; current = current.getNext(); }

return result; } }

4. QueueUnderflowException.java

package ch04.queues;

@SuppressWarnings("serial") public class QueueUnderflowException extends Exception { private String message; public QueueUnderflowException(String message) { this.message=message; System.out.println(this.message); } }

5. LinkedQueueDriver.java

package ch04.queues;

public class LinkedQueueDriver {

public static void main(String[] args) { try { LinkedQueue linkedQueue = new LinkedQueue(); linkedQueue.enqueue(10); linkedQueue.enqueue(20); linkedQueue.enqueue(30); linkedQueue.enqueue(40); linkedQueue.enqueue(50); System.out.println("Queue"); System.out.println(linkedQueue);

System.out.println("after swapStart"); linkedQueue.swapStart(); System.out.println(linkedQueue); System.out.println("after swapEnds"); linkedQueue.swapEnds(); System.out.println(linkedQueue); System.out.println("after remove 2 count"); linkedQueue.remove(2); System.out.println(linkedQueue); linkedQueue.remove(3); System.out.println("after remove 3 count"); System.out.println(linkedQueue); linkedQueue.remove(1); } catch (QueueUnderflowException e) { e.printStackTrace(); }

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions

Question

What are the objectives of Human resource planning ?

Answered: 1 week ago

Question

Explain the process of Human Resource Planning.

Answered: 1 week ago