1 Star 0 Fork 0

20172304段志轩/段志轩20172304java

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Deque.java 3.87 KB
一键复制 编辑 原始数据 按行查看 历史
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;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/duanzhixuan/duan_zhixuan_20172304java.git
git@gitee.com:duanzhixuan/duan_zhixuan_20172304java.git
duanzhixuan
duan_zhixuan_20172304java
段志轩20172304java
master

搜索帮助

371d5123 14472233 46e8bd33 14472233