|
#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 ("Delete 1\n after% d times", count);
// break;
}
if (count == 100)
{
printf ("The last deleted is:% d\n", temp2-> data);
break;
}
delete temp2;
}
delete temp2;
return 0;
}
The result is: after 98 deletions of 1, the last deleted is 92. |
|