【问题标题】:how can i sort a linked list in ascending order如何按升序对链表进行排序
【发布时间】:2014-12-27 17:30:57
【问题描述】:

这是我尝试过的......

这只会交换第一个链接...我该如何解决这个问题以对整个链接列表进行排序????

链表类

public class LinkedList {


private Link first;

public void LinkList() {

    first = null;

}public Link find(int key) {
    Link current = first;

    while (current.iData != key) {
        if (current.next == null) {
            return null;
        } else {
            current = current.next;

        }
    }
    return current;
}

public void insertFirst(int idata, String sdata) {
    Link nl1 = new Link(idata, sdata);

    nl1.next = first;
    first = nl1;
}

public Link deleteFirst() {

    Link temp = first;
    first = first.next;
    return temp;
}

public void displayList() {
    System.out.println("List : ");
    Link current = first;
    while (current != null) {
        current.displayLink();
        current = current.next;
    }
    System.out.println("");
}

public Link delete(int key) {
    Link current = first;
    Link previous = first;

    while (current.iData != key) {
        if (current.next == null) {
            return null;
        } else {
            previous = current;
            current = current.next;

        }

    }
    if (current == first) {
        first = first.next;
    } else {
        previous.next = current.next;

    }
    return current;
}

public void insertmidl(int key, int idata, String sdata) {

    Link nl = new Link(2, "name2");
    Link current = first;
    Link previous = first;

    while (current.iData != key) {
        if (current.next == null) 
        {
            System.out.println("");
        } else {
            previous = current;
            current = current.next;

        }

    }

    if (current == first) {

        nl.next = current.next;
        current.next = nl;

    }
    previous.next = nl;
    nl.next = current;

}

public void update(int key, int i, String n) {

    Link tobeupdated = find(key);

    tobeupdated.iData = i;
    tobeupdated.sData = n;


}
public void sort(){

            Link current = first;

   if(current.iData > current.next.iData) {

        Link temp = current.next;
        current.next = current.next.next;
        temp.next = current;
        first = temp;

    }
}

链接类

public class Link {
public int iData;
public String sData;
public Link next;

public Link(int id,String sd)
{
iData =id;
sData =sd;
next = null;
}
public void displayLink()
{
System.out.println(iData+""+sData); 
}
}

有没有人可以帮助我? ..................................................... ....................

【问题讨论】:

  • 从链表创建一个 BST。执行中序遍历。基于该 InOrder 遍历创建 LinkedList。

标签: java algorithm sorting linked-list


【解决方案1】:

本示例向您介绍如何使用 Comparator 对 LinkedList 进行排序。 LinkedList 包含用户定义的对象。通过使用 Collections.sort() 方法,您可以对 LinkedList 进行排序。您必须传递包含您的排序逻辑的 Comparator 对象。该示例根据最高薪水对 Empl 对象进行排序。

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;

public class MyLinkedListSort {

    public static void main(String a[]){

        LinkedList<Empl> list = new LinkedList<Empl>();
        list.add(new Empl("Ram",3000));
        list.add(new Empl("John",6000));
        list.add(new Empl("Crish",2000));
        list.add(new Empl("Tom",2400));
        Collections.sort(list,new MySalaryComp());
        System.out.println("Sorted list entries: ");
        for(Empl e:list){
            System.out.println(e);
        }
    }
}

class MySalaryComp implements Comparator<Empl>{

    @Override
    public int compare(Empl e1, Empl e2) {
        if(e1.getSalary() < e2.getSalary()){
            return 1;
        } else {
            return -1;
        }
    }
}

class Empl{

    private String name;
    private int salary;

    public Empl(String n, int s){
        this.name = n;
        this.salary = s;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public String toString(){
        return "Name: "+this.name+"-- Salary: "+this.salary;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    相关资源
    最近更新 更多