generated from msb_29139/520
parent
b80ed3e553
commit
d6f22d3f04
@ -0,0 +1,62 @@
|
||||
package algorithm;
|
||||
|
||||
import common.Node;
|
||||
|
||||
public class C21_Node {
|
||||
|
||||
public static void main(String args[]) {
|
||||
System.out.println("开始:");
|
||||
for (int i = 0; i < 20; i++) {
|
||||
Node head = createRandomNode(8, 6);
|
||||
printNodes(head);
|
||||
System.out.print(" ===>> ");
|
||||
Node node = removeValue(head, 5);
|
||||
printNodes(node);
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static void printNodes(Node head) {
|
||||
Node pre = head;
|
||||
while (pre != null) {
|
||||
System.out.print(pre.value + " ");
|
||||
pre = pre.next;
|
||||
}
|
||||
}
|
||||
|
||||
public static Node createRandomNode(int len, int max) {
|
||||
Node head = new Node(randomNum(max));
|
||||
Node next = head;
|
||||
for (int i = 1; i < len; i++) {
|
||||
int n = randomNum(max);
|
||||
next.next = new Node(n);
|
||||
next = next.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
// 生成一个 [0, n) 的随机数
|
||||
public static int randomNum(int n) {
|
||||
return (int) (Math.random() * n);
|
||||
}
|
||||
|
||||
public static Node removeValue(Node head, int num) {
|
||||
while (head != null) {
|
||||
if (head.value != num) {
|
||||
break;
|
||||
}
|
||||
head = head.next;
|
||||
}
|
||||
Node pre = head;
|
||||
Node cur = head;
|
||||
while (cur != null) {
|
||||
if (cur.value == num) {
|
||||
pre.next = cur.next;
|
||||
} else {
|
||||
pre = cur;
|
||||
}
|
||||
cur = cur.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package common;
|
||||
|
||||
public class Node {
|
||||
public int value;
|
||||
public Node next;
|
||||
|
||||
public Node(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package common;
|
||||
|
||||
public class Student {
|
||||
|
||||
private int id;
|
||||
private int age;
|
||||
private String name;
|
||||
|
||||
public Student() {
|
||||
}
|
||||
|
||||
public Student(int id, int age, String name) {
|
||||
this.id = id;
|
||||
this.age = age;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package heap;
|
||||
|
||||
|
||||
import common.Student;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class C01_Comparator {
|
||||
|
||||
public static class Student {
|
||||
|
||||
public int id;
|
||||
public int age;
|
||||
public String name;
|
||||
|
||||
public Student(int id, int age, String name) {
|
||||
this.id = id;
|
||||
this.age = age;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较器代码
|
||||
*/
|
||||
public static class OrderAsc implements Comparator<Student> {
|
||||
|
||||
@Override
|
||||
public int compare(Student o1, Student o2) {
|
||||
return o1.id == o2.id ? o1.age - o2.age : o1.id - o2.id;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Student s1 = new Student(1, 21, "A");
|
||||
Student s2 = new Student(2, 30, "B");
|
||||
Student s3 = new Student(3, 21, "C");
|
||||
|
||||
Student[] s = new Student[]{s1, s2, s3};
|
||||
Arrays.sort(s, new OrderAsc());
|
||||
System.out.println("====1====");
|
||||
for (Student c : s) {
|
||||
System.out.println("name: " + c.name + ", id: " + c.id + ", age: " + c.age);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package heap;
|
||||
|
||||
|
||||
public class C02_heap {
|
||||
|
||||
|
||||
}
|
Loading…
Reference in new issue