Ai
2 Star 0 Fork 0

CS-IMIS-23/GK20172301_JavaProgramming

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
LinkedQueue.java 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
20172301郭恺 提交于 2018-09-27 20:58 +08:00 . LinkedQueue用链表实现队列
package week3;
import jsjf.LinearNode;
import jsjf.QueueADT;
import jsjf.exceptions.EmptyCollectionException;
public class LinkedQueue<T> implements QueueADT<T> {
private int count;
private LinearNode<T> head,tail;
// 创建一个空队列
public LinkedQueue()
{
count = 0;
head = tail = null;
}
@Override
public void enqueue(T element) {
LinearNode<T> node = new LinearNode<T>(element);
if (isEmpty())
{
head = node;
}
else
{
tail.setNext(node);
}
tail = node;
count++;
}
@Override
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;
}
@Override
public T first() {
T result = head.getElement();
return result;
}
@Override
public boolean isEmpty() {
if (count ==0)
return true;
else
return false;
}
@Override
public int size() {
return count;
}
public String toString()
{
String result = "";
LinearNode temp = head;
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

搜索帮助