2012武汉华为机试题

题目:有一个数组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;
}
posted on 2011-09-18 21:50 林中鸟 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/zechen11/archive/2011/09/18/2180700.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值