从主列表中删除无序列表

时间:2017-11-03 14:34:19

标签: c++ sorting

  

我有一个列表:4, 5, 6, 3, 1, 2

我必须删除 3号,因为它是无序的。这就是为什么我有:

  

第一个清单:4,5,6

     

第二个:1,2

结果我应该得到:

  

4,5,6,1,2

或另一个例子:

  

我有:1,2, 2,2 ,2,3, 2,5

     

需要删除2,2,2,5。因为它们是无序的。

     

结果:1​​,2,2,3

不幸的是,试图找到一个排序函数,它对我的​​列表进行排序,但无法猜测我是如何以正确的方式比较我的数字。为了正确的比较,请你给我一个提示,一个例子或一些解释。

我的隐藏功能

E.X。我已输入:4, 5, 6, 3, 1, 2 得到:1,2,3,4,5,6

struct list * Ssort(struct list *root)
{
    struct list *new_root = NULL;

    while (root != NULL)
    {
        struct list *node = root;
        root = root->next;

        if (new_root == NULL || node->data < new_root->data)
        {
            node->next = new_root;
            new_root = node;
        }
        else
        {
            struct list *current = new_root;
            while (current->next != NULL && !(node->data < current->next->data))
            {
                current = current->next;
            }

            node->next = current->next;
            current->next = node;
        }
    }

    return new_root;
}

0 个答案:

没有答案
相关问题