【问题标题】:Add method for Double Linked List (Bubble Sort )双链表的添加方法(冒泡排序)
【发布时间】:2016-07-14 11:45:16
【问题描述】:

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

我的 add 方法,我根据 index 的值从头或尾遍历列表

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");   
}   

而我的冒泡排序,items 是一个列表,可以是几种不同类型的列表,例如数组列表、链接列表、双链接列表。冒泡排序适用于 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

【问题讨论】:

    标签: java list add bubble-sort


    【解决方案1】:

    更新

    大家好,我想通了。在索引为 0 或 = 到 num_items 的情况下,我的删除方法没有更新指针

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-04
      • 2022-01-09
      • 1970-01-01
      • 2011-04-13
      • 2022-01-06
      • 2011-09-04
      • 1970-01-01
      相关资源
      最近更新 更多