簡體   English   中英

C ++:按字母順序將節點添加到雙向鏈表

[英]C++: Adding node to doubly linked list in alphabetical order

我試圖相對於節點的title元素按字母順序將節點添加到雙向鏈表中。 到目前為止,我已經嘗試遍歷列表並檢查大小寫(strcmp(title, currTitle) > 0) 但是,迭代不起作用(由於我不確定的原因),因為下面列出的嘗試不會將任何項目添加到鏈接列表中。

如果沒有通過嘗試排序實現,則if-else塊的四個條件集成功地將項目添加到列表中。

void SongList::addSongSorted(const Song &aSong)
{
    //sort inputs to the list by alphabetical order of the entire title
    char title[MAX_CHAR];
    char currTitle[MAX_CHAR];
    aSong.getTitle(title);

    Node * newNode = new Node(aSong);

    Node * curr = head;
    //Increment through list as added...this does not work
    for(curr = head; curr; curr = curr->next)
    {

        if(strcmp(title, currTitle) > 0)
        {

            if(!head)
            {
                head = newNode;
                tail = newNode;
                head->next = NULL;
                head->prev = NULL;
            }
            else if(head->prev == NULL)
            {
                newNode->next = head;
                head = newNode;
                newNode->prev = NULL;
            }

            else if(head->next == NULL)
            {
                curr->next = newNode;
                newNode->prev = curr;
                newNode->next = NULL;
                tail = newNode;
            }

            else
            {
                newNode->next = curr->next;
                newNode->prev = curr;
            }
        }
    }
}

通過元素的比較在列表中遞增並在節點處添加怎么不起作用? 有什么解決辦法?

正如@verbose所說,您永遠不會真正為currTitle分配值,請嘗試在循環開始時使用curr.getTitle(currTitle) ,或在其位置使用curr->title

關於按順序插入,僅當head本身為null或標題位於head之前時,您才不必擔心head的先前值。 您應該遍歷列表,如果標題位於當前節點之后,並且下一個節點為null,則將curr設置為下一個元素(在for循環中完成),然后再次檢查。 如果下一個元素為NULL,並且標題在當前元素之后,則您已經到達列表的末尾,可以插入歌曲並中斷播放。 如果歌曲名稱在當前歌曲之前或相等,則在新歌曲之前插入新歌曲並返回。 下面的示例代碼:

if(!head)
{
    // set head to currnode, as you have
    head = newNode;
    tail = head;
    next = NULL;
    prev = NULL;
    return; // no need to iterate through the list
}
bool inserted = false;
while (!inserted) 
{
    curr->song.getTitle(currTitle);
    if(strcmp(title, currTitle) > 0)
    {
        // If the title goes after the current title 
        if(curr->next == NULL)
        {
            // End of the list, insert after this node
            curr->next = newNode;
            newNode->prev = curr;
            newNode->next = NULL;
            tail = newNode;
            inserted = true;
        }
        // If we don't insert here, we iterate again
        curr = curr->next;
    }
    else 
    {
        // If the title is the same, or comes before the current title, insert before curr
        newNode->prev = curr->prev;
        newNode->next = curr;
        curr->prev = newNode;
        if (curr == head)
        {
            head = newNode;
        }
        inserted = true
    }
}

您正在比較兩個字符串,但第二個字符串似乎從未初始化過。 大概是aSong.getTitle(title)將當前歌曲的title[]復制到title[]數組。 但是什么時候復制了到currTitle[]

因此currTitle[]的元素是垃圾,因此比較無法正常進行。 要解決此問題,請將您要插入的歌曲的標題與列表中已經存在的歌曲的標題進行比較。 您可能想要類似strcmp(title, curr->title)

當然,在進行比較之前,您首先需要檢查一首歌曲是否確實存在。 首先檢查一首歌曲是否存在於head ,如果不存在,則將該歌曲插入為head歌曲。 如果頭上已經有一首歌,則比較兩個標題,並確定新歌應該是新頭,還是應該放在頭之后。 其余邏輯應該很簡單。

暫無
暫無

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

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