学习链表源码记录

创建一个节点类

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

 

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

你可能感兴趣的:(学习)