hdu 4217 链表 超内存

#include <stdio.h>
#include <malloc.h>

int n,m,c;

struct node
{
	int data;
	struct node * next;
};

void InitList(node * H)
{
	H=(node *)malloc(sizeof(node));
	H->next=NULL;
}

void CreateFromHead(node * H)
{
	node *s;
	node *r;
	r=H;
	for(int i=1;i<=n;i++)
	{
		s=(node *)malloc(sizeof(node));
		s->data=i;
		r->next=s;
		r=s;
	}
}

int Get(node * L,int i)
{
	int j;

	node *p;
	node *q;
	p=L;j=0;
	while(p->next!=NULL&&j<i)
	{
		q=p;
		p=p->next;
		j++;
	}
	int k=p->data;
	q->next=p->next;
	if(i==j)return k;
	else return 0;
}

int main()
{
	int t,sum,i;

	node * nh;

	scanf("%d",&t);

	while(t--)
	{
		sum=0;
		scanf("%d%d",&n,&m);
		//InitList(nh);
		nh=(node *)malloc(sizeof(node));
	    nh->next=NULL;
		CreateFromHead(nh);

		/*node *q;
		q=nh;
		for(i=0;i<n;i++)
		{
			q=q->next;
			//printf("%d\n",q->data);
		}*/

		for(i=0;i<m;i++)
		{
			scanf("%d",&c);
			sum+=Get(nh,c);
		//	printf("%d\n",sum);
		}

		printf("%d\n",sum);
	}

	return 0;
}

  

原文地址:https://www.cnblogs.com/jackes/p/2474148.html