在这里我将使用外部类和内部类两种方法来实现Java的链表,参考了java老师上课讲过的代码~
主要思想是:首先要有一个Node类(节点类),其成员变量为String 类型的name,还有一个Node类型的next,作为指向下一个节点的指针;然后会设计增删查改四个方法(addNode()、printNode()、searchNode()还有deleteNode() 这里作简写~)。然后是一个LinkList类,它首先会有一个根节点(Node 类型的root),然后是增删查改四个方法,不要忘了还有创建根节点的方法~
(⊙o⊙)…很不友好,因为我没怎么写注释。具体的代码如下(已调通):
1 class LinkList { 2 3 class Node{ 4 5 private String name; 6 private Node next; 7 public Node(String name){ 8 this.name=name; 9 } 10 public String getName(){ 11 return this.name; 12 } 13 14 //to add a node 15 public void addNode(Node newNode){ 16 if(this.next==null){ 17 this.next=newNode; 18 }else{ 19 this.next.addNode(newNode);//recursive call.it'll keep searching until finding a Node which doesn't have next node 20 } 21 } 22 23 //to print these nodes 24 public void printNode(){ 25 System.out.print(this.name+"-->"); 26 if(this.next!=null){ 27 this.next.printNode(); 28 } 29 } 30 //searching for a node 31 public boolean searchNode(String name){ 32 if(this.name.equals(name)){ 33 return true; 34 }else{ 35 if(this.next!=null){ 36 return this.next.searchNode(name); 37 }else{ 38 return false; 39 } 40 } 41 } 42 43 //to delete a node 44 public void deleteNode(Node preNode,String name){ 45 if(this.name.equals(name)){ 46 preNode.next = this.next ; 47 }else{ 48 this.next.deleteNode(this,name) ; 49 } 50 } 51 } 52 53 54 private Node root;//root node 55 56 //add 57 public void add(String name){ 58 Node newNode = new Node(name); 59 if(this.root==null){ 60 this.root = newNode; 61 } 62 else{ 63 this.root.addNode(newNode); 64 } 65 } 66 67 //print 68 public void print(){ 69 if(this.root!=null){ 70 this.root.printNode(); 71 } 72 } 73 74 //search 75 public boolean search(String name){ 76 if(this.root!=null){ 77 return root.searchNode(name); 78 } 79 else{ 80 return false; 81 } 82 } 83 84 //delete 85 public void delete(String name){ 86 if(this.search(name)){//if the node exists 87 if(this.root.name.equals(name)){ 88 if(this.root.next!=null){ 89 this.root = this.root.next ; 90 }else{ 91 this.root = null ; 92 } 93 }else{ 94 if(this.root.next!=null){ 95 this.root.next.deleteNode(root,name) ; 96 } 97 } 98 } 99 100 } 101 102 } 103 104 105 public class InnerLinkListTest{ 106 107 public static void main(String[] args) { 108 LinkList link = new LinkList(); 109 //add four node 110 System.out.println("add a root node"); 111 link.add("root node"); 112 System.out.println("add a first node"); 113 link.add("first node"); 114 System.out.println("add a second node"); 115 link.add("second node"); 116 System.out.println("add a third node"); 117 link.add("third node"); 118 System.out.println("add a fourth node"); 119 link.add("fourth node"); 120 121 //print all of the nodes 122 System.out.print("Now you haeve :"); 123 link.print(); 124 System.out.println(" "); 125 126 //search for the first node 127 System.out.print("Does the first node exist?"); 128 if(link.search("first node")){ 129 System.out.println(" YES"); 130 }else{ 131 System.out.println(" NO"); 132 } 133 System.out.println(" "); 134 System.out.print("Does the fifth node exist?"); 135 if(link.search("fifth node")){ 136 System.out.println(" YES"); 137 }else{ 138 System.out.println(" NO"); 139 } 140 System.out.println(" "); 141 142 //delete the second node 143 System.out.println("Delete the second node"); 144 link.delete("second node"); 145 System.out.print("Now you have :"); 146 link.print(); 147 System.out.println(" "); 148 149 } 150 }
运行结果是这样哒!!!
然后是基本上一样的外部类:
1 //Node类 2 class Node{ 3 private String name; 4 private Node next; 5 public Node(String name){ 6 this.name=name; 7 } 8 public String getName(){ 9 return this.name; 10 } 11 public Node getNode(){ 12 return this.next; 13 } 14 15 //to add a node 16 public void addNode(Node newNode){ 17 if(this.next==null){ 18 this.next=newNode; 19 }else{ 20 this.next.addNode(newNode);//recursive call.it'll keep searching until finding a Node which doesn't have next node 21 } 22 } 23 24 //to print these nodes 25 public void printNode(){ 26 System.out.print(this.name+"-->"); 27 if(this.next!=null){ 28 this.next.printNode(); 29 } 30 } 31 32 //searching for a node 33 public boolean searchNode(String name){ 34 if(this.name.equals(name)){ 35 return true; 36 }else{ 37 if(this.next!=null){ 38 return this.next.searchNode(name); 39 }else{ 40 return false; 41 } 42 } 43 } 44 45 //to delete a node 46 public void deleteNode(Node preNode,String name){ 47 if(this.name.equals(name)){ 48 preNode.next = this.next ; 49 }else{ 50 this.next.deleteNode(this,name) ; 51 } 52 } 53 } 54 55 56 //Link类 57 class Link{ 58 private Node root;//root node 59 60 //add 61 public void add(String name){ 62 Node newNode = new Node(name); 63 if(this.root==null){ 64 this.root = newNode; 65 } 66 else{ 67 this.root.addNode(newNode); 68 } 69 } 70 71 //print 72 public void print(){ 73 if(this.root!=null){ 74 this.root.printNode(); 75 } 76 } 77 78 //search 79 public boolean search(String name){ 80 if(this.root!=null){ 81 return root.searchNode(name); 82 } 83 else{ 84 return false; 85 } 86 } 87 88 //delete 89 public void delete(String name){ 90 if(this.search(name)){//if the node exists 91 if(this.root.getName().equals(name)){ 92 if(this.root.getNode()!=null){ 93 this.root = this.root.getNode() ; 94 }else{ 95 this.root = null ; 96 } 97 }else{ 98 if(this.root.getNode()!=null){ 99 this.root.getNode().deleteNode(root,name) ; 100 } 101 } 102 } 103 104 } 105 106 } 107 108 109 public class OuterLinkListTest { 110 public static void main(String[] args) { 111 Link link = new Link(); 112 //add four node 113 System.out.println("add a root node"); 114 link.add("root node"); 115 System.out.println("add a first node"); 116 link.add("first node"); 117 System.out.println("add a second node"); 118 link.add("second node"); 119 System.out.println("add a third node"); 120 link.add("third node"); 121 System.out.println("add a fourth node"); 122 link.add("fourth node"); 123 124 //print all of the nodes 125 System.out.print("Now you haeve :"); 126 link.print(); 127 System.out.println(" "); 128 129 //search for the first node 130 System.out.print("Does the first node exist?"); 131 if(link.search("first node")){ 132 System.out.println(" YES"); 133 }else{ 134 System.out.println(" NO"); 135 } 136 System.out.println(" "); 137 System.out.print("Does the fifth node exist?"); 138 if(link.search("fifth node")){ 139 System.out.println(" YES"); 140 }else{ 141 System.out.println(" NO"); 142 } 143 System.out.println(" "); 144 145 //delete the second node 146 System.out.println("Delete the second node"); 147 link.delete("second node"); 148 System.out.print("Now you have :"); 149 link.print(); 150 System.out.println(" "); 151 } 152 }
嗯!运行结果是一样的~就是这样!