链表学习笔记

来源:互联网 发布:守望先锋性能数据rtt 编辑:程序博客网 时间:2024/05/16 09:27
package Understand;

//链表

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;


/**
 * @author YouYuan
 * @contact 1265161633@qq.com
 * @date 2016年7月13日 上午8:58:08
 * @descript 实现双向链表
 * @param <E>
 */
public class MyLinkedList<E> implements List<E> {
/**
* 节点类
* @author YouYuan
* @param <T>
*/
class Node<T> {
Node preNode;// 上一个节点
T data;// 存储的数据
Node nextNode;// 下一个节点


public Node() {
this(null);
}


public Node(T e) {
preNode = null;
data = e;
nextNode = null;
}
}

private int size;// 链表长度
private Node<E> head, end;// 头节点,尾节点(哑元节点)


public MyLinkedList() {
head = new Node();// 初始化
end = new Node();
head.nextNode = end;
end.preNode = head;
size = 0;
}


/**
* 向链表尾部添加数据
*/
@Override
public boolean add(E e) {
Node<E> temp = new Node<E>(e);// 将添加的数据保存到新节点
Node preNode = end.preNode;//获取end的上一个节点
preNode.nextNode = temp;//将上一个节点的下一个指针指向新的节点
temp.preNode = preNode;//新节点的上一个指针指向上一个节点
temp.nextNode = end;//新节点的下一个指针指向end
end.preNode = temp;//end的上一个节点指向新节点
size++;// 链表长度+1
return true;
}


/**
* 清空链表
*/
@Override
public void clear() {
head = new Node<E>();// 初始化
end = new Node<E>();
head.nextNode = end;
end.preNode = head;
size = 0;
}


/**
* 判断链表是否包含指定对象
*/
@Override
public boolean contains(Object o) {
for (Node<E> n = head.nextNode; n != null && n != end; n = n.nextNode) {
if (n.data == o || (n.data != null && n.data.equals(o))) {
return true;
}
}
return false;
}


/**
* 判断链表是否为空
*/
@Override
public boolean isEmpty() {
return size == 0;
}


/**
* 删除链表中指定的对象
*/
@Override
public boolean remove(Object o) {
for (Node<E> n = head.nextNode; n != null && n != end; n = n.nextNode) {
if (n.data == o || (n.data != null && n.data.equals(o))) {
// 删除元素
Node<E> pre = n.preNode;
Node<E> next = n.nextNode;
pre.nextNode = next;
n = null;
size--;
return true;
}
}
return false;
}


/**
* 返回链表的迭代器
*/
@Override
public Iterator iterator() {
Iterator it = new Iterator() {
Node<E> current = head;


@Override
public boolean hasNext() {
return current.nextNode != null && current.nextNode != end;
}


@Override
public E next() {
current = current.nextNode;
return current.data;
}
};
return it;
}


/**
* 返回链表大小
*/
@Override
public int size() {
return size;
}


/**
* 将链表的所有数据以Object数组的形式返回
*/
@Override
public Object[] toArray() {
Object[] result = new Object[size];
int i = 0;
for (Node<E> n = head.nextNode; n != null && n != end; n = n.nextNode) {
result[i++] = n.data;
}
return result;
}


// ////////////////////////下面的方法不做要求(可以不实现)////////////////////////////////////
@Override
public List subList(int fromIndex, int toIndex) {
return null;
}


@Override
public int indexOf(Object o) {
return 0;
}


@Override
public void add(int index, Object element) {
}


@Override
public Object[] toArray(Object[] a) {
return null;
}


@Override
public E get(int index) {
return null;
}


@Override
public boolean containsAll(Collection c) {
return false;
}


@Override
public E remove(int index) {
return null;
}


@Override
public boolean addAll(Collection c) {
return false;
}


@Override
public boolean removeAll(Collection c) {
return false;
}


@Override
public int lastIndexOf(Object o) {
return 0;
}


@Override
public ListIterator listIterator() {
return null;
}


@Override
public ListIterator listIterator(int index) {
return null;
}


@Override
public boolean retainAll(Collection c) {
return false;
}


@Override
public Object set(int index, Object element) {
return null;
}


@Override
public boolean addAll(int index, Collection c) {
return false;
}


}


//测试类

package Understand;


import java.util.Iterator;
import java.util.List;


import org.junit.Assert;
import org.junit.Test;


/**
 * @author YouYuan
 * @contact 1265161633@qq.com
 * @date 2016年7月13日 上午9:30:43
 * @descript
 */
public class TestMyLinkedList {


    /**
     * 测试链表
     */
    @Test
    public void testList() {
        List<String> list = new MyLinkedList<String>();
        Assert.assertEquals(list.isEmpty(), true);
        Assert.assertEquals(list.add("a"), true);
        Assert.assertEquals(list.add("b"), true);
        Assert.assertEquals(list.add("c"), true);
        Assert.assertEquals(list.add(null), true);
        Iterator it = list.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
        System.out.println();
        Object[] array = list.toArray();
        for (Object s : array) {
            System.out.println("arr:" + s);
        }
        Assert.assertEquals(list.contains("a"), true);
        Assert.assertEquals(list.contains(null), true);
        Assert.assertEquals(list.size(), 4);
        Assert.assertEquals(list.remove("c"), true);
        Assert.assertEquals(list.remove(null), true);
        Assert.assertEquals(list.remove(new Integer(3)), false);
        Assert.assertEquals(list.size(), 2);
        Assert.assertEquals(list.contains("c"), false);
        Assert.assertEquals(list.contains("f"), false);
        list.clear();
        Assert.assertEquals(list.contains("a"), false);
        Assert.assertEquals(list.size(), 0);
        Assert.assertEquals(list.add("d"), true);
        Assert.assertEquals(list.contains("d"), true);
    }


}




0 0