2 Star 0 Fork 0

CS-IMIS-23 / 20172309_javaProgramming

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
LinkedQueue.java 2.71 KB
一键复制 编辑 原始数据 按行查看 历史
package second_term.third_week;
import second_term.first_week.EmptyCollectionException;
/**
* LinkedQueue represents a linked implementation of a queue.
*
* @author Lewis and Chase
* @version 4.0
*/
public class LinkedQueue<T> implements QueueADT<T>
{
private int count;
private LinearNode<T> head, tail;
/**
* Creates an empty queue.
*/
public LinkedQueue()
{
count = 0;
head = tail = 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 enqueue(T element)
{
LinearNode<T> node = new LinearNode<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 dequeue() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("queue");
T result = head.getElement();
head = head.getNext();
count--;
if (isEmpty())
tail = 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 first() throws EmptyCollectionException
{
if (isEmpty()){
throw new EmptyCollectionException("queue");
}
else
return head.getElement() ;
// To be completed as a Programming Project
}
/**
* Returns true if this queue is empty and false otherwise.
* @return true if this queue is empty
*/
public boolean isEmpty()
{
return count == 0;
// To be completed as a Programming Project
}
/**
* Returns the number of elements currently in this queue.
* @return the number of elements in the queue
*/
public int size()
{
return count;
// To be completed as a Programming Project
}
/**
* Returns a string representation of this queue.
* @return the string representation of the queue
*/
public String toString()
{
String result = "" ;
while(head!=null){
result+=head.getElement()+" ";
head = head.getNext();
}
return result;
// To be completed as a Programming Project
}
}
Java
1
https://gitee.com/CS-IMIS-23/20172309_javaProgramming.git
git@gitee.com:CS-IMIS-23/20172309_javaProgramming.git
CS-IMIS-23
20172309_javaProgramming
20172309_javaProgramming
master

搜索帮助