设指针la和lb分别指向两个无头结点单链表中的首元结点,试设计从表la中删除自第i个元素起共len个元素,并将它们插入到表lb的第j个元素之后的算法。
#include "stdio.h"
#include "stdlib.h"
struct student
{
int data;
struct student *next;
};
struct student *CreateLista();
struct student *CreateListb();
struct student *tiaozheng(struct student *La,int i,int len);
struct student *chazhao(struct student *Lb,int j);
struct student *chazhao1(struct student *L1);
int main()
{
int i,j,len;
struct student *La,*Lb,*L1,*p,*q;
La=CreateLista();
Lb=CreateListb();
scanf("%d %d %d",&i,&len,&j);
L1=tiaozheng(La,i,len);
printf("\n");
p=chazhao(Lb,j);
printf("%d\n",p->data);
q=chazhao1(L1);
printf("%d\n",q->data);
q->next=p->next;
p->next=L1->next;
while(Lb->next!=NULL)
{
printf("%5d",Lb->next->data);
Lb=Lb->next;
}
}
struct student *CreateLista()
{
int x;
struct student *a=NULL;
struct student *pNew,*pEnd;
a=(struct student *)malloc(sizeof(struct student));
pEnd=a;
scanf("%d",&x);
while(x!=-1)
{
pNew=(struct student*)malloc(sizeof(struct student));
pNew->data=x;
pEnd->next=pNew;
pEnd=pNew;
scanf("%d",&x);
}
pEnd->next=NULL;
return a;
}
struct student *CreateListb()
{
int x;
struct student *b=NULL;
struct student *pNew,*pEnd;
b=(struct student *)malloc(sizeof(struct student));
pEnd=b;
scanf("%d",&x);
while(x!=-1)
{
pNew=(struct student*)malloc(sizeof(struct student));
pNew->data=x;
pEnd->next=pNew;
pEnd=pNew;
scanf("%d",&x);
}
pEnd->next=NULL;
return b;
}
struct student *tiaozheng(struct student *La,int i,int len)
{
struct student *pHead,*pNew,*pEnd,*pFan;
pHead=La;
pEnd=(struct student*)malloc(sizeof(struct student));
while(i)
{
pHead=pHead->next;
i--;
}
pFan=pEnd;
while(len)
{
pNew=(struct student*)malloc(sizeof(struct student));
pNew->data=pHead->data;
pEnd->next=pNew;
pEnd=pNew;
pHead=pHead->next;
len--;
}
pEnd->next=NULL;
return pFan;
}
struct student *chazhao(struct student *Lb,int j)
{
struct student *p;
p=Lb;
while(j)
{
p=p->next;
j--;
}
return p;
}
struct student *chazhao1(struct student *L1)
{
struct student *p;
p=L1;
while(p->next!=NULL)
{
p=p->next;
}
return p;
}