在一个链表中插入另一个链表中

设指针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;
}

做了好长时间。。。。。其实不是很难,但是中间一段为了检查我把Lb链表遍历了一遍输出查看结果,到后面忘记删除了,然后就蝴蝶效应了,半天输出不出来结果。唉,感觉自己还是个弟弟,加油,争取成为弟弟王!

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值