244 lines
5.8 KiB
Java
244 lines
5.8 KiB
Java
import java.util.NoSuchElementException;
|
|
|
|
/**
|
|
* Represents a double-ended queue (deque) that allows adding and removing items
|
|
* from both ends.
|
|
*/
|
|
public class Deque<T> {
|
|
|
|
/**
|
|
* The front item of the deque.
|
|
*/
|
|
private Item<T> front;
|
|
|
|
/**
|
|
* The back item of the deque.
|
|
*/
|
|
private Item<T> back;
|
|
|
|
/**
|
|
* Constructs an empty deque.
|
|
*/
|
|
public Deque() {
|
|
this.front = null;
|
|
this.back = null;
|
|
}
|
|
|
|
/**
|
|
* Returns the front item in the deque.
|
|
*
|
|
* @return The front item in the deque.
|
|
*/
|
|
private Item<T> getFront() {
|
|
return front;
|
|
}
|
|
|
|
/**
|
|
* Returns the back item in the deque.
|
|
*
|
|
* @return The back item in the deque.
|
|
*/
|
|
private Item<T> getBack() {
|
|
return back;
|
|
}
|
|
|
|
/**
|
|
* Sets the front item in the deque.
|
|
*
|
|
* @param front The item to set as the front.
|
|
*/
|
|
private void setFront(Item<T> front) {
|
|
this.front = front;
|
|
}
|
|
|
|
/**
|
|
* Sets the back item in the deque.
|
|
*
|
|
* @param back The item to set as the back.
|
|
*/
|
|
private void setBack(Item<T> back) {
|
|
this.back = back;
|
|
}
|
|
|
|
/**
|
|
* Adds an item to the front of the deque, if the value is not null or empty.
|
|
*
|
|
* @param value The value of the item to add.
|
|
*/
|
|
public void addFirst(Object value) { // TODO: change
|
|
if (value == null) {
|
|
// TODO: change
|
|
return;
|
|
}
|
|
|
|
Item item = new Item(value);
|
|
if (isEmpty()) {
|
|
setFront(item);
|
|
setBack(item);
|
|
} else {
|
|
item.setNext(getFront());
|
|
getFront().setPrev(item);
|
|
setFront(item);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Adds an item to the back of the deque, if the value is not null or empty.
|
|
*
|
|
* @param value The value of the item to add.
|
|
*/
|
|
public void addLast(Object value) { // TODO: change
|
|
if (value == null) {
|
|
// TODO: change
|
|
return;
|
|
}
|
|
|
|
Item item = new Item(value);
|
|
if (isEmpty()) {
|
|
setFront(item);
|
|
setBack(item);
|
|
} else {
|
|
item.setPrev(getBack());
|
|
getBack().setNext(item);
|
|
setBack(item);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the value of the front item in the deque, or null if the deque is
|
|
* empty.
|
|
*
|
|
* @return The value of the front item in the deque, or null if the deque is
|
|
* empty.
|
|
*/
|
|
public T peekFirst() {
|
|
if (isEmpty()) {
|
|
return null;
|
|
} else {
|
|
return getFront().getValue();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the value of the back item in the deque, or null if the deque is
|
|
* empty.
|
|
*
|
|
* @return The value of the back item in the deque, or null if the deque is
|
|
* empty.
|
|
*/
|
|
public T peekLast() {
|
|
if (isEmpty()) {
|
|
return null;
|
|
} else {
|
|
return getBack().getValue();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removes and returns the front item in the deque, or null if the deque is
|
|
* empty.
|
|
*
|
|
* @return The value of the removed front item, or null if the deque is empty.
|
|
*/
|
|
public T pollFirst() {
|
|
if (isEmpty()) {
|
|
return null;
|
|
} else {
|
|
T value = getFront().getValue();
|
|
setFront(getFront().getNext());
|
|
if (getFront() == null) {
|
|
setBack(null);
|
|
}
|
|
return value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removes and returns the back item in the deque, or null if the deque is
|
|
* empty.
|
|
*
|
|
* @return The value of the removed back item, or null if the deque is empty.
|
|
*/
|
|
public T pollLast() {
|
|
if (isEmpty()) {
|
|
return null;
|
|
} else {
|
|
T value = getBack().getValue();
|
|
setBack(getBack().getPrev());
|
|
if (getBack() == null) {
|
|
setFront(null);
|
|
} else {
|
|
getBack().setNext(null);
|
|
}
|
|
return value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the value of the first item in the deque, or throws an exception if
|
|
* the deque is empty.
|
|
*
|
|
* @return The value of the first item in the deque.
|
|
* @throws NoSuchElementException if the deque is empty.
|
|
*/
|
|
public Object getFirst() { // TODO: change
|
|
// TODO: add check
|
|
return getFront().getValue();
|
|
}
|
|
|
|
/**
|
|
* Returns the value of the last item in the deque, or throws an exception if
|
|
* the deque is empty.
|
|
*
|
|
* @return The value of the last item in the deque.
|
|
* @throws NoSuchElementException if the deque is empty.
|
|
*/
|
|
public Object getLast() { // TODO: change
|
|
// TODO: add check
|
|
return getBack().getValue();
|
|
}
|
|
|
|
/**
|
|
* Returns the number of items in the deque.
|
|
*
|
|
* @return The number of items in the deque.
|
|
*/
|
|
public int size() {
|
|
int count = 0;
|
|
Item<T> current = getFront();
|
|
while (current != null) {
|
|
count++;
|
|
current = current.getNext();
|
|
}
|
|
return count;
|
|
}
|
|
|
|
/**
|
|
* Checks if the deque is empty.
|
|
*
|
|
* @return true if the deque is empty, false otherwise.
|
|
*/
|
|
public boolean isEmpty() {
|
|
return getFront() == null && getBack() == null;
|
|
}
|
|
|
|
/**
|
|
* Returns a string representation of the deque.
|
|
*/
|
|
@Override
|
|
public String toString() {
|
|
StringBuilder sb = new StringBuilder();
|
|
Item<T> current = getFront();
|
|
sb.append("[");
|
|
while (current != null) {
|
|
sb.append(current.getValue());
|
|
current = current.getNext();
|
|
if (current != null) {
|
|
sb.append(", ");
|
|
}
|
|
}
|
|
sb.append("]");
|
|
return sb.toString();
|
|
}
|
|
}
|