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

        在这里我将使用外部类和内部类两种方法来实现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 node
		public 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 nodes
		public void printNode(){
			System.out.print(this.name+"-->");
			if(this.next!=null){
				this.next.printNode();
			}
		}
		//searching for a node
		public 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 node
		public 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
		
		//add
		public void add(String name){
			Node newNode = new Node(name);
			if(this.root==null){
				this.root = newNode; 
			}
			else{
				this.root.addNode(newNode);
			}
		}
		
		//print
		public void print(){
			if(this.root!=null){
				this.root.printNode();
			}
		}
		
		//search
		public boolean search(String name){
			if(this.root!=null){
				return root.searchNode(name);
			}
			else{
				return false;
			}
		}
		
		//delete
		public 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 node
		System.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 nodes
		System.out.print("Now you haeve :");
		link.print();
		System.out.println("\n");
		
		//search for the first node
		System.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 node
		System.out.println("Delete the second node");
		link.delete("second node");
		System.out.print("Now you have :");
		link.print();
		System.out.println("\n");

	}
}



这个是运行结果~

java链表 分别用内部类和外部类实现_第1张图片












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

//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 node
	public 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 nodes
	public void printNode(){
		System.out.print(this.name+"-->");
		if(this.next!=null){
			this.next.printNode();
		}
	}

	//searching for a node
	public 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 node
	public 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

	//add
	public void add(String name){
		Node newNode = new Node(name);
		if(this.root==null){
			this.root = newNode; 
		}
		else{
			this.root.addNode(newNode);
		}
	}

        //print
	public void print(){
		if(this.root!=null){
			this.root.printNode();
		}
	}

	//search
	public boolean search(String name){
		if(this.root!=null){
			return root.searchNode(name);
		}
		else{
			return false;
		}
	}

	//delete
	public 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 node
		System.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 nodes
		System.out.print("Now you haeve :");
		link.print();
		System.out.println("\n");
		
		//search for the first node
		System.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 node
		System.out.println("Delete the second node");
		link.delete("second node");
		System.out.print("Now you have :");
		link.print();
		System.out.println("\n");
	}
}


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


你可能感兴趣的:(java,数据结构)