【发布时间】:2020-09-13 01:44:55
【问题描述】:
我试图在链表末尾插入元素,但如果我在 while 循环中注释中断,它会进入一个连续循环,我无法弄清楚原因
代码:
head=NULL;
node *temp=head;
for(int i=0;i<5;i++)
{
//temp=head;
node* t1=new node;
if(head==NULL)
{
t1->a=i;
t1->next=NULL;
head=t1;
}
else
{
temp=head;
while(temp!=NULL)
{
if(temp->next==NULL)
{
t1->a=i;
t1->next=NULL;
temp->next=t1;
//break;
}
temp=temp->next;
}
}
}
temp=head;
while(temp!=NULL)
{
cout<<temp->a<<endl;
temp=temp->next;
}
【问题讨论】:
标签: c++ linked-list