如何在Java中对自定义链表进行排序?
我正在基于Crunchify的实现开发一个自定义链接列表,以显示员工列表。现在,我可以添加新员工或从列表中删除现有员工。但是,我的项目需要添加不基于集合的排序方法。sort()。我的老师希望这种分类方法是定制的,所以这对我来说相当困难。有没有按照易于编码的名字对列表进行排序的方法(我对面向对象编程一无所知) 这是我的自定义链接列表:如何在Java中对自定义链表进行排序?,java,sorting,linked-list,comparable,Java,Sorting,Linked List,Comparable,我正在基于Crunchify的实现开发一个自定义链接列表,以显示员工列表。现在,我可以添加新员工或从列表中删除现有员工。但是,我的项目需要添加不基于集合的排序方法。sort()。我的老师希望这种分类方法是定制的,所以这对我来说相当困难。有没有按照易于编码的名字对列表进行排序的方法(我对面向对象编程一无所知) 这是我的自定义链接列表: import java.util.Scanner; import java.io.IOException; public class MyLinkedListTe
import java.util.Scanner;
import java.io.IOException;
public class MyLinkedListTest2 {
public static MyLinkedList linkededList;
public static void main(String[] args) {
linkededList = new MyLinkedList();
linkededList.add(new Employee("Agness", "Bed", 2000.0, 32));
linkededList.add(new Employee("Adriano", "Phuks", 4000.0, 16));
linkededList.add(new Employee("Panda", "Mocs", 6000.0, 35));
System.out.println(linkededList);
//OPTIONS
Scanner scanner = new Scanner(System.in);
int selection;
do {
System.out.println("OPTIONS:\n[1] ADD EMPLOYEE\n[2] REMOVE EMPLOYEE\n[3] SORT \n[4] EXIT\n");
selection = scanner.nextInt();
switch (selection) {
case 1:
System.out.println("Name:");
scanner.nextLine();
String name = scanner.nextLine();
System.out.println("Surname:");
String surname = scanner.nextLine();
System.out.println("Salary:");
double salary = scanner.nextDouble();
System.out.println("Experience:");
int experience = scanner.nextInt();
linkededList.add(new Employee(name, surname, salary, experience));
System.out.println(linkededList);
break;
case 2:
System.out.println("Which row do you want to remove?");
int choice = scanner.nextInt();
if (choice == 0)
System.out.println("No such row exists");
else if (choice > linkededList.size())
System.out.println("No such row exists");
else
linkededList.remove(choice - 1);
System.out.println(linkededList);
break;
case 3:
System.out.println("SORT BY: 1.NAME\t2.SURNAME\t3.SALARY\t4.EXPERIENCE\n");
//In this section sorting algorithm should be added
break;
case 4:
break;
default:
System.out.println("Wrong choice");
}
} while (selection != 4);
}
}
class MyLinkedList<Employee> {
private static int counter;
private Node head;
public MyLinkedList() {
}
public void add(Object data) {
if (head == null) {
head = new Node(data);
}
Node myTemp = new Node(data);
Node myCurrent = head;
if (myCurrent != null) {
while (myCurrent.getNext() != null) {
myCurrent = myCurrent.getNext();
}
myCurrent.setNext(myTemp);
}
incrementCounter();
}
private static int getCounter() {
return counter;
}
private static void incrementCounter() {
counter++;
}
private void decrementCounter() {
counter--;
}
public void add(Object data, int index) {
Node myTemp = new Node(data);
Node myCurrent = head;
if (myCurrent != null) {
for (int i = 0; i < index && myCurrent.getNext() != null; i++) {
myCurrent = myCurrent.getNext();
}
}
myTemp.setNext(myCurrent.getNext());
myCurrent.setNext(myTemp);
incrementCounter();
}
public Object get(int index){
if (index < 0)
return null;
Node myCurrent = null;
if (head != null) {
myCurrent = head.getNext();
for (int i = 0; i < index; i++) {
if (myCurrent.getNext() == null)
return null;
myCurrent = myCurrent.getNext();
}
return myCurrent.getData();
}
return myCurrent;
}
public boolean remove(int index) {
if (index < 1 || index > size())
return false;
Node myCurrent = head;
if (head != null) {
for (int i = 0; i < index; i++) {
if (myCurrent.getNext() == null)
return false;
myCurrent = myCurrent.getNext();
}
myCurrent.setNext(myCurrent.getNext().getNext());
decrementCounter();
return true;
}
return false;
}
public int size() {
return getCounter();
}
public String toString() {
String output = "";
if (head != null) {
Node myCurrent = head.getNext();
while (myCurrent != null) {
output += myCurrent.getData().toString();
myCurrent = myCurrent.getNext();
}
}
return output;
}
public void compare(int index){
Node myCurrent = head.getNext();
if(myCurrent != myCurrent.getNext())
myCurrent = head;
else
myCurrent = myCurrent.getNext();
}
private class Node {
Node next;
Object data;
public Node(Object dataValue) {
next = null;
data = dataValue;
}
@SuppressWarnings("unused")
public Node(Object dataValue, Node nextValue) {
next = nextValue;
data = dataValue;
}
public Object getData() {
return data;
}
@SuppressWarnings("unused")
public void setData(Object dataValue) {
data = dataValue;
}
public Node getNext() {
return next;
}
public void setNext(Node nextValue) {
next = nextValue;
}
}
}
代码现在正在编译,但是您对链表的实现有什么建议吗?如果有人能提出一个排序的解决方案,我将不胜感激,因为这样我的项目就可以完成了。只能使用Comparable,而Collections.sort()方法由于项目的要求而无法实现。您可以定义自己的EmployeeComparator,该Comparator实现了
Comparator
(请参阅),并按如下方式使用:
SortedSet<Employee> set = new TreeSet<Employee>(new EmployeeComparator());
set.addAll(employees);
SortedSet set=new TreeSet(new EmployeeComparator());
set.addAll(员工);
因为您需要自己实现排序,所以最简单的方法之一是将每个列表节点与下一个进行比较,如果它们没有按排序顺序进行交换。您需要执行此操作,直到列表中剩下任何此类无序节点为止
您可以查看bubble sort实现,了解其工作原理。请访问并阅读以了解如何有效使用此网站。询问“关于链表实现的建议”和要求某人提供代码的问题被认为是离题的。你能再解释一下为什么不能使用Collection.sort吗?是因为你的项目想看看你是如何实现排序算法的吗?是的,我可以。我的老师希望这种排序方法是定制的,所以它必须使用列表逻辑。我认为Collections.sort在这种情况下不起作用。您好,谢谢您的建议。我现在正在挖掘气泡排序实现。但是,在本例中,如何通过员工的名字来比较每个节点。我是否应该将每个节点移动到一个数组中,然后使用排序?在链接列表中,您有“对象数据;”由于您在此处存储的是Employee类型的对象,您还可以将其设置为“Employee data”,这样就可以访问Employee的字段。您不需要另一个数组,您需要一种方法来交换列表中的两个项目,这样,如果当前顺序是a->C->B,您可以交换任意两个项目,使其类似于a->B->C。请注意,节点a、B和C的下一个项目在此处更改。你可以在网上找到这类代码的好例子。
SortedSet<Employee> set = new TreeSet<Employee>(new EmployeeComparator());
set.addAll(employees);