创建类对象的有序链接列表

时间:2014-03-29 03:00:19

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

bool CharacterList::addCharacter(Character *newCharacter)
{
    Character *temp, *back;

    if(head == NULL)
    {
        head = newCharacter;
    }

    else
    {
        temp = head;
        back = NULL;
        while((temp != NULL) &&  (temp < newCharacter))
        {
            back = temp;
            temp = temp.next;
        }

        if(back == NULL)
        {
            newCharacter.next = head;
            head = newCharacter;
        }

        else
        {
            back.next = newCharacter;
            newCharacter.next = temp;
        }

        return true;
    }

}

我为类Character的对象创建有序链表(CharacterList)。此函数只接受一个参数,一个指向Character类对象(* newCharacter)的指针。然后,它会将此字符添加到角色对象的链接列表中。我不确定这是否是我将对象插入链表的方式。有人可以指导我吗?

1 个答案:

答案 0 :(得分:3)

不应重新发明自己,而应使用std::list,如下所示:

std::list<Character> CharacterList;

但请在继续之前请read a good book