java - 添加双链表方法(冒泡排序)

标签 java list addition bubble-sort

您好,我在双链表上执行冒泡排序时遇到问题。冒泡排序适用于不同的列表类型,因此我假设它是正确的。然而,当我在双链表上使用它时,我最终会得到未排序的困惑和重复的节点。我在双链表类中使用添加和删除方法进行排序,所以我认为其中一个或两个一定是错误的。无论如何,这是我的代码,任何帮助将不胜感激。

我的添加方法,我根据索引的值从头或尾遍历列表

public void my_add_element(int index, T element) throws myException{
    if(index <= num_items && index > -1)
    {
        if (index == num_items){
            myNode<T> nodeAtEnd = new myNode<T>(element);
            nodeAtEnd.setLeft(tail); 
            nodeAtEnd.setRight(null);
            if(tail != null)
                tail.setRight(nodeAtEnd); //link the list

            tail = nodeAtEnd; //now the tail is the new node i added

            if(head == null)       // if the list has no elements then set the head
                head = nodeAtEnd;

            num_items++;
        }
        else
        {
            myNode<T> current;
            myNode<T> nodeToInsert =  new myNode<T>(element);
            if(index <= Math.round(num_items/2))
            {
                current = this.head;
                for(int i = 0; i < index; i++)
                {
                    current = current.getRight();
                }
                if(current.getLeft() == null)
                {
                    this.head = nodeToInsert;
                    nodeToInsert.setRight(current);
                    current.setLeft(nodeToInsert);
                    num_items++;
                }
                else
                {
                    current.getLeft().setRight(nodeToInsert);
                    nodeToInsert.setLeft(current.getLeft());
                    nodeToInsert.setRight(current);
                    current.setLeft(nodeToInsert);
                    num_items++;
                }
            }

            else
            {
                current = this.tail;
                for(int i = 0; i < (num_items-(index+1)); i++)
                {
                    current = current.getLeft();
                }
                current.getLeft().setRight(nodeToInsert);
                nodeToInsert.setLeft(current.getLeft());
                nodeToInsert.setRight(current);
                current.setLeft(nodeToInsert);
                num_items++;
            }
        }
    }
    else
        throw new myException("Invalid Index. The ADT does not have such an Index Position");   

}

我的删除方法,还是一样

public void my_remove_element(int index) throws myException{
    if(index < num_items)
    {
        myNode<T> current;
        if(index <= Math.round(num_items/2))
        {
            current = this.head;
            for(int i = 0; i < index; i++)
            {
                current = current.getRight();
            }
            if(current.getLeft() == null)
                this.head = current.getRight();
            else
            {
                current.getRight().setLeft(current.getLeft());
                current.getLeft().setRight(current.getRight());
            }
            num_items--;
        }
        else
        {
            current = this.tail;
            for(int i = 0; i < (num_items-(index+1)); i++)
            {
                current = current.getLeft();
            }
            if(current.getRight() == null)
                this.tail = current.getLeft();
            else
            {
                current.getRight().setLeft(current.getLeft());
                current.getLeft().setRight(current.getRight());
            }
            num_items--;
        }
    }
    //2.2. If the index is a wrong one
    else
        throw new myException("Invalid Index. The ADT does not have such an Index Position");   
}   

对于我的冒泡排序,项目是一个列表,可以是几种不同类型的列表,例如数组列表、链表、双链表。冒泡排序适用于 arrayList 和 LinkedList。

public void bubble_sort(){
    for (int i = 0; i < items.my_get_length()-1; i++)
        for (int j = 0; j < ((items.my_get_length() - 1) - i); j++)
        {
            if (items.my_get_element(j+1).smaller(items.my_get_element(j))) {
                items.my_add_element(j+2, items.my_get_element(j));
                items.my_remove_element(j);
            }
        }
}

items 包含足球运动员详细信息列表(姓名、进球数)。 这是我使用的列表。

姓名:鲁尼,进球:30
姓名:伊布,进球:46
姓名:梅西,进球:80
姓名:阿奎罗,进球:21
姓名:罗纳尔多,进球:89
姓名:穆勒,进球:33
姓名:莱万多夫斯基,进球:30

这就是冒泡排序后的样子

姓名:伊布拉希莫维奇,进球:46
姓名:梅西,进球:80
姓名:伊布,进球:46
姓名:伊布,进球:46
姓名:莱万多夫斯基,进球:30
姓名:鲁尼,进球:30
姓名:阿奎罗,进球:21

最佳答案

更新

大家好,我已经弄清楚了。在索引为 0 或 = num_items 的情况下,我的删除方法不会更新指针

关于java - 添加双链表方法(冒泡排序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36241629/

相关文章:

python - 使用 python 将列表打印到 .txt 文件中的表的有效方法

java - 关于hadoop "java.lang.RuntimeException: java.lang.ClassNotFoundException: "的问题

java - 如何在 iText 7 中用字符填充页面宽度

java - Java 中的抽象类和反射

python - 替换主词典中的用户输入列表元素?

java - 从一个 ArrayList 转移到另一个

java - Dsun.java2d.trace 是如何工作的

Java 克隆数组作为构造函数中的参数

java - ArrayList 列表在不应该的情况下被更改

Java:无法将 int 添加到数组列表