-4
struct Node {
    float  item;
    Node * next;
    Node * previous;
};

我正在尝试insert为此双链表编写一个函数,该函数将浮点数据插入到使用标题的列表上的位置 posbool insert (int pos, float data);如果插入成功,该函数应返回 true,否则返回 false

但是我正在尝试复制我在网上看到的东西,但我没有成功,有人可以给我看一个示例代码,也许可以理解或了解如何做到这一点吗?

这是我到目前为止得到的。如果插入成功,该函数应返回 true,否则返回 false。但我不认为我接近这个权利

  bool insert ( int pos, float data)
{
    if(pos< 1||pos> 1) throw...
        if (pos ==1)
        {
            ListNode* node=new Listnode;
            node-> data=item;
            node-> next-head;
            if(head!=NULL)
                head->prev=node;
            node-> prev=NULL;
            head=node;
            ++count;
        }
4

1 回答 1

2

像这样让你开始

struct Node {
    float  item;
    Node * next;
    Node * previous;
};

class DoublyLinkedList
{
public:
    DoublyLinkedList();
    bool insert(int pos, float data);
private:
    Node* head;
    int count;
};

bool DoublyLinkedList::insert(int pos, float data)
{
    ...
}

但我认为在你完成这个之前,你需要学习很多关于 C++ 的知识。也许最好在网上看一个完整的例子。

于 2012-10-25T12:45:25.827 回答