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

时间:2016-08-09 05:17:28

标签: c++ sorting linked-list doubly-linked-list

我试图按照字母顺序将节点添加到双向链表中,相对于节点的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;
            }
        }
    }
}

如何通过列表递增并在基于元素比较的节点添加不起作用?什么是解决方案?

2 个答案:

答案 0 :(得分:1)

正如@verbose所说,你永远不会真正为currTitle分配一个值,尝试在循环开始时使用curr.getTitle(currTitle),或者在其位置使用curr->title

关于按顺序插入,只要头部本身为空,或者标题位于头部之前,您就不必担心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
    }
}

答案 1 :(得分:1)

您正在比较两个字符串,但第二个似乎没有被初始化。据推测aSong.getTitle(title)将当前歌曲的标题复制到title[]数组。但什么时候被复制到currTitle[]

因此currTitle[]的元素是垃圾,并且比较不起作用。要解决此问题,请将要插入的歌曲的标题与列表中已有歌曲的标题进行比较。你可能想要像strcmp(title, curr->title)这样的东西。

当然,在进行比较之前,首先需要检查一首歌是否确实存在。首先检查head是否存在歌曲,如果不存在,则将歌曲作为首歌插入。如果头部已有歌曲,则比较两个标题并确定新歌曲是否应该是新头部,或者是否应该在头部之后的某个位置。其余的逻辑应该是直截了当的。

相关问题