华为机试时第三题由于自己构造链表时间过长,未能按时完成,回去之后弄了出来,如有更为简洁的方法,欢迎大家指教

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define MAX 100


struct Timer{
	int ID;
	int time;
	int status;
};


struct node{
	struct Timer timer;
	struct node * next;
};


struct List{
	struct node * head;
	struct node * rear;
};


void Init(struct List * plist)
{
	plist->head = plist->rear = NULL;
}


struct node * searchtimer(struct List * plist,int id)
{
	if(plist->head == NULL)
		return NULL;
	struct node * sech = plist->head;
	while(sech->timer.ID != id && sech->next != NULL)
		sech = sech->next;
	if(sech->timer.ID == id)
		return sech;
	if(sech->next == NULL)
		return NULL;
}



void starttimer(struct List * plist,int ID,int time)
{
	struct node * cur;
	if(plist->head == NULL)	//条件成立表示计时器链表中无任何计时器,新建计时器节点
	{
		cur = (struct node *)malloc(sizeof(struct node));
		cur->next = NULL;
		cur->timer.ID = ID;
		cur->timer.status = 1;
		cur->timer.time = time;
		plist->head = plist->rear = cur;
		return;
	}
	else				//计时器链表中存在计时器,需要判断start的计时器是否是被停止的计时器,是的话无需添加,否则新建计时器
	{
		struct node * pcur;
		if((pcur = searchtimer(plist,ID)) == NULL)	//条件成立表示没找到既有节点,故要新建计时器节点
		{
			cur = (struct node *)malloc(sizeof(struct node));
			cur->next = NULL;
			cur->timer.ID = ID;
			cur->timer.status = 1;
			cur->timer.time = time;
			plist->rear->next = cur;
			plist->rear = cur;
			return;
		}
		else							//无需新建节点,将既有计时器的status项置1
		{
			pcur->timer.status = 1;
			return;
		}
	}
	
}



void stoptimer(struct List * plist,int id)
{
	struct node * pcur;
	if((pcur = searchtimer(plist,id)) == NULL)	//没找到符合ID要求的计时器,无效命令
		return;
	else
		pcur->timer.status = 0;					//时间启动有效位置0
}



void elapse(struct List * plist,int time)
{
	if(plist->head == NULL)
		return;
	struct node * cur = plist->head;
	while(cur->next != NULL)
	{
		if(cur->timer.status == 1)
		{
			cur->timer.time -= time;
			if(cur->timer.time < 0)
				cur->timer.time = 0;
		}
		cur = cur->next;
	}
	//处理最后一个计时器
	if(cur->timer.status == 1)
	{
		cur->timer.time -= time;
		if(cur->timer.time < 0)
			cur->timer.time = 0;
	}
}


void print(struct List * plist)
{
	if(plist->head == NULL)
		return;
	struct node * cur = plist->head;
	while(cur->next != NULL)
	{
		if(cur->timer.time > 0)
			printf("timer:%d,%d\n",cur->timer.ID,cur->timer.time);
		cur = cur->next;
	}
	//last node
	if(cur->timer.time > 0)
		printf("timer:%d,%d\n",cur->timer.ID,cur->timer.time);
}

int main()
{
	char command[MAX];
	char * str;
	char com1[MAX];
	int com2;
	int com3;
	struct List list;
	Init(&list);

	while(gets(command) != NULL)
	{
		str = command;
		if(strcmp(command,"end") == 0)	//end命令结束循环读取命令操作,输出结果
			break;
		if(strchr(command,',') != NULL)	//条件成立表示是starttimer命令 starttimer:1,1000
		{
			while(*str != ':')
				str++;
			*str = ' ';					//处理掉':',命令变成 starttimer 1,1000
			while(*str != ',')
				str++;
			*str = ' ';					//处理掉',',命令变成 starttimer 1 1000
			sscanf(command,"%s%d%d",com1,&com2,&com3);
			starttimer(&list,com2,com3);
		}
		else	//可能是stoptimer::1命令或elapse:1000
		{
			while(*str != ':')
				str++;
			if(*str == ':' && *(str+1) == ':')//条件成立则表示有双冒号,为stoptimer::1命令 
			{
				*str = *(str + 1) = ' ';	//stoptimer  1
				sscanf(command,"%s%d",com1,&com2);
				stoptimer(&list,com2);
			}
			else		//为elapse:1000命令
			{
				*str = ' ';		//elapse 1000
				sscanf(command,"%s%d",com1,&com2);
				elapse(&list,com2);
			}
		}
	}
	print(&list);
	return 0;
}
<img src="https://img-blog.csdn.net/20140911211044843?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWmJ0cmlr/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />


对于stoptimer命令可能不同的人有不同的理解,我的理解是被stop的计时器还是可以被重新start的,但新的starttimer命令所带的时间数据不能覆盖旧有的时间数据。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值