我试图在双向链表的末尾插入一个值,我成功地将值插入到头或第一个节点,但第二个值没有被插入
这里的问题是在输入第二个值时
class d_list
{
private:
struct node
{
double data;
node *next;
node *previous;
};
node *first;
node *last ;
public:
d_list(void)
{
first = nullptr;
last = nullptr;
};
void append(double);
};
void d_list::append(double num)
{
node *ptr;
node *toinsert;
if(!first)
{
first = new node;
first->previous= nullptr;
first->data = num;
last= new node;
first->next= last->previous;
last->previous = first->next;
last->next= nullptr;
}
else
{
if(last->next == nullptr)
{
ptr = new node;
ptr->next =last->previous;
ptr->data=num;
last->previous = ptr->next ;
}
last->next= nullptr;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
d_list aa;
cout<<"going to append first"<<endl;
aa.append(44);
cout<<"going to append second"<<endl;
aa.append(50.5);
return 0;
}