linknode.java

来自「这个程序提供了JAVA很基础的CLASS的应用举例」· Java 代码 · 共 135 行

JAVA
135
字号
import java.io.*;
class linknode{
	int data;
	linknode previous;
	linknode next;
	
	public linknode(){
		data=0;
		previous=null;
		next=null;
	}
	public linknode(int data,linknode p,linknode q){
		this.data=data;
		this.previous=p;
		this.next=q;
	}
	
	
	
	public void move(int n){
		data= n;
		}
	
	
	public linknode input(linknode p)throws IOException{
		int number;
		System.out.println("please input the numbers that you want to sort and sign the end with 0;");
		String s;
		InputStreamReader ir;
		BufferedReader in;
		ir=new InputStreamReader(System.in);
		in=new BufferedReader(ir);
	    s=in.readLine();
	    number=Integer.parseInt(s);
	    p.move(number);
	    s=in.readLine();
	    number=Integer.parseInt(s);
		while(number!=0)
		{
			linknode temp=new linknode();
			temp.move(number);
			temp.next=p;
			p.previous=temp;
			p=temp;
			s=in.readLine();
	        number=Integer.parseInt(s);     
		}
		linknode check=new linknode();
		check=p;
		System.out.println("the numbers that will be sorted are :");
		
		while(check!=null)
		{
			System.out.println(check.data);
			check=check.next;	
		}
		//System.out.println("OK");
		return p;
		
	}
	//It is right;
	
	
	
	
	
	
	 public void change(linknode p){
     	int temp;
     	linknode temp1=p;
     	linknode temp2=p;
     	linknode min;
     	while(temp1!=null)
     	{
     		temp2=temp1;
     		min=temp1;
     		while(temp2!=null)
     		{
     			if(temp2.data<min.data)
     			{
     				min=temp2;
     			}
     			temp2=temp2.next;     	
     		}
     		temp=min.data;
     		min.move(temp1.data);
     		temp1.move(temp);
     		temp1=temp1.next;	
     	} 
     	System.out.println("the numbers that have been sorted are :");
     	
     	while(p!=null)
     	{
     		System.out.println(p.data);
     		p=p.next;	
     	}
     	System.out.println("OK");
     }
     	
     	
     	
     	
     	
     	
     	
     	public static void main(String args[])throws IOException{
		linknode p=new linknode();
		p=p.input(p);
		p.change(p);
		}
			
	
}

	
	

		
		
		
		
		
	
	
	
	
	
	
	
			
    
     	


	

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?