package creat_java;
public class Node<T> {
T value;
Node<T> next;
}
package creat_java;
import shuzu.string_creat;
public class Person2 {
private String name;
private int age;
private boolean gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public String toString() {
return this.name+","+this.age+","+(this.gender?"男":"女");
}
}
package creat_java;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.util.Scanner;
public class TestLinkedPersonTable {
private static Node<Person2> header = null;
private static Scanner scanner = null;
private static Node<Person2> createNode(Person2 value) {
Node<Person2> node = new Node<Person2>();
node.value = value;
return node;
}
private static Person2 createPerson() {
System.out.println("请输入人员的姓名:");
String name = scanner.next();
System.out.println("请输入人员的年龄:");
int age = scanner.nextInt();
System.out.println("请输入人员的性别:");
boolean gender = scanner.nextBoolean();
Person2 value = new Person2();
value.setName(name);
value.setAge(age);
value.setGender(gender);
return value;
}
private static void menu() {
System.out.println("1.增加节点");
System.out.println("2.插入节点");
System.out.println("3.删除节点");
System.out.println("4.更新节点");
System.out.println("5.清空链表");
System.out.println("6.遍历链表");
System.out.println("7.退出");
System.out.println("72.按年龄升序排序");
System.out.println("72.按年龄降序排序");
System.out.println("73.按姓名升序排序");
System.out.println("73.按姓名降序排序");
System.out.println("74.按性别升序排序");
System.out.println("74.按性别降序排序");
System.out.println("81.从文件中加载数据");
System.out.println("82.将数据保存到文件");
}
private static void add() {
Node<Person2> node = createNode(createPerson());
Node<Person2> lst = header;
while (lst.next != null) {
lst = lst.next;
}
lst.next = node;
}
private static void insert() {
System.out.println("请输入插入位置的索引");
int pos = scanner.nextInt();
Person2 value = createPerson();
if(pos < 0) {
System.out.println("索引不能小于0");
}else {
Node<Person2> node = createNode(value);
Node<Person2> lst = header;
while(lst != null && pos-- > 0) {
lst = lst.next;
}
if(lst == null) {
System.out.println("索引超出范围");
}else {
node.next = lst.next;
lst.next = node;
}
}
}
private static void delete() {
System.out.println("请输入删除节点的索引");
int pos = scanner.nextInt();
if(pos < 0) {
System.out.println("索引不能小于0");
}else {
Node<Person2> lst = header;
while(lst != null && pos-- >0) {
lst = lst.next;
}
if(lst == null) {
System.out.println("索引超出范围");
}else {
lst.next = lst.next.next;
}
}
}
private static void update() {
System.out.println("请输入更新节点的索引");
int pos = scanner.nextInt();
Person2 value = createPerson();
if(pos < 0) {
System.out.println("索引不能小于0");
}else {
Node<Person2> lst = header;
while(lst != null && pos-- > 0) {
lst = lst.next;
}
if(lst == null) {
System.out.println("索引超出范围");
}else {
lst.value = value;
}
}
}
private static void clear() {
header.next = null;
}
private static void list() {
int index = 1;
Node<Person2> lst = header.next;
while(lst != null) {
System.out.println("("+ index++ +")" + lst.value);
lst = lst.next;
}
System.out.println();
}
public static void sort(Comparator<Person2> c) {
Node<Person2> header2 = new Node<Person2>();
Node<Person2> lst;
while ((lst=header.next) != null) {
header.next = lst.next;
lst.next = null;
Node<Person2> lst2 = header2;
while (lst.next != null && c.compare(lst.value,lst2.next.value) > 0) {
lst2 = lst2.next;
}
if (lst2.next == null) {
lst2.next = lst;
}else {
lst.next = lst2.next;
lst2.next = lst;
}
}
header2.next = header2.next;
}
private static int Comparable(Integer value, Integer value2) {
return 0;
}
public static void main(String[] args) {
header = new Node<Person2>();
scanner = new Scanner(System.in);
do {
menu();
int choice = scanner.nextInt();
switch(choice) {
case 1:
add();
break;
case 2:
insert();
break;
case 3:
delete();
break;
case 4:
update();
break;
case 5:
clear();
break;
case 6:
list();
break;
case 72:
sort(new Comparator<Person2>(){
@Override
public int compare(Person2 o1,Person2 o2) {
return o2.getAge() - o1.getAge();
}
});
case 73:
sort(new Comparator<Person2>(){
@Override
public int compare(Person2 o1,Person2 o2) {
return o2.getName().compareTo(o1.getName());
}
});
case 74:
sort(new Comparator<Person2>(){
@Override
public int compare(Person2 o1,Person2 o2) {
return Boolean.compare(o1.isGender(), o2.isGender());
}
});
break;
case 81:
File src = new File("new.data");
if(!src.exists()) {
System.out.println("还没有数据");
}else {
BufferedReader reader = null;
try (BufferedReader read=new BufferedReader(new InputStreamReader(new FileInputStream(src)))) {
Node<Person2> lst = header;
while (lst.next != null) {
lst = lst.next;
}
String line;
while ((line = read.readLine()) != null) {
String[] vs =line.split(",");
Person2 person = new Person2();
person.setName(vs[0]);
person.setAge(Integer.valueOf(vs[1]));
person.setGender(Boolean.valueOf(vs[3]));
Node<Person2> node = createNode(person);
lst.next = node;
lst = node;
}
}catch (Exception e) {
System.out.println(e.getMessage());
}
}
break;
case 82:
try (BufferedWriter write =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(
new File("new.data"))))){
Node<Person2> lst = header.next;
while (lst != null) {
write.write(lst.value.toString());
write.write("\r\n");
lst = lst.next;
}
System.out.println();
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
default:
System.out.println("输入错误!");
}
}while(true);
}
}