Daily work entitled the twenty-first title 20,200,323

/ **
* Title: the check list by a certain value of the left divided into small, equal to the intermediate, a large right form
*
* requirements:
* first node to a predetermined head-way linked list of nodes is an integer value types, then given an integer privot. Implement
* function to adjust a linked list, the linked list are adjusted to the left portion is less than the value of the node privot, the intermediate portion is equal privot
* node, is greater than the right part of the node privot. In addition to this requirement, there is no more requirement for the node order adjusted.
* For example: list 9-> 0-> 4-> 5-> 1, privot = 3. List may be adjusted 1-> 0-> 4-> 9-> 5, it may be
* 0-> 1-> 9-> 5-> 4. In short, to meet some of the nodes are left thousands of small 3, the middle part is equal to the node 3, the right parts are
* node can be greater than 3. On a part of the internal node order is not required.
*
* Analysis:
* Solution time complexity is O (N), additional space complexity is O (N), that is, all nodes in the linked list into a frontal
array * outside, and then measures the position of the uniform adjustment. Specific process is as follows:
* 1 again to traverse the list in order to obtain a length, assuming that the length of the list is N.
* 2. Generate Node length N type array nodeArr, then traverse a linked list of nodes successively into nodeArrray.
* 3. nodeArray in the node on the left of privot less than, equal to put the middle, the greater than on the right.
* 4. After the adjustment of step 3, nodeAn- node order to meet the requirements of the subject, as long as the nodes by nodeArray
* reconnection times up to the whole process ends.
*
* @Author snow pupil
*
* /

public class Node {
	public int value;
	public Node next;
	public Node(int data) {
		this.value=data;
	}
}

  

{class ListPart public 
	
	Private the Node Current; // current node 
	private int nodeLength = 0; // chain length 
	private Node nodeArray [] = null; // list array 
	private int i = 0; // loop variable 
	private int currentValue = 0; // current value of the node 
	
	public listPart the node (the node head, int privot) { 
		IF (head == null) { 
			return head; 
		} 
		// Get list length 
		current = head; 
		the while (! current = null) { 
			nodeLength ++; 
			current = current .next; 
		} 
		// write the contents of the list array 
		nodeArray the Node new new = [nodeLength]; 
		Current = head; 
		the while (I <nodeLength) { 
			nodeArray [I] = Current; 
			I ++; 
			Current = current.next; 
		} 
		// Sort
		int small = -1;
		int big = nodeArray.length;
		int index = 0 ;
		while(index != big) {
			 currentValue =nodeArray[index].value;
			if(currentValue < privot) {
				swapElements(nodeArray, ++small, index++);
			}else if(currentValue > privot) {
				swapElements(nodeArray, --big, index);
			}else {
				index++;
			}
		}
		//重新连接链表 
		for(i=1;i != nodeLength;i++) {
			nodeArray[i-1].next=nodeArray[i];
		}
		nodeArray[nodeLength-1].next=null;
		head = nodeArray[0];
		return head;
	}
	
	//数值交换函数
	public void swapElements (Node nodeArray [], int a, int b) {
		
		Node tranElements = null; 
		tranElements nodeArray = [b]; 
		nodeArray [b] = nodeArray [a]; 
		nodeArray [a] = tranElements; 
	}	 
}

  

import java.util.Random;
import java.util.Scanner;

public class TestListPart {
	public static void main(String[] args) {
		ListPart list = new ListPart();
		TestListPart test = new TestListPart();
		//获取初始信息
		Random rand = new Random();	
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入链表长度");
		int K = sc.nextInt();
		System.out.println("请输入位置元素值");
		int privot = sc.nextInt();
		//随机生成链表
		Node nodes[]=new Node[K];
		for(int i=0;i<nodes.length;i++) {
			nodes[i]=new Node(rand.nextInt(20)+1);
		}
		for(int i =0;i<nodes.length-1;i++) {
			nodes[i].next=nodes[i+1];
		}
		Node head = nodes[0];
		//test
		test.showNode(head);
		Node partNode = list.listPart(head, privot);
		test.showNode(partNode);
		
	}
	public void showNode(Node head) {
		System.out.println("链表内的元素如下所示...");
		while(head != null) {
			System.out.print(head.value+"\t");
			head = head.next;
		}
		System.out.println();
	}
}

  

* 运行结果

 

 

Guess you like

Origin www.cnblogs.com/walxt/p/12551712.html