弄清楚链表的插入函数参数

Figuring out Insert function parameter for linked list?

本文关键字:函数 参数 插入 链表 弄清楚      更新时间:2023-10-16

我的老师给一个司机上课来完成一个程序,我不确定如何编写插入函数。

给我带来麻烦的台词:

you.Insert(me,0);

you 用于默认构造函数,me 用于显式值构造函数,因此此行应该创建一个包含 me 内容的 you 节点。

我迷失了如何编写参数来访问我的插入函数

void WRD::Insert( ?, int new_data)

我将包括我拥有的显式构造函数,任何在心理上理解这一点的见解都会有所帮助。(包括insert应该是什么样子或根据我给出的示例做什么的示例。

WRD::WRD(const string & s)
{
    cout<<"one calledn";
    front = 0;
    for(unsigned i=0; i<s.length(); i++)
    {
        AddChar(s[i]);
    }
}

class node
{
public:
    char symbol;  
    node *   next; 
};

v

oid Insert(node * &ptr, int new_data)
{
    node *new_ptr = new node;
    new_ptr -> data = new_data;
    new_ptr -> next = 0;  //always initialize a pointer
    if (Empty(ptr))
    {
        ptr = new_ptr;
    }
    else if (new_ptr->data <= ptr->data)
    {
        new_ptr->next = ptr;
        ptr = new_ptr;
    }
    else
    {
        node *fwd_ptr=ptr, *pre_ptr=ptr;
        while(fwd_ptr!=0 && (fwd_ptr->data < new_ptr->data))
        {
            pre_ptr = fwd_ptr;
            fwd_ptr = fwd_ptr->next;
        }
        if (fwd_ptr == 0)
        {
            pre_ptr->next = new_ptr;
        }
        else
        {
            new_ptr->next = fwd_ptr;
            pre_ptr->next = new_ptr;
        }
    }
}

像这样我想(假设我理解你没错(

void WRD::Insert(const WRD& w, int new_data)

展示更多驱动程序程序可能会有所帮助,尤其是如何声明youme