数据结构--链表

根据定义,自己一步一步写代码,肯定写得出来

总的思想,链表用一个类表示,链表节点用一个类,那链表都是由节点,还要写什么在链表里面?反过来想,怎么知道你这个链表是个啥,怎么起始。那就得找个头来带一下。有了头,就可以推倒下一个,下下一个。以此类推。java的话。我不叫指针,叫引用地址。这么以来链表有人引用。就不至于给垃圾回收。各个节点在堆里面飘。容易吗,没家可归啊。

链表就是个头,或者个起始的标示。起始地址的标示。代码你就可想而知。至于关联关系都是靠节点。

一、单链表

定义,链表里面的节点是个啥?存了值,存了下个节点的引用地址,通常叫指针,显得高级。忽悠人

接下来增删自己想了。怎么定位到尾部的节点。

    class NodeList{    
        class Node{
            private String value = null;
            private Node nextNode = null;
            public Node(){
            }
            public Node(String value){
                this.value = value;
            }
        }        
        private int size;
        private Node headNode;        
        public NodeList(){
            this.headNode = null;
            this.size = 0;
        }
        public void addNode(Node node){
            if(size == 0){
                this.headNode = node;
            }else{
                Node temp = this.headNode;
                while(temp.nextNode != null){
                    temp = temp.nextNode;
                }
                temp.nextNode = node;                
            }
            size++;
        }
        
        public Node getNode(int i){
            if(i > size){
                return null;
            }
            int tempSize = 1;
            Node temp = this.headNode;
            while(tempSize <= i){
                temp = temp.nextNode;
                tempSize++;
            }
            return temp;
        }
    }
        
    @SuppressWarnings("rawtypes")
    public static void main(String[] args) throws ParseException{
        NodeList nodeList = new test().new NodeList();
        nodeList.addNode(new test().new NodeList().new Node("1"));
        nodeList.addNode(new test().new NodeList().new Node("2"));
        nodeList.addNode(new test().new NodeList().new Node("3"));    
        nodeList.addNode(new test().new NodeList().new Node("4"));    
        nodeList.addNode(new test().new NodeList().new Node("5"));    
        
        System.out.println(nodeList.getNode(2).value);

二、双向链表

定义,链表节点有双向引用,强强联合,每一步都必须强强联合。这个就是跟单链表的区别。

这么以来,节点有向前引用,向后引用,自己有个值。链表呢?跟单链表一样就一个头?也可以写。只是失去了双向的目的,人家就是为了两个头来的。你搞一个头有意思吗。参考jdk里面linkedlist

class NodeList{
        
        class Node{
            private String value = null;
            private Node previousNode = null;
            private Node nextNode = null;
            public Node(){
            }
            public Node(String value){
                this.value = value;
            }
        }
        
        private int size;
        private Node headNode;
        private Node lastNode;
        
        public NodeList(){
            this.headNode = this.lastNode = null;
            this.size = 0;
        }
        
        public void addNode(Node node){
            if(this.headNode == null){
                this.headNode = node;
                this.lastNode = new Node();
                this.headNode.nextNode = this.lastNode;
                this.lastNode.previousNode = this.headNode;
            }else{
                this.lastNode.previousNode.nextNode = node;
                node.previousNode = this.lastNode.previousNode;
                node.nextNode = this.lastNode;
                this.lastNode.previousNode = node;
            }
            size++;
        }
        //可判断i是否中间,两边开刀
        public Node getNode(int i){
            if(i > size){
                return null;
            }
            int tempSize = 1;
            Node temp = this.headNode;
            while(tempSize <= i){
                temp = temp.nextNode;
                tempSize++;
            }
            return temp;
        }
    }
        
    @SuppressWarnings("rawtypes")
    public static void main(String[] args) throws ParseException{
        NodeList nodeList = new test().new NodeList();
        nodeList.addNode(new test().new NodeList().new Node("1"));
        nodeList.addNode(new test().new NodeList().new Node("2"));
        nodeList.addNode(new test().new NodeList().new Node("3"));    
        nodeList.addNode(new test().new NodeList().new Node("4"));    
        nodeList.addNode(new test().new NodeList().new Node("5"));    
        
        System.out.println(nodeList.getNode(0).value);

三、循环链表

头结点,尾部指向头结点。。链表,依次类比,带个头

    class NodeList{    
        class Node{
            private String value = null;
            private Node nextNode = null;
            public Node(){
            }
            public Node(String value){
                this.value = value;
            }
        }        
        private int size;
        private Node headNode;        
        public NodeList(){
            this.headNode = null;
            this.size = 0;
        }
        public void addNode(Node node){
            if(size == 0){
                this.headNode = node;
                this.headNode.nextNode = this.headNode;
            }else{
                Node temp = this.headNode;
                while(temp.nextNode != this.headNode){
                    temp = temp.nextNode;
                }
                temp.nextNode = node;
                node.nextNode = this.headNode;
            }
            size++;
        }
        
        public Node getNode(int i){
            if(i > size){
                return null;
            }
            int tempSize = 1;
            Node temp = this.headNode;
            while(tempSize <= i){
                temp = temp.nextNode;
                tempSize++;
            }
            return temp;
        }
    }
        
    @SuppressWarnings("rawtypes")
    public static void main(String[] args) throws ParseException{
        NodeList nodeList = new test().new NodeList();
        nodeList.addNode(new test().new NodeList().new Node("1"));
        nodeList.addNode(new test().new NodeList().new Node("2"));
        nodeList.addNode(new test().new NodeList().new Node("3"));    
        nodeList.addNode(new test().new NodeList().new Node("4"));    
        nodeList.addNode(new test().new NodeList().new Node("5"));    
        
        System.out.println(nodeList.getNode(4).value);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值