ajout crypto
This commit is contained in:
105
DEV3.2/ancien_controle/SimpleDeque.java
Normal file
105
DEV3.2/ancien_controle/SimpleDeque.java
Normal file
@@ -0,0 +1,105 @@
|
||||
public class SimpleDeque<T> implements MinimalDeque<T> {
|
||||
private static class Node<T> {
|
||||
T value;
|
||||
Node<T> next;
|
||||
Node<T> prev;
|
||||
|
||||
Node(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Node<T> head;
|
||||
private Node<T> tail;
|
||||
private int size;
|
||||
|
||||
public SimpleDeque() {
|
||||
head = null;
|
||||
tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFirst(T element) {
|
||||
Node<T> newNode = new Node<>(element);
|
||||
if (isEmpty()) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
newNode.next = head;
|
||||
head.prev = newNode;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLast(T element) {
|
||||
Node<T> newNode = new Node<>(element);
|
||||
if (isEmpty()) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
newNode.prev = tail;
|
||||
tail.next = newNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T removeFirst() {
|
||||
if (isEmpty()) {
|
||||
throw new IllegalStateException("Deque is empty");
|
||||
}
|
||||
T value = head.value;
|
||||
head = head.next;
|
||||
if (head != null) {
|
||||
head.prev = null;
|
||||
} else {
|
||||
tail = null;
|
||||
}
|
||||
size--;
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T removeLast() {
|
||||
if (isEmpty()) {
|
||||
throw new IllegalStateException("Deque is empty");
|
||||
}
|
||||
T value = tail.value;
|
||||
tail = tail.prev;
|
||||
if (tail != null) {
|
||||
tail.next = null;
|
||||
} else {
|
||||
head = null;
|
||||
}
|
||||
size--;
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T peekFirst() {
|
||||
if (isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return head.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T peekLast() {
|
||||
if (isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return tail.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user