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

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

  • 相关阅读:
    使用postman玩转接口测试
    使用virtualbox安装centos虚拟机,以及VirtualBox无法安装64位Linux CentOS的解决办法
    pytest学习笔记(三)
    【Robot Framework】robot framework 学习以及selenium、appnium、requests实践(一)
    some tips
    python连mysql时避免出现乱码
    我回来啦_(:з」∠)_
    杂题记录
    日常记录。。。
    网络流
  • 原文地址:https://www.cnblogs.com/wxwall/p/3175181.html
Copyright © 2011-2022 走看看