Question: complete following 2 methods: printQueue method and printQueue2 method Write an implementation of the printQueue method which removes and prints each element in the Queue,

complete following 2 methods: printQueue method and printQueue2 method

Write an implementation of the printQueue method which removes and prints each element in the Queue, restoring the queue before it returns. It uses a temporary queue that actually holds the same information as the original queue.

// **************************************************************

// QueueTest.java

//

// A simple driver to manipulate a queue.

//

// **************************************************************

public class QueueTest

{

public static void main(String[] args)

{

QueueADT queue = new LinkedQueue();

//put some stuff in the queue: 0,2,4,..,14

for (int i=0; i<8; i++)

queue.enqueue(i*2);

System.out.println(" ** Initial queue **");

printQueue(queue);

//dequeue 4 items

for (int i=0; i<4; i++)

queue.dequeue();

System.out.println( " ** After dequeueing 4 items **");

printQueue(queue);

//enqueue 7 more: 1,2,..,7

for (int i=0; i<7; i++)

queue.enqueue(i+1);

System.out.println (" ** After enqueueing 7 more items **");

printQueue(queue);

}

//----------------------------------------------------------

// Prints elements of queue, restoring it before returning

//----------------------------------------------------------

public static void printQueue(QueueADT queue)

{

}

//write a printQueue method that prints the queue and restores it to its original form without using an auxiliary data structure (stack,queue, etc.). but printQueue2 method can do the same thing as printQueue method.

public static void printQueue2(QueueADT queue)

{

}

}

}

////////LinkedQueue.java

import jsjf.exceptions.*;

/** * LinkedQueue represents a linked implementation of a queue. * * @author Java Foundations * @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 if (isEmpty()) throw new EmptyCollectionException ("queue");

return head.getElement(); }

/** * 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 return (count == 0); } /** * 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 return count; }

/** * Returns a string representation of this queue. * @return the string representation of the queue */ public String toString() { // To be completed as a Programming Project String result = ""; LinearNode current = head;

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

return result; } }

make sure can run in netbeans and has an out put.

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!