zoukankan      html  css  js  c++  java
  • 内部类实现动态链表(增,删,查,打印)

    书上的练习,自己实现了下。

      1 class Link
    2 {
    3 private Node root;
    4 class Node
    5 {
    6 private Node next;
    7 private String name;
    8 public Node(String str)
    9 {
    10 this.name=str;
    11 }
    12 public String getName()
    13 {
    14 return this.name;
    15 }
    16 public void addNode(Node node)
    17 {
    18 if(this.next==null)
    19 {
    20 this.next=node;
    21 }
    22 else
    23 {
    24 this.next.addNode(node);
    25 }
    26 }
    27 public void printNode()
    28 {
    29 if(this.next==null)
    30 {
    31 System.out.print(this.getName());
    32 }
    33 else
    34 {
    35 System.out.print(this.getName()+"->");
    36 this.next.printNode();
    37 }
    38 }
    39 public boolean searchNode(String name)
    40 {
    41 if(this.getName().equals(name))
    42 {
    43 return true;
    44 }
    45 else
    46 {
    47 if(this.next==null)
    48 {
    49 return false;
    50 }
    51 else
    52 {
    53 return this.next.searchNode(name);
    54 }
    55 }
    56 }
    57 public void deleteNode(Node preNode,String name)
    58 {
    59 if(this.getName().equals(name))
    60 {
    61 preNode.next=this.next;
    62 }
    63 else
    64 {
    65 this.next.deleteNode(this,name);
    66 }
    67 }
    68 }
    69 public void add(String name)
    70 {
    71 Node newNode=new Node(name);
    72 if(root==null)
    73 {
    74 this.root=newNode;
    75 }
    76 else
    77 {
    78 this.root.addNode(newNode);
    79 }
    80 }
    81 public void printLink()
    82 {
    83 if(root!=null)
    84 {
    85 this.root.printNode();
    86 }
    87 }
    88 public boolean search(String name)
    89 {
    90 if(this.root.getName().equals(name))
    91 {
    92 return true;
    93 }
    94 else
    95 {
    96 return this.root.next.searchNode(name);
    97 }
    98 }
    99 public void delete(String name)
    100 {
    101 if(this.search(name))//判断节点是否存在
    102 {
    103 if(this.root.getName().equals(name))//判断要删除的节点是否是头结点
    104 {
    105 if(this.root.next==null)//判断头节点是否有后续节点
    106 {
    107 this.root=null;
    108 }
    109 else
    110 {
    111 this.root=root.next;//更改头结点
    112 }
    113 }
    114 else
    115 {
    116 this.root.next.deleteNode(root,name);
    117 }
    118 }
    119 else
    120 {
    121 System.out.println("要删除的节点不存在!");
    122 }
    123 }
    124 }
    125 public class LinkedNode
    126 {
    127 public static void main(String[] args)
    128 {
    129 Link l=new Link();
    130 l.add("根节点");
    131 l.add("第一个节点");
    132 l.add("第二个节点");
    133 l.add("第三个节点");
    134 l.add("第四个节点");
    135 l.add("第五个节点\n");
    136
    137 l.printLink();
    138 System.out.println(l.search("根节点"));
    139 l.delete("根节点");
    140 l.printLink();
    141 l.delete("第八个节点");
    142 l.delete("第四个节点");
    143 l.printLink();
    144 }
    145 }



  • 相关阅读:
    hdu 4403 枚举
    hdu 4405概率dp
    lightoj 1036 dp
    lightoj 1033 区间dp
    lightoj 1032 二进制的dp
    hdu 4293 dp求最大权值不重合区间
    poj 2449 第k短路
    hdu 4284 状态压缩
    hdu4281 区间dp
    poj 2288 tsp经典问题
  • 原文地址:https://www.cnblogs.com/xiongyu/p/2225182.html
Copyright © 2011-2022 走看看