import java.util.NoSuchElementException;
public interface Deque extends Queue{
boolean offerFirst(Integer e);
Integer peekFirst();
Integer pollFirst();
boolean offerLast(Integer e);
Integer peekLast();
Integer pollLast();
int size();
default void addFirst(Integer e) {
if (offerFirst(e) == false) {
throw new IllegalStateException();
}
}
default Integer getFirst() {
Integer e = peekFirst();
if (e == null) {
throw new NoSuchElementException();
}
return e;
}
default Integer removeFirst() {
Integer e = pollFirst();
if (e == null) {
throw new NoSuchElementException();
}
return e;
}
default void addLast(Integer e) {
if (offerLast(e) == false) {
throw new IllegalStateException();
}
}
default Integer getLast() {
Integer e = peekLast();
if (e == null) {
throw new NoSuchElementException();
}
return e;
}
default Integer removeLast() {
Integer e = pollLast();
if (e == null) {
throw new NoSuchElementException();
}
return e;
}
default boolean offer(Integer e) {
return offerLast(e);
}
default Integer peek() {
return peekFirst();
}
default Integer poll() {
return pollFirst();
}
default void push(Integer e) {
addFirst(e);
}
default Integer pop() {
return removeFirst();
}
}
import java.util.NoSuchElementException;
public interface Queue {
boolean offer(Integer e);
Integer peek();
Integer poll();
default boolean add(Integer e) {
if (offer(e) == false) {
throw new IllegalStateException();
}
return true;
}
default Integer element() {
Integer e = peek();
if (e == null) {
throw new NoSuchElementException();
}
return e;
}
default Integer remove() {
Integer e = poll();
if (e == null) {
throw new NoSuchElementException();
}
return e;
}
}
public class Node {
Node pre;
Node next;
int data;
public Node(int data) {
this.data = data;
}
}
public class LinkedList implements Deque {
Node head;
Node tail;
int size;
@Override
public boolean offerFirst(Integer e) {
Node node=new Node(e);
if(size==0){
this.head=this.tail=node;
}else{
node.next=this.head;
this.head.pre=node;
this.head=node;
}
this.size++;
return true;
}
@Override
public Integer peekFirst() {
if(size==0){
return null;
}
return this.head.data;
}
@Override
public Integer pollFirst() {
if (size==0){
return null;
}
Node cur=this.head;
Integer e=this.head.data;
this.head=cur.next;
cur.next=null;
this.head.pre=null;
this.size--;
return e;
}
@Override
public boolean offerLast(Integer e) {
Node node=new Node(e);
if (size==0){
this.head=this.tail=node;
}else{
node.pre=this.tail;
this.tail.next=node;
this.tail=node;
}
this.size++;
return true;
}
@Override
public Integer peekLast() {
if (size==0){
return null;
}
return this.tail.data;
}
@Override
public Integer pollLast() {
if (size==0){
return null;
}
Node cur=this.tail;
Integer e=this.tail.data;
this.tail=cur.pre;
this.tail.next=null;
cur.pre=null;
this.size--;
return e;
}
@Override
public int size() {
return size;
}
public boolean isEmpty(){
return size==0;
}
}
public class Test {
public static void main(String[] args) {
LinkedList linkedList=new LinkedList();
linkedList.offerFirst(1);
linkedList.offerFirst(2);
linkedList.offerFirst(3);
linkedList.offerFirst(4);
linkedList.offerFirst(5);
System.out.println(linkedList.peekFirst());
System.out.println(linkedList.pollFirst());
System.out.println(linkedList.peekFirst());
linkedList.offerLast(6);
System.out.println(linkedList.peekLast());
System.out.println(linkedList.pollLast());
System.out.println(linkedList.peekLast());
}
}