黑马程序员技术交流社区

标题: 自己实现的LinkedList中怎么删除一个指定节点 [打印本页]

作者: adamjy    时间: 2014-4-7 13:12
标题: 自己实现的LinkedList中怎么删除一个指定节点
本帖最后由 adamjy 于 2014-4-8 21:12 编辑

如下代码是自己实现的LinkedList,有添加、删除(为实现)、显示元素功能,请问注释掉的"delete()"方法该怎么实现?

  1. class Node{
  2.         Node in;
  3.         String name;
  4.         public Node(){
  5.             in = null;
  6.         }
  7.         public Node(String n){
  8.             in = null;
  9.             name = n;
  10.         }
  11.         public void setIn(Node n){
  12.             in = n;
  13.         }
  14.         public Node getIn(){
  15.             return in;
  16.         }
  17.         
  18.         public void setName(String n){
  19.             name = n;
  20.         }
  21.         
  22.         public String getName(){
  23.             return name;
  24.         }
  25. }
  26. public class Main {
  27.         public static void main(String args[]){
  28.             Scanner scan = new Scanner(System.in);
  29.             LinkList bi = new LinkList();
  30.             while(true){
  31.                 System.out.println("Choose!\n[a] Insert Name\n[b] Delete\n[c] Show\n[d] Exit");
  32.                 char c = scan.next().charAt(0);
  33.                 System.out.println();
  34.                
  35.                 if(c == 'a'){
  36.                     System.out.print("Enter Name: ");
  37.                     bi.insert(scan.next());
  38.                     System.out.println();
  39.                 }
  40.                 else if(c == 'b'){
  41.                     System.out.print("Enter Name to delete: ");
  42. //                    bi.delete(scan.next());
  43.                     System.out.println();
  44.                 }
  45.                 else if(c == 'c'){
  46.                     bi.show();
  47.                     System.out.println();
  48.                 }
  49.                 else if(c == 'd'){
  50.                     System.exit(0);
  51.                 }
  52.             }
  53.         }
  54. }

  55. class LinkList{
  56.         private Node root;

  57.         public LinkList(){
  58.             root = null;
  59.         }
  60.         public void insert(String n){
  61.             root = insert(root, n);
  62.         }
  63.         private Node insert(Node n, String r){
  64.             if(n == null){
  65.                 n = new Node(r);
  66.             }
  67.             else{
  68.                 n.in = insert(n.in, r);
  69.             }
  70.             return n;
  71.         }

  72. //        public void delete(String n){
  73. //            root = delete(root, n);
  74. //        }
  75. //
  76. //        private Node delete(Node n, String r){
  77. //
  78. //        }

  79.         public void show(){
  80.             show(root);
  81.         }

  82.         private Node show(Node n){
  83.             if(n == null){
  84.                 System.out.println("Empy list!");
  85.             }
  86.             else{
  87.                 while(n!=null){
  88.                     System.out.println(n.getName());
  89.                     n = n.getIn();
  90.                 }
  91.             }
  92.             return n;
  93.         }
  94. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2