zoukankan      html  css  js  c++  java
  • 第6次课堂学习

    class Link{
     class Node{
      private String data;
      private Node next;
      public Node(String data){
       this.data=data;
       
      }
      public void add(Node newNode){
       if(this.next==null){
        this.next=newNode;
       }else{
        this.next.add(newNode);
        
       }
      }
      public void print(){
       System.out.print(this.data+" ");
       if(this.next!=null){
        this.next.print();
       }
      }
      public boolean search(String data){
       if(data.equals(this.data)){
        return true;
       }else{
        if(this.next!=null){
         return this.next.search(data);
        }else{
         return false;
        }
       }
      }
      public void delete(Node previous,String data){
       if(data.equals(this.data)){
        previous.next=this.next;
       }else{
        if(this.next!=null){
         this.next.delete(this,data);
        }
       }
      }
     }
     private Node root;
     public void addNode(String data){
      Node newNode=new Node(data);
      if(this.root==null){
       this.root=newNode;
       
      }else{
       this.root.add(newNode);
      }
     }
     public void printNode(){
      if(this.root!=null){
       this.root.print();
      }
     }
     public boolean contains(String name){
      return this.root.search(name);
     }
     public void deleteNode(String data){
      if(this.contains(data)){
       if(this.root.data.equals(data)){
        this.root=this.root.next;
       }else{
        this.root.next.delete(root,data);
       }
      }
     }
    }
    public class LinkDemo02 {

     /**
      * @param args
      */
     public static void main(String[] args) {
      // TODO Auto-generated method stub
    Link I=new Link();
    I.addNode("A");
    I.addNode("B");
    I.addNode("C");
    I.addNode("D");
    I.addNode("E");
    System.out.println("==============删除之前========");
    I.printNode();
    I.deleteNode("C");
    I.deleteNode("D");
    System.out.println();
    System.out.println("==============删除之后========");
    I.printNode();
    System.out.println();
    System.out.println("查询节点:"+I.contains("A"));

     }

    }

  • 相关阅读:
    Python的招牌花旦
    Python的for循环究竟是什么回事儿
    人人都喜欢用的十大python标准库
    有编程基础学python怎么赚点小钱?
    python3 Flask Redis 如何切换数据库
    贪心算法
    【ACM】nyoj_106_背包问题_201308152026
    【ACM】nyoj_14_会场安排问题_201308151955
    【ACM】nyoj_132_最长回文子串_201308151713
    【ACM】nyoj_47_过桥问题_201308151616
  • 原文地址:https://www.cnblogs.com/Lx160809221/p/6652211.html
Copyright © 2011-2022 走看看