105 lines
2.1 KiB
Java
105 lines
2.1 KiB
Java
package queue;
|
|
|
|
import exceptions.EmptyQueueException;
|
|
import stack.utility.Node;
|
|
|
|
import java.awt.image.AreaAveragingScaleFilter;
|
|
|
|
/**
|
|
* Created with xgiovio.macbookair.
|
|
* User: xgiovio
|
|
* Date: 17/03/14
|
|
* Time: 14:08
|
|
*/
|
|
//Copyright (c) 2003 Brown University, Providence, RI
|
|
//Additional modifications and methods by xgiovio
|
|
public class NodeQueue<E> implements Queue<E> {
|
|
|
|
|
|
@Override
|
|
public void enqueue(E element) {
|
|
|
|
Node<E> temp = new Node<E>(element,null);
|
|
if (rear == null){
|
|
rear = temp;
|
|
}else {
|
|
rear.setNext(temp);
|
|
rear = temp;
|
|
}
|
|
number_of_elements++;
|
|
if (number_of_elements == 1){
|
|
front = rear;
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public E dequeue() throws EmptyQueueException {
|
|
if (isEmpty()){
|
|
throw new EmptyQueueException();
|
|
}else{
|
|
Node<E> temp = front;
|
|
front = front.getNext();
|
|
number_of_elements--;
|
|
if (number_of_elements == 0){
|
|
rear = front;
|
|
}
|
|
return temp.getElement();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public E front() throws EmptyQueueException {
|
|
if (isEmpty()){
|
|
throw new EmptyQueueException();
|
|
} else{
|
|
return front.getElement();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean isEmpty() {
|
|
if (number_of_elements == 0){
|
|
return true;
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
public int size() {
|
|
return number_of_elements;
|
|
}
|
|
|
|
|
|
@Override
|
|
public String toString() {
|
|
Node<E> temp;
|
|
String to_return = "[";
|
|
for (temp = front;temp != null;temp = temp.getNext()){
|
|
to_return+=temp.getElement();
|
|
if (temp.getNext() != null){
|
|
to_return+=",";
|
|
}
|
|
|
|
}
|
|
to_return += "]";
|
|
|
|
return to_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Node<E> front = null;
|
|
private Node<E> rear = null;
|
|
private int number_of_elements = 0;
|
|
|
|
|
|
}
|