首页 > 其他 > 详细

单向链表的实现

时间:2015-03-24 14:31:27      阅读:254      评论:0      收藏:0      [点我收藏+]

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 + "\t");

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

      }

    }

  }

};

单向链表的实现

原文:http://www.cnblogs.com/win24/p/4362536.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!