代码拉取完成,页面将自动刷新
package weeek3;
import weeek3.jsjf.exceptions.EmptyCollectionException;
/**
* LinkedQueue represents a linked implementation of a queue.
*
* @author Lewis and Chase
* @version 4.0
*/
public class Deque<T>
{
private int count;
private LinearNode1<T> head, tail,previous;
/**
* Creates an empty queue.
*/
public Deque()
{
count = 0;
head = tail =previous= 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 Headenqueue(T element)
{
LinearNode1<T> node = new LinearNode1<T>(element);
if (isEmpty())
tail = node;
else
head.setPrevious(node);
head = node;
count++;
}
public void Tailenqueue(T element)
{
LinearNode1<T> node= new LinearNode1<T>(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 Taildequeue() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("queue");
T result = head.getElement();
head = head.getNext();
count--;
if (isEmpty())
tail = null;
return result;
}
public T Headdequeue() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("queue");
T result =tail.getElement();
tail=tail.getNext();
count--;
if (isEmpty())
head=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 EmptyCollectionException if the queue is empty
*/
public T Headfirst() throws EmptyCollectionException
{
if(isEmpty()) {
throw new EmptyCollectionException("queue");
}
else
return head.getElement();
}
public T Tailfirst() throws EmptyCollectionException
{
if(isEmpty())
{
throw new EmptyCollectionException("queue");
}
else
return tail.getElement();
}
/**
* Returns true if this queue is empty and false otherwise.
* @return true if this queue is empty
*/
public boolean isEmpty()
{
if (head.getElement()==null)
return true;
else
return false;
}
/**
* Returns the number of elements currently in this queue.
* @return the number of elements in the queue
*/
public int size()
{
return count;
}
/**
* Returns a string representation of this queue.
* @return the string representation of the queue
*/
@Override
public String toString()
{
String result="";
int count1=count;
LinearNode1 list=head;
while(count1!=0) {
result = list.getElement() + " ";
count1--;
}
return result;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。