将节点插入到没有头指针的排序列表中

时间:2015-12-13 10:32:58

标签: c++ algorithm linked-list singly-linked-list

有人可以改善这个答案吗?我相信AddNode函数可以很小。将节点插入到已排序的链表中是一个问题,但有一点需要注意。你没有头指针。因此,如果节点数据小于头部,则必须将数据交换到类中。

public static void main (String[] args) {
    ...
    UserManagement.getInstance().getUsers().add("winkum");
    UserManagement.getInstance().getUsers().add("blinkum");
    UserManagement.getInstance().getUsers().add("nod");
    frame.getContentPane().add (new UserHistory(UserManagement.getInstance().getUsers()));
    ...
}

2 个答案:

答案 0 :(得分:1)

首先测试值是否小于头并创建新头。 如果value大于head iterate,直到next元素比head更大,并且之前插入。

class SList
{
public:
    SList(int value = 0,
                 SList* n = nullptr) :
        foo(value), pNext(n)
    {
    }

    void Output() 
    { 
        cout << foo;  
        if (nullptr != pNext) 
        {
            cout << ", ";
            pNext->Output(); 
        }
    }  

    void AddNode(int value)
    {
        SList* head = this;

        // Insert to front
        if (value < head->foo)
        {
            SList* pNode = new SList(foo);
            pNode->pNext = this->pNext;
            this->pNext = pNode;
            foo = value;
            return;
        }

        while ( head->pNext && head->pNext->foo < value )
            head = head->pNext;

        SList* pNode = new SList(value);
        pNode->pNext = head->pNext;
        head->pNext = pNode;
    }

protected:
    int         foo;
    SList*      pNext;
};

void sortedListTest()
{
    SList* list = new SList(5);

    cout << endl;
    list->AddNode(19);
    list->AddNode(3);
    list->AddNode(8);
    list->AddNode(12);
    list->AddNode(33);
    list->AddNode(9);
    list->AddNode(1);
    list->AddNode(23);
    list->Output();
    cout << endl;
}

答案 1 :(得分:0)

另一个版本:

基本上是复制元素(必须在其之后插入)并更新该副本的下一个指针和数据。需要处理特殊情况。

#include<iostream>
using namespace std;

class SList
{
public:
    SList(int value = 0,
          SList* n = nullptr) :
        foo(value), pNext(n)
    {
    }

    void Output() 
    { 
        cout << foo;  
        if (nullptr != pNext) 
        {
            cout << ", ";
            pNext->Output(); 
        }
    }  

    void AddNode(int value)
    {
        SList* current = this;
        SList* prev = NULL;

        while( current && current->foo < value)
        {
            prev = current;
            current = current->pNext;
        }

        if(prev)
        {
            SList *newNode =  new SList(*prev);
            newNode->foo = value;
            prev->pNext = newNode;
        }
        else
        {
            SList *newNode =  new SList(*current);
            current->foo = value;
            current->pNext = newNode;
        }



    }

protected:
    int         foo;
    SList*      pNext;
};

int main()
{
    SList* list = new SList(5);

    cout << endl;
    list->AddNode(19);
    list->AddNode(3);
    list->AddNode(8);
    list->AddNode(12);
    list->AddNode(33);
    list->AddNode(9);
    list->AddNode(1);
    list->AddNode(23);
    list->Output();
    cout << endl;
}