数据结构03之链表

链表和数组相比,数组的插入慢、删除慢、查询快!

当然链表这种数据结构的插入和删除的速度很快不需要遍历,但是查询就慢了,因为不像数组一样是有序的通过下标示就可以访问,所以查询的时候只能通过遍历来完成!

数组是在内存中开辟一块连续的区域读取也方便,而链表则是分布在不同的内存块中通过链来关联所以访问也受限制,从本质来说数组是不可变的虽然我们有很多手段让其动态增加长度,而链表的长度和大小是没有限制的! 

简单的链表

class Link<E> { 

    public E data;

    public Link<E> next;

    public Link(E data) {
        this.data = data;
    }
}

class LinkList<E> {

    public Link<E> first;
    //链表中数据项的个数
    public int size;

    public LinkList() {
        first = null;
        size = 0;
    }
    //在表头插入新的数据
    public void insertFirst(E value) {
        Link<E> link = new Link<E>(value);
        link.next = first;
        first = link;
        size++;
    }
    //判断链表是否为空
    public boolean isEmpty() {
        return size == 0;
    }
    //删除表头
    public Link<E> deleteFirst() {
        Link<E> temp = first;
        first = first.next;
        size--;
        return temp;
    }
    //输出链表中的所有数据
    public void display() {
        Link<E> curr = first;
        while (curr != null) {
            System.out.print(curr.data + " ");
            curr = curr.next;
        }
        System.out.println();
    }
    //返回链表中数据项的个数
    public int size() {
        return size;
    }
    //获取从头至尾的第i个数据项
    public Link<E> get(int i) {
        if (i > size() - 1 || i < 0)
            try {
                throw new IndexOutOfBoundsException();
            } catch (Exception e) {
                e.printStackTrace();
            }
        Link<E> curr = first;
        for (int n = 0; n < size(); n++) {
            if (n == i)
                return curr;
            else
                curr = curr.next;
        }
        return null;
    }
    //输出从头至尾的第i个数据项
    public void remove(int i) {
        if (i == 0)
            deleteFirst();
        else if (i == size() - 1)
            get(i - 1).next = null;
        else {
            get(i - 1).next = get(i + 1);
        }
        size--;
    }
}


    public static void main(String[] args) {
        LinkList<Long> ll = new LinkList<Long>();
        for (int i = 0; i < 10; i++) {
            Long value = (long) (Math.random() * 100);
            ll.insertFirst(value);
        }
        ll.display();
        while (!ll.isEmpty()) {
            ll.deleteFirst();
            ll.display();
        }
        System.out.println("Ok");
    }

 我们可以看出来链表是从头部开始往前添加,并且我们只持有最新的头元素,而每一个元素都曾经做过头元素,并且指向下一个元素,所以我们只需要用头元素就可以一直next下去,遍历所有

 

 

有序链表:主要是在插入的时候保持插入的时候要求之前的元素总是大于当前值或者小于当前值!这样在插入的时候就排好序了

class SortedLink {

    public Link<Long> first;

    int size;

    public SortedLink() {
        first = null;
        size = 0;
    }
    //向有序链表中插入数据
    public void insert(long value) {
        Link<Long> newLink = new Link<Long>(value);
        Link<Long> previous = null;
        Link<Long> curr = first;
        while (curr != null && (value > curr.data)) {
            previous = curr;
            curr = curr.next;
        }
        if (previous == null)// 链表为空(在表头插入)
            first = newLink;
        else
            previous.next = newLink;//插入新的节点
        newLink.next = curr;
        size++;
    }
    //删除第一个节点
    public Link<Long> remove() {
        Link<Long> temp = first;
        first = first.next;
        size--;
        return temp;
    }
    //判断链表是否为空
    public boolean isEmpty() {
        return size == 0;
    }
    //输出链表的所有数据
    public void display() {
        Link<Long> curr = first;
        while (curr != null) {
            System.out.print(curr.data + " ");
            curr = curr.next;
        }
        System.out.println();
    }
}

    public static void main(String[] args) {
        SortedLink sl = new SortedLink();
        for (int i = 0; i < 10; i++) {
            sl.insert((long) (Math.random() * 100));
        }
        while (!sl.isEmpty()) {
            sl.remove();
            sl.display();
        }

    } 

 

 

双向链表:之前的只有从头到尾现在再持有从尾到头实现双向循环!

class DoubleLink<E> {

    public Link<E> first;

    public Link<E> last;

    int size;

    @SuppressWarnings("hiding")
    class Link<E> {
        public E data;

        public Link<E> next;// 链表的下一项

        public Link<E> previous;// 链表的前一项

        public Link(E value) {
            this.data = value;
        }
    }

    public DoubleLink() {
        first = null;
        last = null;
        size = 0;
    }

    // 在链表的首部插入一项
    public void insertFirst(E value) {
        Link<E> newLink = new Link<E>(value);
        if (isEmpty())// 如果链表为空则first == last
            last = newLink;
        else
            first.previous = newLink;// 确定原first与newLink的前后关系
        newLink.next = first;
        first = newLink;// 设置新的first值
        size++;
    }

    // 在链表的尾部插入一项
    public void insertLast(E value) {
        Link<E> newLink = new Link<E>(value);
        if (isEmpty())// 如果链表为空则last == first
            first = newLink;
        else {
            last.next = newLink;// 确定原last与newLink的前后关系
            newLink.previous = last;
        }
        last = newLink;// 设置新的last值
        size++;
    }

    // 删除双向链表的表头
    public Link<E> deleteFirst() {
        Link<E> temp = first;
        if (first.next == null)// 链表中只有一项数据
            last = null;
        else
            first.next.previous = null;// 销毁原链表的头部
        first = first.next;
        size--;
        return temp;
    }

    // 删除链表的最后一项
    public Link<E> deleteLast() {
        Link<E> temp = last;
        if (first.next == null)// 链表中只有一项数据
            first = null;
        else
            last.previous.next = null;// 销毁原链表的尾部
        last = last.previous;
        size--;
        return temp;
    }

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

    // 输出链表中的所有数据项
    public void display() {
        Link<E> curr = first;
        while (curr != null) {
            System.out.print(curr.data + " ");
            curr = curr.next;
        }
        System.out.println();
    }
}

    public static void main(String[] args) {
        DoubleLink<Integer> dl = new DoubleLink<Integer>();
        for (int i = 0; i < 5; i++) {
            dl.insertFirst((int) (Math.random() * 100));
        }
        for (int i = 0; i < 5; i++) {
            dl.insertLast((int) (Math.random() * 100));
        }
        dl.display();
        while (!dl.isEmpty()) {
            dl.deleteFirst();
            dl.deleteLast();
            dl.display();
        }
        System.out.println("Ok");

    } 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2012-04-12 15:24  _公孓℡  阅读(150)  评论(0编辑  收藏  举报