簡體   English   中英

雙向鏈表(氣泡排序)的添加方法

[英]Add method for Double Linked List (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,doubleLinkedList。 冒泡排序適用於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);
            }
        }
}

項目包含足球運動員詳細信息(姓名,進球數)的列表。 這是我使用的列表。

名稱:魯尼,進球數:30
姓名:易卜拉欣莫維奇,進球:46
名稱:梅西,進球:80
名稱:阿奎羅(Aguero),進球:21
名稱:羅納爾多,進球:89
姓名:穆勒,進球數:33
姓名:萊萬多夫斯基,進球:30

這就是氣泡排序后的樣子

姓名:易卜拉欣莫維奇,進球:46
名稱:梅西,進球:80
姓名:易卜拉欣莫維奇,進球:46
姓名:易卜拉欣莫維奇,進球:46
姓名:萊萬多夫斯基,進球:30
名稱:魯尼,進球數:30
名稱:阿奎羅(Aguero),進球:21

更新

你好,我弄清楚了。 我的remove方法在索引為0或= num_items的情況下未更新指針

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM