用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

数据结构与算法----1.9 两表合一,要求两个表的元素交错排列,利用原内存空间

2019-07-19 作者: Ryan2019举报

[c++]代码库

//***********************************
//功能:把两个单链表合并为一个单链表,要求两个表的元素交错排列,新表利用原表结点的内存空间
//日期:2017年9月27日
//作者:Ryan2019
//***********************************
#include <iostream>
using namespace std;
typedef int LElemType;
typedef struct LNode
{
    LElemType data;
	LNode *next;
}* LList;
void ListCreate(LList &L,int n,LElemType a[]);//创建链表
void Listshow(LList &L);//显示链表
bool ListMerge(LList &A,LList &B);//合并

int main()
{
	LList A,B; int m=3,n=5;
	LElemType a[3]={1,2,3};
	LElemType b[5]={4,5,6,7,8};
	ListCreate(A,m,a);	
	ListCreate(B,n,b);
	cout<<"原链表A为:";	Listshow(A);
	cout<<"原链表B为:";	Listshow(B);
	ListMerge(A,B);
	cout<<"合并后链表为:";	Listshow(A);
	return 0;
}

void ListCreate(LList &L,int n,LElemType a[])
{
	LList p;	int i;
	L=new LNode; L->next=NULL;
	for(i=n-1;i>=0;i--)
	{
		p=new LNode;
		p->data=a[i];
		p->next=L->next;
		L->next=p;
	}
}
void Listshow(LList &L)
{
	LList p;
	for(p=L->next;p;p=p->next)
	{
		cout<<p->data<<" ";
	}
	cout<<endl;
}
bool ListMerge(LList &A,LList &B)
{
	LList p,q,t;
	p = A->next;
	q = B->next;
	delete B;
	while (p&&q)
	{	
		t=p;
		p=p->next;
		t->next=q;
		t=q;
		q=q->next;
		t->next=p;
	}
	if(!p)
	{			
		t->next=q;
	}
	else if(!q)
	{
		t->next=p;
	}
	return true;

}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...