implementato parzialmente la deque
This commit is contained in:
106
queue/NodeQueue.java
Normal file
106
queue/NodeQueue.java
Normal file
@@ -0,0 +1,106 @@
|
||||
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
|
||||
*/
|
||||
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 boolean isFull() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return number_of_elements;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
Node<E> temp;
|
||||
String to_return = "";
|
||||
if (!isEmpty()){
|
||||
for (temp = front;temp != null;temp = temp.getNext()){
|
||||
to_return+=temp.getElement();
|
||||
if (temp.getNext() != null){
|
||||
to_return+=",";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return to_return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private Node<E> front = null;
|
||||
private Node<E> rear = null;
|
||||
private int number_of_elements = 0;
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user