public class Deque<T> {
public class DequeNode<T> {
public T value;
public DequeNode<T> pre;
public DequeNode<T> next;
}
public int Lenght;
public DequeNode<T> Front = new DequeNode<T>();
public DequeNode<T> Back = new DequeNode<T>();
public Deque(){
Clear();
}
public void PushBack(T value) {
DequeNode<T> node = new DequeNode<T>() {
value = value,
pre = Back.pre,
next = Back
};
Back.pre = node;
node.pre.next = node;
Lenght++;
}
public void PushFront(T value) {
DequeNode<T> node = new DequeNode<T>() {
value = value,
pre = Front,
next = Front.next
};
Front.next = node;
node.next.pre = node;
Lenght++;
}
public T PopBack() {
if (Lenght <= 0) {
return default(T);
}
DequeNode<T> node = Back.pre;
Back.pre = node.pre;
node.pre.next = Back;
Lenght--;
return node.value;
}
public T PopFront() {
if (Lenght <= 0) {
return default(T);
}
DequeNode<T> node = Front.next;
Front.next = node.next;
node.next.pre = Front;
Lenght--;
return node.value;
}
public T PeekBack() {
if (Lenght <= 0) {
return default(T);
}
return Back.pre.value;
}
public T PeekFront() {
if (Lenght <= 0) {
return default(T);
}
return Front.next.value;
}
public void Clear() {
Lenght = 0;
Front.next = Back;
Front.pre = null;
Back.next = null;
Back.pre = Front;
}
}
08-26
1684
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)