JAvaは2つの秩序化された単一チェーンテーブルのマージを実現し、マージされた新しい秩序化された単一チェーンテーブルを出力する


  • javaは2つの秩序化された単一チェーンテーブルのマージを実現し、マージされた新しい秩序化された単一チェーンテーブル
  • を出力する.
  • 新しいチェーンテーブルは元のチェーンテーブルの構造を変更しない
  • package LinkedList;
    /*
     *           ,                          
     *             data    data     
     *         data  0
     * @author bxh
     */
    public class SortLinkedList {
    	public static void main(String[] args) {
    		Linkedlist linkedlist1 = new Linkedlist();
    		Linkedlist linkedlist2 = new Linkedlist();
    		linkedlist1.addnode(new Datanode(1));
    		linkedlist1.addnode(new Datanode(3));
    		linkedlist1.addnode(new Datanode(5));
    		linkedlist2.addnode(new Datanode(2));
    		linkedlist2.addnode(new Datanode(6));
    		linkedlist2.addnode(new Datanode(7));
    		System.out.println("              ");
    		linkedlist1.show();
    		linkedlist2.show();
    		System.out.println("=====================");
    		System.out.println("        ");
    		CombineLinkedList(linkedlist1.getHead(), linkedlist2.getHead());
    		linkedlist1.show();
    		linkedlist2.show();
    	}
    	
    	
    	
    	//               
    	//@param           
    	//@return                 
    	public static void CombineLinkedList(Datanode head1,Datanode head2) {
    		if(head1.next==null)
    		{
    			System.out.println("     ");
    			return ;
    		}
    		if(head2.next==null) {
    			System.out.println("     ");
    			return;
    		}
    		Linkedlist linkedlist = new Linkedlist(); //    
    		Datanode temp1=head1.next;
    		Datanode temp2=head2.next; 
    		while(temp1!=null && temp2!=null)     //             
    		{
    			if(temp1.data<=temp2.data)
    			{
    				linkedlist.addnode(new Datanode(temp1.data));
    				temp1=temp1.next;
    			}
    			else {
    				linkedlist.addnode(new Datanode(temp2.data));
    				temp2=temp2.next;
    			}
    		}
    		if(temp1==null)     //  link1    ,              
    		{
    			while(temp2!=null)
    			{
    				linkedlist.addnode(new Datanode(temp2.data));
    				temp2=temp2.next;
    			}
    		}
    		if(temp2==null) {       //  link2    ,              
    			while(temp1!=null)
    			{
    				linkedlist.addnode(new Datanode(temp1.data));
    				temp1=temp1.next;
    			}
    		}
    		
    		linkedlist.show();
    		return;
    		
    	}
    }
    class Linkedlist{
    	
    	Datanode head= new Datanode(0);
    	//     
    	public Datanode getHead() {
    		return head;
    	}
    
    	//        
    	public void addnode(Datanode node) {
    		if(head.next==null)
    		{
    			head.next=node;
    		}     //             ,                
    		else {
    			Datanode temp=head;
    			while(temp.next!=null) {
    				temp=temp.next;
    			}             //                
    			temp.next=node;    
    			//          
    			System.out.println("      ");
    		}
    	}
    	
    	//          
    	public void show() {
    		Datanode temp=head.next;
    		System.out.println("        :");
    		while(temp!=null)
    		{
    			System.out.println(temp);
    			temp=temp.next;
    		}
    	}
    	
    }
    
    
    //     
    class Datanode{
    	public int data;
    	public Datanode next;
    	public Datanode(int data) {
    		this.data=data;
    	}
    	@Override
    	public String toString() {
    		return "Datanode [data=" + data + "]";
    	}
    	
    }