返回链接列表节点,然后将其删除

时间:2019-03-25 14:38:55

标签: c linked-list return-value free

下面是我编写的一个函数,该函数遍历链表并返回“频率”值最低的节点。但是,在主代码中,我连续两次调用它。我如何拥有它,以便代码返回该节点然后将其删除,以便在再次运行时不会返回与以前相同的节点?令我感到困惑的是,如何删除该节点,如果该节点不再存在,则将其返回。

struct LetterFrequencyPair* lowestF()
{
    int val = 1000;
    struct LetterFrequencyPair* temp;
    struct LetterFrequencyPair* lowest = malloc(sizeof(struct 
LetterFrequencyPair));
    temp = root;
    if (temp == NULL)
    {
        printf("List is empty.\n");
    }
    else
    {
        while (temp != NULL)
        {
            if (temp->frequency < val)
            {
                lowest->character = temp->character;
                lowest->frequency = temp->frequency;
                val = lowest->frequency;
            }
            temp = temp->next;
        }
        printf("%c\t%d\n", lowest->character, lowest->frequency);
        return lowest;
}

0 个答案:

没有答案
相关问题