Ai
2 Star 0 Fork 0

CS-IMIS-23/GK20172301_JavaProgramming

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
LinkedDeque.java 2.53 KB
一键复制 编辑 原始数据 按行查看 历史
20172301郭恺 提交于 2018-09-27 20:57 +08:00 . LinkedDeque用链表实现双端队列
package week3;
import jsjf.DequeADT;
import jsjf.LinearNode1;
import jsjf.exceptions.EmptyCollectionException;
public class LinkedDeque<T>implements DequeADT<T> {
private LinearNode1<T> front,rear;
int count;
public LinkedDeque(){
count = 0;
front = rear = null;
}
@Override
public void enqueueLast(T element) {
LinearNode1<T> node = new LinearNode1<T>(element);
if (isEmpty())
{
rear = node;
}
else
{
rear.setNext(node);
}
node.setPrevious(rear);
rear = node;
count++;
}
@Override
public T dequeueLast() throws EmptyCollectionException {
if (isEmpty())
{
throw new EmptyCollectionException("queue");
}
LinearNode1<T> temp = rear;
rear = rear.getPrevious();
T result = temp.getElement();
if (!isEmpty())
{
rear.setNext(null);
}
else
{
front = null;
}
count--;
return result;
}
@Override
public void enqueueFirst(T element) {
LinearNode1<T> node = new LinearNode1<T>(element);
if (isEmpty())
{
front = node;
}
else
{
front.setPrevious(node);
}
node.setNext(front);
front = node;
count++;
}
@Override
public T dequeueFirst() {
if (isEmpty())
return null;
LinearNode1<T> temp = front;
front = front.getNext();
if (front!= null)
{
front.setPrevious(null);
}
else {
rear = null;
}
T result = temp.getElement();
return result;
}
@Override
public T first() {
if(isEmpty())
throw new EmptyCollectionException("queue");
return front.getElement();
}
@Override
public T last() {
if(isEmpty())
throw new EmptyCollectionException("queue");
return rear.getElement();
}
@Override
public boolean isEmpty() {
if (count == 0)
{
return true;
}
else
return false;
}
@Override
public int size() {
return count;
}
public String toString()
{
String result = "";
LinearNode1 temp = front;
while (temp != null)
{
result += temp.getElement() + "";
temp = temp.getNext();
}
return result;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/GK20172301_JavaProgramming.git
git@gitee.com:CS-IMIS-23/GK20172301_JavaProgramming.git
CS-IMIS-23
GK20172301_JavaProgramming
GK20172301_JavaProgramming
master

搜索帮助