Java、对链表中的数字进行排序

 

package collection;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

public class Exercise20_02 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        TextField number = new TextField();
        HBox topHBox = new HBox(10, new Label("Enter a number:"), number);  //顶部
        topHBox.setAlignment(Pos.CENTER);

        TextArea textArea = new TextArea(); //中部
        textArea.setPrefRowCount(6);
        textArea.setPrefColumnCount(31);
        textArea.setEditable(false);

        Button sort = new Button("Sort");
        Button shuffle = new Button("Shuffle");
        Button reverse = new Button("Reverse");
        HBox bottomHBox = new HBox(10, sort, shuffle, reverse); //底部
        bottomHBox.setAlignment(Pos.CENTER);

        LinkedList<Integer> linkedList = new LinkedList<>();
        number.setOnAction(event -> {   //按钮注册动作事件
            try {
                add(linkedList, Integer.parseInt(number.getText()));
                addText(linkedList, textArea);
                number.setText("");
            } catch (NumberFormatException ex) {
            }
        });
        sort.setOnAction(event -> {
            Collections.sort(linkedList);
            addText(linkedList, textArea);
        });
        shuffle.setOnAction(event -> {
            Collections.shuffle(linkedList);
            addText(linkedList, textArea);
        });
        reverse.setOnAction(event -> {
            Collections.reverse(linkedList);
            addText(linkedList, textArea);
        });

        BorderPane pane = new BorderPane(new ScrollPane(textArea)); //总面板
        pane.setTop(topHBox);
        pane.setBottom(bottomHBox);

        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Exercise20_02");
        primaryStage.show();
    }

    /** 不重复的值添加进链表 */
    private void add(LinkedList<Integer> linkedList, int value) {
        boolean isTheSame = false;
        for (Integer i : linkedList)    //遍历链表
            if (i.equals(value))    //如果值重复,变量设置为true
                isTheSame = true;
        if (!isTheSame) //值不重复时添加进链表
            linkedList.addLast(value);
    }

    /** 添加链表数据到文本区域 */
    private void addText(LinkedList<Integer> linkedList, TextArea textArea) {
        textArea.clear();   //清除原有文本区域数据
        Iterator<Integer> iterator = linkedList.iterator(); //返回链表迭代器
        while (iterator.hasNext())  //遍历迭代器,追加数据
            textArea.appendText(iterator.next() + " ");
    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基数排序是一种非比较性的排序算法,通过将待排序数字按照个位、十位、百位等位数进行排序,最终得到有序的结果。在使用链表来实现单趟基数排序时,可以按照以下步骤进行操作: 1. 创建一个链表数组,用于存储待排序数字链表数组的数量应当等于基数的个数,例如十进制的基数排序需要创建10个链表。 2. 将待排序数字按照个位数的值分别放入对应的链表。为了实现这一步,可以从头到尾遍历待排序数字链表,取每一个数字的个位数,然后根据个位数的值将数字插入到相应的链表。如果数字的个位数为3,则将其插入到第4个链表(下标从0开始)。 3. 依次遍历链表数组,将每一个链表的元素重新按照十位数的值重新放入对应的链表。这一步的操作与第2步类似,只是将个位数改为十位数。 4. 重复步骤3,直到最高位数的值得链表没有数字。 5. 最后按照从低位到高位的顺序,依次遍历链表数组,将每一个链表的元素依次取出,得到有序的结果。 使用链表来实现单趟基数排序的好处是可以节省空间,因为链表可以动态地分配内存,而不需要预先确定数字的个数。同时,链表的插入和删除操作比较高效,可以有效地进行大量数据的排序。另外,链表还可以很好地处理相同数字的情况,因为相同数字会被放入同一个链表。但是使用链表实现的单趟基数排序算法需要设计合适的数据结构和算法来实现链表排序和合并,以及数字的位置移动等操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值