java实现双向循环链表和单链表

参照Java SE uitil LinkedList实现了一下简单化的LinkedList

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package linkedlisttest;

/**
*
* @author Lindily
*/
public class LinkedList<E> {

    int size = 0;
    Node<E> head = new Node(null, null, null);

    public LinkedList() {
        head.next = head.previous = head;
    }

    public void add(E node) {
        //核心 循环双向链表
        Node<E> newNode = new Node(head.previous, node, head);   //新节点的prev指向头结点的prev 新节点的next指向头结点
        newNode.previous.next = newNode;    //调整,新节点的前一个的后一个
        newNode.next.previous = newNode;    //调整,新节点的后一个的前一个
        size++;
    }

    public Node get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index:" + index + ",size:" + size);
        }
        Node node = head;
        if (index < (size >> 1)) {          //index转为二进制带符号向右移动一个bit,相当于index/2
            for (int i = 0; i <= index; i++) {    //head是哑元,i<=index当index=0时,返回head.next
                node = node.next;           //对头结点进行迭代
            }
        }else{
            for(int i=size;i>index;i--){
                node=node.previous;
            }
        }
        return node;
    }

    public int size() {
        return size;
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.add(1);
        list.add(2);
        for (int i = 0; i < list.size; i++) {
            System.out.println(list.get(i).node);
        }
    }
}

class Node<E> {

    E node;
    Node<E> next;
    Node<E> previous;

    public Node(Node<E> previous, E node, Node<E> next) {
        this.node = node;
        this.next = next;
        this.previous = previous;
    }
}

//以下实现的是单链表,没有了双链表的head头指针的哑元(空元素),但必须构造一个tail尾结点辅助。

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package linkedlisttest;

/**
*
* @author Lindily
*/
public class SingleLinkedList<T> {

    int size = 0;
    Node<T> head, tail;

    public SingleLinkedList() {
        head = tail = null;
    }

    public void add(T node) {
        if (size == 0) {
            head = tail = new Node<T>(node, null);
        } else {
            tail.next = new Node<T>(node, null);
            tail = tail.next;
        }
        size++;
    }

    public Node<T> get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index:" + index + ",size:" + size);
        }
        Node<T> node = head;
        for (int i = 0; i < index; i++) { //当index=0时,i不小于index(零不小于零),于是直接return头结点,与linkedList不同,head不是哑元
            node = node.next;           //对头结点进行迭代
        }
        return node;
    }

    public int size(){
        return size;
    }

    public static void main(String[] args){
        SingleLinkedList list=new SingleLinkedList();
        list.add(1);
        list.add(2);
        list.add(3);
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i).node);
        }
    }
}

class Node<T> {

    T node;
    Node<T> next;

    public Node(T node, Node next) {
        this.node = node;
        this.next = next;
    }
}

这两个链表最大的不同就是头结点是否是哑元,以及取出元素(get函数)的时候for循环变量i的不同:

双向循环链表(和java.util包的设计一样):由于head是哑元,因此取元素从head的下一个结点取

单向链表:head不是哑元,第一次必须取head头结点的元素,因此循环上和双向循环链表不同。也就是第一次get并没有进入for循环,直接返回了头结点,从第二次才开始进入for循环,这里要特别注意。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值