C++ C++;:按字母顺序将节点添加到双链接列表

C++ C++;:按字母顺序将节点添加到双链接列表,c++,sorting,linked-list,doubly-linked-list,C++,Sorting,Linked List,Doubly Linked List,我试图按照节点标题元素的字母顺序将节点添加到双链接列表中。到目前为止,我已经尝试遍历列表并检查案例(strcmp(title,currtTitle)>0)。但是,迭代不起作用(原因我不确定),因为下面列出的尝试没有将任何项目添加到链接列表中 if-else块的四个条件集在未尝试进行排序时成功地将项目添加到列表中 void SongList::addSongSorted(const Song &aSong) { //sort inputs to the list by alphab

我试图按照节点标题元素的字母顺序将节点添加到双链接列表中。到目前为止,我已经尝试遍历列表并检查案例
(strcmp(title,currtTitle)>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
之前的值,只要head本身为null,或者标题在head之前。您应该遍历列表,如果标题位于当前节点之后,而下一个节点为null,则将curr设置为下一个元素(在for循环中完成),然后再次检查。如果下一个元素为空,且标题位于当前元素之后,则表示您已到达列表的末尾,可以插入歌曲,然后中断。 如果歌曲标题在当前歌曲之前,或与当前歌曲标题相等,则在当前歌曲之前插入新歌并返回。示例代码如下:

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[]
数组中。但是什么时候有任何东西被复制到
currTitle[]

因此,
currTitle[]
的元素都是垃圾,比较也不起作用。要解决此问题,请将要插入的歌曲的标题与列表中已有歌曲的标题进行比较。您可能需要类似于strcmp(title,curr->title)的东西


当然,在进行比较之前,首先需要检查歌曲是否确实存在。首先检查
首歌是否存在,如果不存在,则插入该首歌作为首歌。如果头上已经有一首歌,比较这两个标题,确定新歌是应该是新头,还是应该放在头后面。其余的逻辑应该很简单。

为什么要使用字符数组?使用
std::string
@Inline这是一个家庭作业任务你不需要处理
strcmp(title,currttitle)@GMichael这个想法是检查列表中的所有元素,并在达到该条件时启动插入。@0x1000001如果第一次插入在丢失的情况下会发生什么?我已根据您的建议更改了我的代码。但是,它不允许将整个列表输入到列表中,只对部分条目进行排序。在粘贴箱中输入和输出:字符串是否打印在那里以及歌曲标题?是的,歌曲#是正在比较的歌曲标题。根据我对您的输出的了解,您仍然只能在
strcmp>0
这是不正确的情况下添加。你添加了else语句了吗?当你进入歌曲2时,它应该在1和7之间插入,但是除非你考虑
strcmp,否则不会发生这种情况。我已经添加了else语句——粘贴库中的I/O来自对代码的测试。