闲来无事,想联系一下对链表的操作,上一次写链表还是用C++呢,都已经是两年前的事情了,现在想用Java试一下,于是自己引入List接口,come on !
写完以后(没有关于迭代器的部分),对照源码看了一下,我去,差距有点大
public class MyLinkedList<E> implements List<E>{
private Node<E> header = null ;
private Node<E> tail = null ;
private int size = 0 ;
public int size() {
return size ;
}
public boolean isEmpty() {
return header == null;
}
public boolean contains(Object o) {
return (indexOf(o) == -1);
}
public Iterator<E> iterator() {
// TODO Auto-generated method stub
return null;
}
public Object[] toArray() {
Object[] objs = new Object[size];
int count = 0 ;
for (Node<E> i = header; i != null; i = i.next) {
objs[count] = i.item ;
}
return objs;
}
/**
* 返回指定类型的数组
*/
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
T[] arr = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size) ;
int i = 0 ;
for (Node<E> temp = header; temp != null; temp = temp.next) {
arr[i++] = (T) temp.item ;
}
if(a.length>size)
a[size] = null ;
return arr;
}
public boolean add(E e) {
Node<E> newNode = new Node<E>(tail , e, null);
if(header == null){
header = tail = new Node<E>(null,e,null);
size++ ;
return true ;
}
tail = tail.next = newNode;
size++;
return true;
}
public boolean remove(Object o) {
if(size == 0)
return false ;
if(o == null){
for (Node<E> node = header ; node != null ; node = node.next) {
if(node.item == null){
removeNode(node);
return true ;
}
}
}else{
for (Node<E> node = header ; node != null ; node = node.next) {
if(o.equals(node.item)){
removeNode(node);
return true ;
}
}
}
return false;
}
/**
* 删除链表中的节点
* @param node
*/
private void removeNode(Node<E> node) {
if (node.pre == null) {
header = node.next;
}else{
node.pre.next = node.next;
}
if(node.next == null){
tail = node.pre ;
}else{
node.next.pre = node.pre ;
}
size-- ;
}
public boolean containsAll(Collection<?> c) {
for (int i = 0; i < c.size(); i++) {
Iterator<?> iter = c.iterator() ;
while(iter.hasNext()){
if(!contains(iter.next()))
return false ;
}
}
return true ;
}
public boolean addAll(Collection<? extends E> c) {
if(c.size() == 0)
return false ;
for (int i = 0; i < c.size(); i++) {
Iterator<? extends E> iter = c.iterator() ;
while(iter.hasNext()){
add(iter.next());
}
}
return true;
}
public boolean addAll(int index, Collection<? extends E> c) {
if(c.size() == 0)
return false ;
for (int i = 0; i < c.size(); i++) {
Iterator<? extends E> iter = c.iterator() ;
while(iter.hasNext()){
add(index+i ,iter.next());
}
}
return false;
}
public boolean removeAll(Collection<?> c) {
if((c == null)||(c.size() == 0))
return false ;
for (int i = 0; i < c.size(); i++) {
Iterator<?> iter = c.iterator() ;
while(iter.hasNext()){
remove(iter.next());
}
}
return false;
}
/**
* 求两个集合的交集,如果不存在返回 false
*/
public boolean retainAll(Collection<?> c) {
if((c == null)||(c.size() == 0))
return false ;
boolean flag = false ;
Iterator<?> iter = this.iterator() ;
while(iter.hasNext()){
if(!c.contains(iter.next())){
iter.remove();
flag = true ;
}
}
return flag ;
}
public void clear() {
header = tail = null ;
size = 0 ;
}
public E get(int index) {
checkBounds(index);
Node<E> node = header ;
for (int i = 0; i < index; i++) {
node = node.next ;
}
return node.item;
}
/**
* 数据替换
*/
public E set(int index, E element) {
checkBounds(index);
Node<E> oldValue = header ;
for (int i = 0; i < index; i++) {
oldValue = oldValue.next ;
}
E oldItem = oldValue.item ;
oldValue.item = element ;
return oldItem;
}
public void add(int index, E element) {
checkAddBounds(index);
Node<E> node = header ;
for (int i = 0; i < index; i++) {
node = node.next ;
}
if(size == 0){
header = tail = new Node<E>(null, element, null);
}else{
if(node == null){
tail = tail .next = new Node<E>(tail,element,node);
}else{
Node<E> newNode = new Node<E>(node.pre,element,node);
if(node.pre == null){
header = node.pre = newNode ;
}else{
node.pre = node.pre.next = newNode ;
}
}
}
size++ ;
}
private void checkAddBounds(int index) {
if((index < 0)||(index > size))
throw new IndexOutOfBoundsException();
}
private void checkBounds(int index) {
if((index < 0)||(index >= size))
throw new IndexOutOfBoundsException();
}
public E remove(int index) {
checkBounds(index);
Node<E> node = header ;
for (int i = 0; i < index; i++) {
node = node.next ;
}
removeNode(node);
return node.item;
}
public int indexOf(Object o) {
int i = 0 ;
for (Node<E> x = header; x != null ; x = x.next , i++) {
if(o == null){
if(x.item == null)
return i ;
}else{
if(o.equals(x.item))
return i ;
}
}
return -1;
}
public int lastIndexOf(Object o) {
int i = 0 ;
for (Node<E> x = tail; x != null ; x = x.pre , i++) {
if(o == null){
if(x.item == null)
return size - i ;
}else{
if(o.equals(x.item))
return size - i ;
}
}
return -1;
}
public ListIterator<E> listIterator() {
// TODO 与迭代器相关的内容
return null;
}
public ListIterator<E> listIterator(int index) {
// TODO 与迭代器相关的内容
return null;
}
public List<E> subList(int fromIndex, int toIndex) {
if( (fromIndex < 0) || (toIndex > size) )
throw new IndexOutOfBoundsException();
if( fromIndex > toIndex)
throw new IllegalArgumentException();
MyLinkedList<E> sub = new MyLinkedList<E>() ;
int i = 0 ;
Node<E> node = header ;
for (; i < fromIndex; i++) {
node = node.next ;
}
for (; i< toIndex; i++) {
sub.add(node.item);
node = node.next ;
}
return sub;
}
private static class Node<E>{
Node<E> pre ;
E item ;
Node<E> next ;
public Node(Node<E> pre , E e, Node<E> next) {
this.pre = pre ;
this.item = e ;
this.next = next ;
}
}
}