package cn.net.pikachu.other; import org.jetbrains.annotations.NotNull; import java.util.*; /** * Created by pikachu on 17-2-19. */ public class MyLinkedList<E> extends AbstractSequentialList<E> implements Deque<E> { private Node<E> head = new Node<E>(); private Node<E> tail = head; private int curSize = 0; /** * Returns a list iterator over the elements in this list (in proper * sequence). * * @param index index of first element to be returned from the list * iterator (by a call to the <code>next</code> method) * @return a list iterator over the elements in this list (in proper * sequence) * @throws IndexOutOfBoundsException {@inheritDoc} */ @Override public ListIterator<E> listIterator(int index) { // TODO throw new UnsupportedOperationException(); } /** * Inserts the specified element at the front of this deque if it is * possible to do so immediately without violating capacity restrictions, * throwing an {@code IllegalStateException} if no space is currently * available. When using a capacity-restricted deque, it is generally * preferable to use method {@link #offerFirst}. * * @param e the element to add * @throws IllegalStateException if the element cannot be added at this * time due to capacity restrictions * @throws ClassCastException if the class of the specified element * prevents it from being added to this deque * @throws NullPointerException if the specified element is null and this * deque does not permit null elements * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ @Override public void addFirst(E e) { head.next= new Node<E>(e,head.next,head); curSize++; } /** * Inserts the specified element at the end of this deque if it is * possible to do so immediately without violating capacity restrictions, * throwing an {@code IllegalStateException} if no space is currently * available. When using a capacity-restricted deque, it is generally * preferable to use method {@link #offerLast}. * <p> * <p>This method is equivalent to {@link #add}. * * @param e the element to add * @throws IllegalStateException if the element cannot be added at this * time due to capacity restrictions * @throws ClassCastException if the class of the specified element * prevents it from being added to this deque * @throws NullPointerException if the specified element is null and this * deque does not permit null elements * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ @Override public void addLast(E e) { Node<E> t = new Node<E>(e,null,tail); tail.next=t; tail=t; curSize++; } /** * Inserts the specified element at the front of this deque unless it would * violate capacity restrictions. When using a capacity-restricted deque, * this method is generally preferable to the {@link #addFirst} method, * which can fail to insert an element only by throwing an exception. * * @param e the element to add * @return {@code true} if the element was added to this deque, else * {@code false} * @throws ClassCastException if the class of the specified element * prevents it from being added to this deque * @throws NullPointerException if the specified element is null and this * deque does not permit null elements * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ @Override public boolean offerFirst(E e) { throw new UnsupportedOperationException(); } /** * Inserts the specified element at the end of this deque unless it would * violate capacity restrictions. When using a capacity-restricted deque, * this method is generally preferable to the {@link #addLast} method, * which can fail to insert an element only by throwing an exception. * * @param e the element to add * @return {@code true} if the element was added to this deque, else * {@code false} * @throws ClassCastException if the class of the specified element * prevents it from being added to this deque * @throws NullPointerException if the specified element is null and this * deque does not permit null elements * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ @Override public boolean offerLast(E e) { throw new UnsupportedOperationException(); } /** * Retrieves and removes the first element of this deque. This method * differs from {@link #pollFirst pollFirst} only in that it throws an * exception if this deque is empty. * * @return the head of this deque * @throws NoSuchElementException if this deque is empty */ @Override public E removeFirst() { if (curSize==0){ throw new NoSuchElementException(); } E e = head.e; head=head.next; curSize--; return e; } /** * Retrieves and removes the last element of this deque. This method * differs from {@link #pollLast pollLast} only in that it throws an * exception if this deque is empty. * * @return the tail of this deque * @throws NoSuchElementException if this deque is empty */ @Override public E removeLast() { if (curSize==0){ throw new NoSuchElementException(); } // 怎么去去掉最后一个呢? Node<E> t=head; while (t.next!=tail){ t=t.next; } tail=t; curSize--; return t.next.e; } /** * Retrieves and removes the first element of this deque, * or returns {@code null} if this deque is empty. * * @return the head of this deque, or {@code null} if this deque is empty */ @Override public E pollFirst() { throw new UnsupportedOperationException(); } /** * Retrieves and removes the last element of this deque, * or returns {@code null} if this deque is empty. * * @return the tail of this deque, or {@code null} if this deque is empty */ @Override public E pollLast() { throw new UnsupportedOperationException(); } /** * Retrieves, but does not remove, the first element of this deque. * <p> * This method differs from {@link #peekFirst peekFirst} only in that it * throws an exception if this deque is empty. * * @return the head of this deque * @throws NoSuchElementException if this deque is empty */ @Override public E getFirst() { return head.e; } /** * Retrieves, but does not remove, the last element of this deque. * This method differs from {@link #peekLast peekLast} only in that it * throws an exception if this deque is empty. * * @return the tail of this deque * @throws NoSuchElementException if this deque is empty */ @Override public E getLast() { return tail.e; } /** * Retrieves, but does not remove, the first element of this deque, * or returns {@code null} if this deque is empty. * * @return the head of this deque, or {@code null} if this deque is empty */ @Override public E peekFirst() { throw new UnsupportedOperationException(); } /** * Retrieves, but does not remove, the last element of this deque, * or returns {@code null} if this deque is empty. * * @return the tail of this deque, or {@code null} if this deque is empty */ @Override public E peekLast() { throw new UnsupportedOperationException(); } /** * Removes the first occurrence of the specified element from this deque. * If the deque does not contain the element, it is unchanged. * More formally, removes the first element {@code e} such that * <tt>(o==null ? e==null : o.equals(e))</tt> * (if such an element exists). * Returns {@code true} if this deque contained the specified element * (or equivalently, if this deque changed as a result of the call). * * @param o element to be removed from this deque, if present * @return {@code true} if an element was removed as a result of this call * @throws ClassCastException if the class of the specified element * is incompatible with this deque * (<a href="Collection.html#optional-restrictions">optional</a>) * @throws NullPointerException if the specified element is null and this * deque does not permit null elements * (<a href="Collection.html#optional-restrictions">optional</a>) */ @Override public boolean removeFirstOccurrence(Object o) { Node<E> t = head; while (t.next!=null){ if (Objects.equals(t.next.e,o)){ t.next=t.next.next; return true; } } return false; } /** * Removes the last occurrence of the specified element from this deque. * If the deque does not contain the element, it is unchanged. * More formally, removes the last element {@code e} such that * <tt>(o==null ? e==null : o.equals(e))</tt> * (if such an element exists). * Returns {@code true} if this deque contained the specified element * (or equivalently, if this deque changed as a result of the call). * * @param o element to be removed from this deque, if present * @return {@code true} if an element was removed as a result of this call * @throws ClassCastException if the class of the specified element * is incompatible with this deque * (<a href="Collection.html#optional-restrictions">optional</a>) * @throws NullPointerException if the specified element is null and this * deque does not permit null elements * (<a href="Collection.html#optional-restrictions">optional</a>) */ @Override public boolean removeLastOccurrence(Object o) { return false; } /** * Inserts the specified element into the queue represented by this deque * (in other words, at the tail of this deque) if it is possible to do so * immediately without violating capacity restrictions, returning * {@code true} upon success and {@code false} if no space is currently * available. When using a capacity-restricted deque, this method is * generally preferable to the {@link #add} method, which can fail to * insert an element only by throwing an exception. * <p> * <p>This method is equivalent to {@link #offerLast}. * * @param e the element to add * @return {@code true} if the element was added to this deque, else * {@code false} * @throws ClassCastException if the class of the specified element * prevents it from being added to this deque * @throws NullPointerException if the specified element is null and this * deque does not permit null elements * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ @Override public boolean offer(E e) { return false; } /** * Retrieves and removes the head of the queue represented by this deque * (in other words, the first element of this deque). * This method differs from {@link #poll poll} only in that it throws an * exception if this deque is empty. * <p> * <p>This method is equivalent to {@link #removeFirst()}. * * @return the head of the queue represented by this deque * @throws NoSuchElementException if this deque is empty */ @Override public E remove() { return null; } /** * Retrieves and removes the head of the queue represented by this deque * (in other words, the first element of this deque), or returns * {@code null} if this deque is empty. * <p> * <p>This method is equivalent to {@link #pollFirst()}. * * @return the first element of this deque, or {@code null} if * this deque is empty */ @Override public E poll() { return null; } /** * Retrieves, but does not remove, the head of the queue represented by * this deque (in other words, the first element of this deque). * This method differs from {@link #peek peek} only in that it throws an * exception if this deque is empty. * <p> * <p>This method is equivalent to {@link #getFirst()}. * * @return the head of the queue represented by this deque * @throws NoSuchElementException if this deque is empty */ @Override public E element() { return null; } /** * Retrieves, but does not remove, the head of the queue represented by * this deque (in other words, the first element of this deque), or * returns {@code null} if this deque is empty. * <p> * <p>This method is equivalent to {@link #peekFirst()}. * * @return the head of the queue represented by this deque, or * {@code null} if this deque is empty */ @Override public E peek() { return null; } /** * Pushes an element onto the stack represented by this deque (in other * words, at the head of this deque) if it is possible to do so * immediately without violating capacity restrictions, throwing an * {@code IllegalStateException} if no space is currently available. * <p> * <p>This method is equivalent to {@link #addFirst}. * * @param e the element to push * @throws IllegalStateException if the element cannot be added at this * time due to capacity restrictions * @throws ClassCastException if the class of the specified element * prevents it from being added to this deque * @throws NullPointerException if the specified element is null and this * deque does not permit null elements * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ @Override public void push(E e) { } /** * Pops an element from the stack represented by this deque. In other * words, removes and returns the first element of this deque. * <p> * <p>This method is equivalent to {@link #removeFirst()}. * * @return the element at the front of this deque (which is the top * of the stack represented by this deque) * @throws NoSuchElementException if this deque is empty */ @Override public E pop() { return null; } @Override public int size() { return 0; } /** * Returns an iterator over the elements in this deque in reverse * sequential order. The elements will be returned in order from * last (tail) to first (head). * * @return an iterator over the elements in this deque in reverse * sequence */ @NotNull @Override public Iterator<E> descendingIterator() { return null; } } class Node<E>{ public E e; public Node<E> next; public Node<E> prev; public Node() { } public Node(E e) { this.e = e; } public Node(E e, Node<E> next, Node<E> prev) { this.e = e; this.next = next; this.prev = prev; } }