简体   繁体   English

找出链表的插入功能参数?

[英]Figuring out Insert function parameter for linked list?

My teacher gave the class for a driver to complete a program, and I'm unsure how to code the insert function because of it. 我的老师为驾驶员提供了完成程序的课程,因此我不确定如何编写插入函数。

The line giving me trouble: 这行给我带来麻烦:

you.Insert(me,0);

you is for the default constructor and me is for an explicit value constructor, so this line is supposed create a node in you with the contents of me . you是默认构造函数,而me是显式值构造函数,因此该行应使用me的内容在you体内创建一个节点。

I'm lost understanding how to write the parameter to access me for my insert function 我无法理解如何编写参数以访问我的插入功能

void WRD::Insert( ?, int new_data)

I'll include the explicit constructor I have, any insight understanding this mentally will help. 我将包括我拥有的显式构造函数,任何对此有深刻理解的见识都会有所帮助。 (included example of what insert should look like or do based on an example I was given.) (随附的示例基于我给出的示例应该具有什么样的insert 。)

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


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

v 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;
        }
    }
}

Like this I think (assuming I've understood you right) 我想是这样的(假设我对你没错)

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

It might help to show more of your driver program, in particular how you and me are declared. 可能有助于显示更多驱动程序,特别是如何声明youme身份。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM