数据结构与算法分析作业之自己实现Java双链表 发表于 2018-05-27 | 更新于 2018-05-27 | 分类于 数据结构与算法分析:Java语言描述 | 浏览 次 字数统计: 795 自己实现的Java双链表 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245package com.hegongshan.collections;import java.util.NoSuchElementException;/** * 双链表 * @author hegongshan https://www.hegongshan.com * @param <E> */public class DoubleLinkedList<E> { private int size = 0; private Node<E> first; private Node<E> last; private static class Node<E> { Node<E> prev; E data; Node<E> next; Node(Node<E> prev, E data, Node<E> next) { super(); this.prev = prev; this.data = data; this.next = next; } } public DoubleLinkedList(){ } public int size() { return size; } public boolean isEmpty() { return first == null; } public boolean add(E e) { linkLast(e); return true; } public void add(int index,E e) { checkPositionIndex(index); if(index == size) { linkLast(e); } else { linkBefore(e,node(index)); } } public E get(int index) { checkElementIndex(index); return node(index).data; } public E getFirst() { if(first == null) { throw new NoSuchElementException(); } return first.data; } public E getLast() { if(last == null) { throw new NoSuchElementException(); } return last.data; } public E set(int index,E e) { checkElementIndex(index); Node<E> node = node(index); E oldValue = node.data; node.data = e; return oldValue; } public E remove(int index) { checkElementIndex(index); Node<E> node = node(index); node.prev.next = node.next; node.next.prev = node.prev; E e = node.data; node.prev = null; node.next = null; node.data = null; size--; return e; } public E removeFirst() { if(first == null) { throw new NoSuchElementException(); } Node<E> node = first; E e = node.data; first = node.next; node.next = null; node.data = null; if(first == null) { last = null; } else { first.prev = null; } size--; return e; } public E removeLast() { if(last == null) { throw new NoSuchElementException(); } Node<E> node = last; E e = node.data; last = node.prev; node.prev = null; node.data = null; if(last == null) { first = null; } else { last.next = null; } size--; return e; } public void clear() { for(Node<E> node = first;node != null;) { Node<E> next = node.next; node.prev = null; node.data = null; node.next = null; node = next; } first = last = null; size = 0; } public void reverse() { Node<E> temp = first; first = last; last = temp; } public boolean contains(Object obj) { return indexOf(obj) != -1; } public int indexOf(Object obj) { int index = 0; if(obj == null) { for(Node<E> node = first;node != null;node = node.next) { if(node.data == null) { return index; } index++; } } else { for(Node<E> node = first;node != null;node = node.next) { if(obj.equals(node.data)) { return index; } index++; } } return -1; } public int lastIndexOf(Object obj) { int index = size - 1; if(obj == null) { for(Node<E> node = last;node != null;node = node.prev) { if(node.data == null) { return index; } index--; } } else { for(Node<E> node = last;node != null;node = node.prev) { if(obj.equals(node.data)) { return index; } index--; } } return -1; } public void linkFirst(E e) { Node<E> node = new Node<>(null,e,first); first.prev = node; first = node; size++; } private void linkBefore(E e,Node<E> node) { Node<E> newNode = new Node<>(node.prev,e,node); node.prev.next = newNode; node.prev = newNode; size++; } public void linkLast(E e) { if(size == 0) { first = new Node<>(null,e,null); last = first; size++; return ; } Node<E> node = new Node<E>(last,e,null); last.next = node; last = node; size++; } private Node<E> node(int index) { checkElementIndex(index); if(index < (size >> 1)) { Node<E> node = first; for(int i = 0 ; i < index ; i++) { node = node.next; } return node; } else { Node<E> node = last; for(int i = size - 1 ; i > index ; i--) { node = node.prev; } return node; } } private void checkElementIndex(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("index:" + index + ",size:" + size); } } // 可以添加结点的位置,索引从0开始到size private void checkPositionIndex(int index) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException("index:" + index + ",size:" + size); } }} ----------本文结束感谢您的阅读---------- 坚持原创技术分享,您的支持将鼓励我继续创作! 打赏 微信支付 支付宝 本文作者: Gongshan He 本文链接: https://www.hegongshan.com/2018/05/27/data-structure-and-algorithm-analysis-homework-doublelinkedlist-in-java/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!