题目:有一个数组a[N]如a[10]={0,1,2,3,4,5,6,7,8,9}每隔两个数删除一个数,如0,1,2(删除),3,4,5(删除),6,7,8(删除),9,
到数组尾部回到数组头部继续删除,要求编写一个函数实现实现上述操作,返回最后一个数的数组下标。
函数接口:int getLast(int iLen)
参数:数组初始元素个数iLen
#include <stdio.h> #include <stdlib.h> typedef struct node *List; typedef struct node *PNode; typedef struct node { int data; struct node *next; }Node; int getLast(int iLen) { int i; List L; PNode tempNode,current; L = (List)malloc(sizeof(Node)); L->next = NULL; current = L; for (i=0; i<iLen; i++) { tempNode = (PNode)malloc(sizeof(Node)); tempNode->data = i; current->next = tempNode; current = tempNode; } current->next = L->next; current = L; while (iLen > 1) { current = current->next->next; tempNode = current->next; current->next = tempNode->next; printf("%d\n",tempNode->data); free(tempNode); iLen--; } return current->data; } int main() { printf("last of 20 is %d",getLast(20)); return 0; }