java链表 分别用内部类和外部类实现

来源:互联网 发布:学生管理系统设计c语言 编辑:程序博客网 时间:2024/05/22 14:44

        在这里我将使用外部类和内部类两种方法来实现java的链表,参考了java老师上课讲过的代码~

        主要思想是:首先要有一个Node类节点类),其成员变量为String 类型的name,还有一个Node类型的next,作为指向下一个节点的指针;然后会设计增删查改四个方法(addNode()、printNode()、searchNode()还有deleteNode() 这里作简写~)。然后是一个LinkList类,它首先会有一个根节点(Node 类型的root),然后是增删查改四个方法,不要忘了还有创建根节点的方法~

        (⊙o⊙)…很不友好,因为我没怎么写注释。具体的代码如下(已调通):

class LinkList {class Node{private String name;private Node next;public Node(String name){this.name=name;}public String getName(){return this.name;}//to add a nodepublic void addNode(Node newNode){if(this.next==null){this.next=newNode;}else{this.next.addNode(newNode);//recursive call.it'll keep searching until finding a Node which doesn't have next node }}//to print these nodespublic void printNode(){System.out.print(this.name+"-->");if(this.next!=null){this.next.printNode();}}//searching for a nodepublic boolean searchNode(String name){if(this.name.equals(name)){return true;}else{if(this.next!=null){return this.next.searchNode(name);}else{return false;}}}//to delete a nodepublic void deleteNode(Node preNode,String name){if(this.name.equals(name)){preNode.next = this.next ;}else{this.next.deleteNode(this,name) ;}}}private Node root;//root node//addpublic void add(String name){Node newNode = new Node(name);if(this.root==null){this.root = newNode; }else{this.root.addNode(newNode);}}//printpublic void print(){if(this.root!=null){this.root.printNode();}}//searchpublic boolean search(String name){if(this.root!=null){return root.searchNode(name);}else{return false;}}//deletepublic void delete(String name){if(this.search(name)){//if the node exists if(this.root.name.equals(name)){if(this.root.next!=null){this.root = this.root.next ;}else{this.root = null ;}}else{if(this.root.next!=null){this.root.next.deleteNode(root,name) ;}}}}}public class InnerLinkListTest{public static void main(String[] args) {LinkList link = new LinkList();//add four nodeSystem.out.println("add a root node");link.add("root node");System.out.println("add a first node");link.add("first node");System.out.println("add a second node");link.add("second node");System.out.println("add a third node");link.add("third node");System.out.println("add a fourth node");link.add("fourth node");//print all of the nodesSystem.out.print("Now you haeve :");link.print();System.out.println("\n");//search for the first nodeSystem.out.print("Does the first node exist?");if(link.search("first node")){System.out.println("  YES");}else{System.out.println("  NO");}System.out.println("\n");System.out.print("Does the fifth node exist?");if(link.search("fifth node")){System.out.println("  YES");}else{System.out.println("  NO");}System.out.println("\n");//delete the second nodeSystem.out.println("Delete the second node");link.delete("second node");System.out.print("Now you have :");link.print();System.out.println("\n");}}



这个是运行结果~












下面是用外部类实现的(其实和内部类基本上是一样的):

//Node类class Node{private String name;private Node next;public Node(String name){this.name=name;}public String getName(){return this.name;}public Node getNode(){return this.next;}//to add a nodepublic void addNode(Node newNode){if(this.next==null){this.next=newNode;}else{this.next.addNode(newNode);//recursive call.it'll keep searching until finding a Node which doesn't have next node }}//to print these nodespublic void printNode(){System.out.print(this.name+"-->");if(this.next!=null){this.next.printNode();}}//searching for a nodepublic boolean searchNode(String name){if(this.name.equals(name)){return true;}else{if(this.next!=null){return this.next.searchNode(name);}else{return false;}}}//to delete a nodepublic void deleteNode(Node preNode,String name){if(this.name.equals(name)){preNode.next = this.next ;}else{this.next.deleteNode(this,name) ;}}}//Link类class Link{private Node root;//root node//addpublic void add(String name){Node newNode = new Node(name);if(this.root==null){this.root = newNode; }else{this.root.addNode(newNode);}}        //printpublic void print(){if(this.root!=null){this.root.printNode();}}//searchpublic boolean search(String name){if(this.root!=null){return root.searchNode(name);}else{return false;}}//deletepublic void delete(String name){if(this.search(name)){//if the node exists if(this.root.getName().equals(name)){if(this.root.getNode()!=null){this.root = this.root.getNode() ;}else{this.root = null ;}}else{if(this.root.getNode()!=null){this.root.getNode().deleteNode(root,name) ;}}}}}public class OuterLinkListTest {public static void main(String[] args) {Link link = new Link();//add four nodeSystem.out.println("add a root node");link.add("root node");System.out.println("add a first node");link.add("first node");System.out.println("add a second node");link.add("second node");System.out.println("add a third node");link.add("third node");System.out.println("add a fourth node");link.add("fourth node");//print all of the nodesSystem.out.print("Now you haeve :");link.print();System.out.println("\n");//search for the first nodeSystem.out.print("Does the first node exist?");if(link.search("first node")){System.out.println("  YES");}else{System.out.println("  NO");}System.out.println("\n");System.out.print("Does the fifth node exist?");if(link.search("fifth node")){System.out.println("  YES");}else{System.out.println("  NO");}System.out.println("\n");//delete the second nodeSystem.out.println("Delete the second node");link.delete("second node");System.out.print("Now you have :");link.print();System.out.println("\n");}}


运行结果和第一个是一样的。


0 0
原创粉丝点击