加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

双向链表实现java代码

发布时间:2020-05-28 16:15:10 所属栏目:Java 来源:互联网
导读:双向链表实现java代码

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

/*
 * 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);
        list.add(3);
        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;
    }
}

/*
 * 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;
    }
}

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读