|
发表于 2020-3-11 23:00:01
|
显示全部楼层
#include <stdio.h>
int main()
{
typedef struct Node
{
int data;
Node *next;
}Node;
Node *head,*tail;
head=new Node;
head->data=1;
head->next=NULL;
tail=head;
for(int i=2;i<=100;i++)
{
Node *temp=new Node;
temp->data=i;
temp->next=NULL;
tail->next=temp;
tail=temp;
}
tail->next=head;
Node *temp=head;
Node *temp1,*temp2;
int count=0;
while(1)
{
temp1=temp->next->next;
temp2=temp1->next;
temp1->next=temp2->next;
temp=temp1;
count++;
if(temp2->data == 1)
{
printf("经过%d次删除1\n",count);
// break;
}
if(count == 100)
{
printf("最后删除的是:%d\n",temp2->data);
break;
}
delete temp2;
}
delete temp2;
return 0;
}
求得结果为:经过98次删除1,最后一个被删除的是92。 |
|