1. 程式人生 > >java連結串列 分別用內部類和外部類實現

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");

	}
}



這個是執行結果~

下面是用外部類實現的(其實和內部類基本上是一樣的):

//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");
	}
}

執行結果和第一個是一樣的。