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();
        }
    }

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

  • 相关阅读:
    堡垒问题
    装载问题
    最长公共子序列(LCS)
    windows 8(8.1) 、windows 7 、linux(fadora,ubuntu) 三个系统安装方法介绍
    编译sass,遇到报错error style.scss (Line 3: Invalid GBK character "\xE5")
    使用sublime text3手动安装插件
    win7 安装 nodesass报错
    收藏的一些github开源项目,在这里记录一下
    总是有人问我,那你能造出你自己都搬不动的石头吗? 我说不能,但我能写出个我自己都无法 fix 的 bug。
    genmotion 安装 app 报错 This application is't compatible with your mobile phone解决办法
  • 原文地址:https://www.cnblogs.com/wxwall/p/3175181.html
Copyright © 2011-2022 走看看