疫苗接种(链表练习)

疫苗接种(链表练习)_第1张图片疫苗接种(链表练习)_第2张图片疫苗接种(链表练习)_第3张图片

很明显,数组也可以做,但是我想练习链表

这道题我上交的时候,同一份代码,三个编译器,三个成绩,有点搞心态

代码如下:

#include
#include
#include
typedef int ElemType;
typedef struct LNode{
	ElemType ID;
    ElemType date;
	struct LNode * next;
}LinkList;

LinkList * creat(int n)
{
	LinkList * head, * node, * end;//定义头节点,普通节点,尾部节点
	head = (LinkList *)malloc(sizeof(LinkList));//分配地址
	end = head;
	for(int i = 0; i < n; i++)
	{
		node = (LinkList *)malloc(sizeof(LinkList));//分配地址
		scanf("%d%d", &node->ID, &node->date);
		end->next = node;
		end = node;
	}
	end->next = NULL;//结束创建
	return head;
}//创建长度为n的链表
void DestroyList(LinkList * head)
{
	LinkList * end = head;
	do
	{
		end = end->next;
		free(head);
		head = end;
	}while(end != NULL);
	return;
}//销毁链表

int main(void)
{
    int n, T = 0;
    scanf("%d", &n);
    LinkList * head = creat(1);
    for(int i = 1; i < n; i++)
    {
        int tID, tdate;
        scanf("%d%d", &tID, &tdate);
        for(LinkList * node = head->next, * f_node = head; 1; node = node->next, f_node = f_node->next)
            if(node == NULL || tdate < node->date || (tdate == node->date && tID < node->ID))
            {
                LinkList * tmp = (LinkList *)malloc(sizeof(LinkList));
                tmp->ID = tID, tmp->date = tdate;
                f_node->next = tmp, tmp->next = node;
                break;
            }
    }
    if(n % 10) n = (int)(n * 0.3) + 1;
    else  n = n / 10 * 3;
    LinkList * node = head;
    for(int i = 0; i < n; i++)
        node = node->next;
    T = node->date;
    printf("%d\n", T + 180);
    for(node = head->next; node->date <= T; node = node->next)
        printf("%d %d\n", node->ID, node->date);
    DestroyList(head);

    return 0;
}

你可能感兴趣的:(链表,数据结构)