zoukankan      html  css  js  c++  java
  • 学习链表源码记录

    创建一个节点类

    package com.wxw.Link;

    public class Node {

        private String nodeName;
        private Node next;
       
        public Node(String nodeName){
            this.nodeName = nodeName;
        }
       
        public Node getNext(){
            return this.next;
        }

        public String getNodeName() {
            return nodeName;
        }

        public void setNodeName(String nodeName) {
            this.nodeName = nodeName;
        }

        public void setNext(Node next) {
            this.next = next;
        }
       
       
        public boolean hasNext(){
            boolean is = false;
            if(null != this.next){
                is = true;
            }
            return is;
        }
       
       
    }

    创建一个链表

    package com.wxw.Link;

    public class Link {
        private Node head;
        public Link(Node head){
            this.head = head;
        }
       
       
        public void addNode(Node node){
            Node p = head;
            while(true){
                if(!p.hasNext()){
                    p.setNext(node);
                    break;
                }
                p = p.getNext();
            }
        }
       
        /**
         * 遍历节点
         */
        public void display(){
            Node p = head.getNext();
            while(true){
                System.out.println(p.getNodeName());
                if(p.getNext() == null){
                    break;
                }
                p=p.getNext();
               
            }
        }
       
        /**
         * 插入到Node p后面的节点上
         * @param p
         * @param q
         */
        public void insertNode(Node p,Node q){
            q.setNext(p.getNext());
            p.setNext(q);
        }
       
        /**
         * 删除节点
         */
        public void delete(String nodeName){
            Node p = head;
            while(true){
                if(p.getNext().getNodeName() == nodeName){
                    p.setNext(p.getNext().getNext());
                    break;
                }
                p = p.getNext();
            }
        }
       
        public Node getNode(String nodeName){
            Node p = head;
            while(true){
                if(nodeName == p.getNodeName()){
                    return p;
                }
                p = p.getNext();
            }
        }
       
        public Node getLastNode(){
            Node p = head;
            while(true){
                if(p.getNext() == null){
                    return p;
                }
                p = p.getNext();
            }
        }
    }

    用链表实现的一个栈

    package com.wxw.Link;

    public class Stack {
       
        public Link link;
       
        public Stack(){
            this.link = new Link(new Node("head"));
        }
       
        /**
         * 进栈
         */
        public void push(String pushName){
            link.addNode(new Node(pushName));
        }
       
        /**
         * 出栈
         */
        public String pop(){
            String popName = link.getLastNode().getNodeName();
            link.delete(popName);
            return popName;
        }
    }

    测试类

    package com.wxw.Link;

    public class TestLink {
        public static void main(String[] args) {
            Node head = new Node("head");
            Node one = new Node("唐僧");
            Node tow = new Node("孙悟空");
            Node three = new Node ("猪八戒");
            Node four = new Node("沙和尚");
            Link link = new Link(head);
            link.addNode(one);
            link.addNode(tow);
            link.addNode(three);
            link.addNode(four);
            link.insertNode(one, new Node("白龙马"));
            //link.delete("白龙马");
            link.display();
        }
    }

    单向链表实现很简单,关键是需要一个去浮躁的心,慢慢去理解,并实现起来才会有学习的动力,否则,很难真正记住实现原理

  • 相关阅读:
    (转)elasticsearch collapse 折叠字段应用
    java 8 supplier object区别
    搜索 rerank : learn to rank 算法
    Runtime.addShutdownHook用法
    MAT(memory anlayzer tool)使用方法
    win10 打开注册表
    关于svn 更新到本地库 图标不显示问题(二)
    如何重启explorer,不用重启电脑 也能使设置生效
    关于svn 更新到本地库 图标不显示问题(一)
    eclipse 编写完JAVA的代码 ctrl+s保存一下,右下角就有building workspace一直在执行。。特别卡、卡、、
  • 原文地址:https://www.cnblogs.com/wxwall/p/3175181.html
Copyright © 2011-2022 走看看