有一个数组a[1000]存放0-1000,要求每隔二个数删除一个数,到末尾时循环到开头继续进行,求最后一个被删掉数的原始下标。
看到题目可以用循环链表保存这些数,然后循环删除,大大减少了一些复杂的边界判断。
下面上代码,看链表建立和删除的具体过程:
#include <stdio.h>
#include <stdlib.h>
typedef struct stLIST
{
int index;
stLIST *next;
}LIST, *PLIST;
//创建循环链表
PLIST CreateRoundList(int size)
{
PLIST head = NULL;
PLIST temp = (PLIST)malloc(sizeof(LIST));
temp->index = 0;
temp->next = NULL;
head = temp;
for (int i = 1; i < size; i++)
{
temp->next = (PLIST)malloc(sizeof(LIST));
temp = temp->next;
temp->index = i;
temp->next = NULL;
}
//尾部指向头部,构造循环链表
temp->next = head;
return head;
}
PLIST DeleteNode(PLIST head)
{
//判断是否只有一个节点了
while (head != head->next)
{
//指向head的下一个节点
head = head->next;
//保存要删除节点的指针
PLIST temp =