【数据结构和算法分析】单链表的基本实现

来源:互联网 发布:淘宝 7个小白 编辑:程序博客网 时间:2024/04/25 15:10

单链表的基本实现

链表中的数据是以节点来表示的,每个节点的构成:元素 + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个节点的地址数据。 


效率问题:单链表在效率上最大的问题在于,如果要插入一个结点到链表的末端或者删除末端的一个结点,则需要遍历整个链表,时间复杂度是O(N)。平均来说,要访问一个结点,时间复杂度也有O(N/2)。这是链表本身的性质所造成的,没办法解决。不过我们可以采用双链表和循环链表来改善这种情况。


其他说明:java 的 util 中的链表实现有两种方式,ArrayList由数组实现,LinkdeList由结点实现,两者各有千秋,在此,MyList是由结点实现的链表


public class MyList {        /*     * 单向链表的基本实现     * 包括插入结点,删除结点,查询某一位置结点的值,修改结点的值,反回结点的位置等基本功能     */    Node headNode;   //头结点    Node tailNode;   //尾结点    int length;    //链表长度       /*     * 初始化链表,由头结点创建     * 仅有单一  Node 创建,不考虑在 Node 中 new Node     * @param  Node node 头结点     */    public MyList(Node node){            this.headNode = node;        this.tailNode = node;        length++;    }     /*     * 在链表结尾加一个结点     * @param Node node 加在结尾的结点     */    public void insert(Node node){        insert(node, length);    }    /*     * 在链表中加一个结点     * @param Node node,加在链表中的结点   , int index 结点所加 的位置     */    public void insert(Node node,int index){        if (index < 0 || index > length) {            throw new IndexOutOfBoundsException();        }        if (index == 0) {            node.next = this.headNode;            headNode = node;        }else if (index == length) {            this.tailNode.next = node;            tailNode = node;        }else {            Node otherNode = headNode;                while (index > 1) {                otherNode = otherNode.next;                index--;            }            node.next = otherNode.next;            otherNode.next = node;        }        length++;    }    /*     * 删除链表中的一个结点     * @param int index 所需删除的结点的位置     */    public void delete(int index){        if (index < 0 || index >= length) {            throw new IndexOutOfBoundsException();        }        if (index == 0 && length != 1) {            this.headNode = headNode.next;            length--;        }else if(index == 0 && length == 1){            this.headNode = this.tailNode = null;            length = 0;        }else {            Node otherNode = this.headNode;            while (index > 1) {                otherNode = otherNode.next;                index--;            }            otherNode.next = otherNode.next.next;            length--;        }    }    /*     * 查看某一处结点的值     * @param int index 所需查看的结点的位置     */    public Object indexOf(int index){        if (index < 0 || index >= length) {            throw new IndexOutOfBoundsException();        }        Node otherNode = this.headNode;        while (index > 0) {            otherNode = otherNode.next;            index--;        }        return otherNode.data;    }    /*     * 设置某一处结点的值     * @param int index 结点的位置 ,Object data 修改后的结点的值     */        public void setData(int index,Object data){        if (index < 0 || index >= length) {            throw new IndexOutOfBoundsException();        }        Node otherNode = this.headNode;        while (index > 0) {            otherNode = otherNode.next;            index--;        }        otherNode.data = data;    }    /*     * 依次打印所以结点     */    public void print(){        Node otherNode = this.headNode;        while(otherNode.hasnext()){            System.out.print(otherNode.data+" ");            otherNode = otherNode.next;        }        System.out.println(otherNode.data);    }}class Node{        /*     * 节点类,包括结点的值 data 和指向下一结点的指针 next     */        Object data;    Node next;        public Node(){        data = null;        next = null;    }        public Node(Object data){        this.data = data;        this.next = null;    }        public Node(Object data,Node nextnode){        this.data = data;        this.next = nextnode;    }        /*     * 判断有无下一结点     * @return boolean 若true,则该结点有下一结点     */    public boolean hasnext(){        if (this.next != null) {            return true;        }else {            return false;        }    }    }

0 0