/**
分为三种情况:
1.链表长度<i:不进行任何操作
2.i+len>链表长度>i:在i处将链表断开
3.链表长度>i,链表长度>i+len:删除后重新连接链表
i的位置分为两种情况:
i=1,从首结点开始删除:需考虑l指针修改问题
i不等于1,从中间节点删除:无需考虑
**/
#include <stdlib.h>
#include <stdio.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LNode;
//查找指定位置指针
LNode* FindPos(LNode *l,int x)
{
LNode *p;
p=l;
int n=1;
while(p->next!=NULL)
{
if(n==x)return p;
p=p->next;
++n;
}
return NULL;
}
//删除操作
LNode* DelFun(LNode *l,int i,int len)
{
LNode *p,*n,*m;
p=l;
int count=0;
//统计链表长度
while(p!=NULL)
{
++count;
p=p->next;
}
//判断链表是否为空
if(count>0)
{
if(i>1)//不从首结点开始删
{
if(count>i&&count<i+len)
{
n=FindPos(l,i-1);
n->next=NULL;
}
if(count>i&&count>i+len)
{
n=FindPos(l,i-1);
m=FindPos(l,i+len);
n->next=m;
}
}else{
//从头结点开始删
if(count<=len) l=NULL;
if(count>len)
{
n=FindPos(l,i+len);
l=n;
}
}
}
return l;
}
/**用于测试
//创建链表
LNode* create()
{
LNode *c,*s,*r;
c=(LNode*)malloc(sizeof(LNode));
c->next=NULL;
c->data=1;
r=c;
int i;
for(i=2;i<=20;++i)
{
s=(LNode*)malloc(sizeof(LNode));
s->next=NULL;
s->data=i;
r->next=s;
r=r->next;
}
return c;
}
//打印链表
void printL(LNode *l)
{
LNode *p;
p=l;
while(p!=NULL)
{
printf("%d-",p->data);
p=p->next;
}
printf("\n");
}
int main()
{
LNode *l;
l=create();
printL(l);
l=DelFun(l,1,2);
printL(l);
}
**/