单链表连续删除

#include
using namespace std;
typedef struct Node 
{
	int val;
	Node *next;
}LST;
LST *mycreat(int n)
{
	LST *p=NULL,*sec=NULL;
	LST *head=new LST;
	p=head;
	while(n--)
	{
		int v;
		scanf("%d",&v);
		LST *sec=new LST;
		sec->val=v;
		p->next=sec;
		p=sec;
	}
	p->next=NULL;
	return head;
}
void myprint(LST *head)
{
	int f=0;
	LST *p;
	p=head->next;
	while(p!=NULL)
	{
		if (f==0)
			printf("%d",p->val);
		else
			printf(" %d",p->val);
		f++;
		p=p->next;
	}
	printf("\n");
	return ;
}
LST *mydel(LST *head,int i,int l)
{
	LST *p=NULL,*q=NULL;
	p=head;
	int k=0;
	for (k=0;knext;
	}
	if(p->next)
		q=p->next;
	else
		return head;
	l--;
	while(l--)
	{
		q=q->next;
	}
	if (q->next==NULL)
		p->next=NULL;
	else
		p->next=q->next;
	delete q;
	return head;
}
void del(LST *p)
{
	LST *q;
	while(p!=NULL)
	{
		q=p;
		p=p->next;
		delete q;
	}
}
int main()
{
	int n,i,l;
	while(~scanf("%d",&n))
	{
		LST *head;
		head=mycreat(n);
		scanf("%d%d",&i,&l);
		if(i+l-1>n||i==0||l<0||i>n) 
			printf("data error\n");
		else if(l==0)
			myprint(head);
		else
		{
			head=mydel(head,i,l);
			if(head->next)
				myprint(head);
		}
		del(head);
	}
	return 0;
}

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