我的老师给了班级一个驱动程序来完成一个程序,我不确定如何编写插入函数因为它。
这条线给我带来了麻烦:
you.Insert(me,0);
you
用于默认构造函数,me
用于显式值构造函数,因此该行应在you
中创建一个内容为me
的节点。
我无法理解如何编写参数来访问我的插入函数
void WRD::Insert( ?, int new_data)
我将包含我所拥有的显式构造函数,任何洞察力理解这种心理上的帮助。 (包括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
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;
}
}
}
答案 0 :(得分:0)
就像我想的那样(假设我已经理解你了)
void WRD::Insert(const WRD& w, int new_data)
显示更多的驱动程序可能会有所帮助,特别是you
和me
的声明方式。