Files
Testate/WS2526/Testat 2b/Deque.java
T

188 lines
4.3 KiB
Java

/**
* Represents a double-ended queue (deque) that allows adding and removing items
* from both ends.
*/
public class Deque {
/**
* The front item of the deque.
*/
private Item front;
/**
* The back item of the deque.
*/
private Item 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 getFront() {
return front;
}
/**
* Returns the back item in the deque.
*
* @return The back item in the deque.
*/
private Item getBack() {
return back;
}
/**
* Sets the front item in the deque.
*
* @param front The item to set as the front.
*/
private void setFront(Item front) {
this.front = front;
}
/**
* Sets the back item in the deque.
*
* @param back The item to set as the back.
*/
private void setBack(Item 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(String value) {
// TODO: implement this method
}
/**
* 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(String value) {
if (value == null || value.isEmpty()) {
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 String 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 String peekLast() {
// TODO: implement this method
return "Not implemented yet";
}
/**
* 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 String pollFirst() {
// TODO: implement this method
return "Not implemented yet";
}
/**
* 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 String pollLast() {
if (isEmpty()) {
return null;
} else {
String value = getBack().getValue();
setBack(getBack().getPrev());
if (getBack() == null) {
setFront(null);
} else {
getBack().setNext(null);
}
return value;
}
}
/**
* Returns the number of items in the deque.
*
* @return The number of items in the deque.
*/
public int size() {
// TODO: implement this method
return -42;
}
/**
* 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 current = getFront();
sb.append("[");
while (current != null) {
sb.append(current.getValue());
current = current.getNext();
if (current != null) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}
}