关于queue with links的java程序补全

来源:百度知道 编辑:UC知道 时间:2024/05/25 07:55:35
Complete the implementation of the LinkedQueue class. 其中要包括 first, isEmpty, size,和 toString 方法。

//LinkedQueue

package jss2;
import jss2.exceptions.*;

public class LinkedQueue<T> implements QueueADT<T>
{
private int count;
private LinearNode<T> front, rear;
// creates an expty queue.
public LinkedQueue()
{
count = 0;
front = rear = null;
}
}
请高人给出按要求并可以正确运行的程序, 必要处请给注释, 必谢。路过者,只动嘴者没有分。 谢谢大家
接上面的程序

// the enqueue Operation
public void enqueue (T element)
{
LinearNode<T> node = new LinearNode<T> (element)

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

//the dequeue Operation
public T dequeue() throws EmptyCollectionException
{
if

public boolean isEmpty() {
if(count == 0)
return true;
else
return false;
}

public T first() throws EmptyCollectionException{
if (isEmpty())
throw new EmptyCollectionException("queue");
T result = front.getElement();
return result;
}

public int size(){
return count;
}

public String toString(){
StringBuffer sb = new StringBuffer();
LinearNode<T> tempNode = front;
while (front != null) {
sb.append(tempNode.getElement() + ",");
tempNode = front.getNext();
}
// delete the last ',';
return "{" + sb.toString().replaceAll(",$", "") + "}";
}