当前位置:首页 » 编程语言 » c语言链表合并

c语言链表合并

发布时间: 2022-05-13 03:05:20

‘壹’ c语言 把两个有序链表合并为一个有序链表(递增)

  • 设链表结点结构为Node(int data, Node *next),typedef Node List,链表均带表头结点。

  • 思路是:把list1中的元素看成是集合1,把list2中的元素看成是集合2,把list1头结点(即list1结点)从集合1中脱离下来看成是目标集合的头结点,目标集合开始时是空集,并用last指针始终指向该集合的尾部,然后每次从集合1和集合2中取各自的第一个元素进行比较,较小者从相应集合里脱离,插入到目标集合list1的尾部即last的末尾,并将刚插入的元素作为目标集合list1的新的last,直到集合1为空或集合2为空时结束,最后将未空的集合中的剩余元素链接到last后面即可。

‘贰’ c语言中链表合并怎么弄详解

链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。相比于线性表顺序结构,操作复杂。

使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。在计算机科学中,链表作为一种基础的数据结构可以用来生成其它类型的数据结构。链表通常由一连串节点组成,每个节点包含任意的实例数据(datafields)和一或两个用来指向上一个/或下一个节点的位置的链接("links")。链表最明显的好处就是,常规数组排列关联项目的方式可能不同于这些数据项目在记忆体或磁盘上顺序,数据的存取往往要在不同的排列顺序中转换。而链表是一种自我指示数据类型,因为它包含指向另一个相同类型的数据的指针(链接)。链表允许插入和移除表上任意位置上的节点,但是不允许随机存取。链表有很多种不同的类型:单向链表,双向链表以及循环链表。


以上是对链表的一个概述,说的其实很全面了。我们应用链表就是为了克服顺序表(数组)必须在内存中连续分配相邻地址的束缚,更好的应用内存空间(很多破碎不连贯空间)。

你可以把链表类比成货运火车,火车的每一节车皮就是链表的每一个结点(一般用link表示),每个结点实际上有两个部分,一个部分是装货的空间就是链表的数据存储部分(一般用link—>data表示),另一部分就是与下一节车厢的连接部分就是链表的指针部分(用link—>next表示,指向下一个结点)。那么我们平时怎样管理火车呢?记住火车的第一节车皮即可,顺着第一节就能找到找到所有的车皮。链表也是如此,有了头结点(一般用head表示)就能找到所有的结点。这里缺点就来了,比如一共100节车皮,我让你找49节车皮,那你就必须从第一节车皮开始找,否则你没办法确定哪个是第49节。链表也是如此,必须从头结点开始找起,这也就是为什么要记住头结点的原因之一。相比数组直接按照序号访问,链表的访问要麻烦很多。同样我们也需要记住尾结点,就好像我们在一列长火车的尾部插一面小红旗,那么列车工人就能方便找到车尾,把需要的车皮挂载到这列火车上;链表同样如此,我们用tail来标记尾结点,直接把需要增加的结点加载到链表上即可,否则没有尾结点,那我们就要从头开始找到尾,很麻烦啊。

链表合并其实很简单,只要是两个结点数据类型相同(不同也可以),把其中一个的结点的头结点连接到另一个的尾结点就可以了。就是让其中一个的尾结点的指针tail->next=head(另一个结点的头结点)当然这是无序链表。如果是有序链表,比如结点数据时按照从大到小排列的,那首先就需要找到插入位置,读取每一个结点的数据,然后比较。找到插入位置之后按照下图进行的方式即可:

‘叁’ 有序链表的合并,c语言

  • #include<stdio.h>

  • #include<malloc.h>

  • typedefstructlist{

  • intdata;

  • structlist*next;//下一个节点地址

  • }list;

  • //第一条链表

  • structlist*L=NULL;//头

  • structlist*head=NULL;//首

  • structlist*p=NULL;

  • //第二条链表

  • structlist*L1=NULL;//头

  • structlist*head1=NULL;//首

  • structlist*p1=NULL;

  • //代理链表

  • structlist*L2=NULL;//头

  • structlist*q=NULL;//L2备用地址

  • structlist*q1=NULL;//备用地址

  • intmain(){

  • inti=0,length;

  • printf("请输入链表的长度 ");

  • scanf("%d",&length);

  • head=(structlist*)malloc(sizeof(structlist));

  • L=head;

  • printf("请依次输入链表的内容 ");

  • for(i;i<length;i++){

  • p=(structlist*)malloc(sizeof(structlist));

  • scanf("%d",&p->data);

  • p->next=NULL;

  • head->next=p;

  • head=p;

  • }

  • inti1=0,length1;

  • printf("请输入链表的长度 ");

  • scanf("%d",&length1);

  • head1=(structlist*)malloc(sizeof(structlist));

  • L1=head1;

  • printf("请依次输入链表的内容 ");

  • for(i1;i1<length1;i1++){

  • p1=(structlist*)malloc(sizeof(structlist));

  • scanf("%d",&p1->data);

  • p1->next=NULL;

  • head1->next=p1;

  • head1=p1;

  • }

  • L2=(structlist*)malloc(sizeof(structlist));

  • q=L2;//备用合并链表起始地址

  • p=L->next;

  • p1=L1->next;

  • while(p&&p1){

  • if(p->data<p1->data){

  • L2->next=p;

  • L2=p;

  • p=p->next;

  • }elseif(p->data==p1->data){

  • L2->next=p;

  • L2=p;

  • p=p->next;

  • q1=p1->next;//备用相同元素的下一个地址指向

  • free(p1);

  • p1=q1;

  • }elseif(p->data>p1->data){

  • L2->next=p1;

  • L2=p1;

  • p1=p1->next;

  • }

  • }

  • L2->next=p?p:p1;

  • free(L1);

  • printf("合并后链表的内容 ");

  • p=q->next;

  • while(p){

  • printf("%d",p->data);

  • p=p->next;

  • }

  • }

‘肆’ C语言合并链表

#include<stdio.h>
#include<malloc.h>

structstudenta{
intnum;
floatscore;
structstudenta*next;
};

structstudentb{
intnum;
floatscore;
structstudentb*next;
};

structstudenta*add(structstudenta*heada,structstudenta*p0)
{
structstudenta*p1,*p2;
p1=heada;
while(/*p1->next!=NULL*/p1!=NULL&&p1->num<p0->num){
p2=p1;
p1=p1->next;
}
if(p1==heada){
heada=p0;
p0->next=p1;
}else{
if(p2->next!=NULL){
//p2->next=p0;
//p0->next=p1;/*顺序不要搞错*/
p0->next=p1;
p2->next=p0;
}else{
p2->next=p0;
p0->next=NULL;
}
}
returnheada;
}

intmain()
{
structstudentaboya[3],*heada,*p,*p0,*pa;
structstudentbboyb[3],*headb,*pb;
structstudenta*add(structstudenta*,structstudenta*);
heada=boya;
headb=boyb;
boya[0].num=101;
boya[0].score=100.0;
boya[1].num=103;
boya[1].score=98.0;
boya[2].num=105;
boya[2].score=55.0;
boya[0].next=&boya[1];
boya[1].next=&boya[2];
boya[2].next=NULL;

boyb[0].num=102;
boyb[0].score=35.0;
boyb[1].num=104;
boyb[1].score=77.0;
boyb[2].num=106;
boyb[2].score=59.0;
boyb[0].next=&boyb[1];
boyb[1].next=&boyb[2];
boyb[2].next=NULL;

printf("A链表: ");
pa=heada;
do{
printf("num=%d score=%5.1f ",pa->num,pa->score);
pa=pa->next;
}while(pa);//用heada遍历,循环结束heada已经不再指向头结点

printf("B链表: ");
pb=headb;
do{
printf("num=%d score=%5.1f ",pb->num,pb->score);
pb=pb->next;
}while(pb);//headb遍历,循环结束headb已经不再指向头结点

printf("合并链表: ");
do{
p0=(structstudenta*)malloc(sizeof(structstudenta));
p0->num=headb->num;
p0->score=headb->score;
p=add(heada,p0);
headb=headb->next;
}while(headb);

do{
printf("num=%d score=%5.1f ",p->num,p->score);
p=p->next;
}while(p);

return0;
}

‘伍’ 有关c语言两个顺序链表的合并

typedefintElementType;
typedefstructNode*PtrToNode;//定义PtrToNode为指向Node的结构体指针
structNode{
ElementTypeData;
PtrToNodeNext;
};
typedefPtrToNodeList;//定义list为指向node结构体的指针

ListMerge(ListL1,ListL2){
Listpa=L1,pb=L2,head;
Listhead=pc=(structNode*)malloc(sizzeof(structNode));
pc=(pa->Data<=pb->Data)?pa:pb;//pc指向Data较小的节点
while(pa&&pb){//循环比较L1与L2两条链表的Data
if(pa->Data<pb->Data){
pc->Next=pa;
pc=pc->Next;
pa=pa->Next;
}
else{
pc->Next=pb;
pc=pc->Next;
pb=pb->Next;
}
}
while(pa){pc->Next=pa;pc=pc->Next;pa=pa->Next;}//将剩余的链表接到pc后面
while(pb){pc->Next=pb;pc=pc->Next;pb=pb->Next;}
printf("ListMergeisavailable!");
returnhead;
}

‘陆’ C语言 链表合并并排序

/**********start**********//以下是伪代码,有些变量需要定义
if(heada->num
<=
headb->num)//判断第一个节点是在a还是b上,小的作为新链表的头结点,然后设置指示指针pa,pb,p分别在a链表,b链表,新链表上向后移动
{head=heada;
pa=heada->next;
pb=headb;}
else
{head=headb;
pa=heada;
pb=headb->next;}
p=head;
while(pa
&&
pb)//当a,b链表都没到达尾部时,比较a,b链表上当前指示指针pa,pb所指节点的大小,并把新链表的p指向较小的(pa或pb所指的),然后向后移动一下
{
if
(pa->num<=pb->num)
{p->next=pa;p=p->next;pa=pa->next}
else
{p->next=pb;p=p->next;pb=pb->next}
}
if(pa)//说明a链表没到尾部,b链表已结束,直接把a链表剩下的部分接在新链表最后
{
p->next=pa;
tail=taila;
}
if(pb)//同上b链表后面的部分接在新链表后
{
p->next=pb;
tail=tailb;
}
tail->next=NULL;
/***********end***********/

‘柒’ C语言单链表合并

#include<stdio.h>#include<stdlib.h>structlist{intdata;structlist*next;};//两个链表融合,插入排序函数voidsort(structlist*l1,structlist*l2);//输出链表voidoutput(structlist*head);//输入链表voidinput(structlist*head,intnum);intmain(){intn;list*h1,*h2;//两个链表的头,下面四行初始化链表h1=(structlist*)malloc(sizeof(structlist));h2=(structlist*)malloc(sizeof(structlist));h1->next=NULL;h2->next=NULL;//两个链表输入printf("请输入第一个链表节点数: ");scanf("%d",&n);input(h1,n);printf("请输入第二个链表节点数: ");scanf("%d",&n);input(h2,n);//合并链表并排序sort(h1,h2);//输出合并后的链表output(h1);}voidinput(structlist*head,intnum){structlist*tmp;structlist*end;end=head;printf("请输入链表节点: ");for(inti=0;i!=num;i++){tmp=(structlist*)malloc(sizeof(structlist));scanf("%d",&tmp->data);end->next=tmp;tmp->next=NULL;end=tmp;}}voidsort(structlist*l1,structlist*l2){structlist*p1,*p2,*tmp;p1=l1;p2=l2->next;while(p1->next&&p2){if(p1->next->data>p2->data){tmp=p2->next;p2->next=p1->next;p1->next=p2;p2=tmp;}elsep1=p1->next;}if(p2)p1->next=p2;}voidoutput(structlist*head){while(head->next){printf("%d",head->next->data);head=head->next;}}

‘捌’ c语言,如何使两个链表合并,求解答

void test(LNodeL *head1,Node *head2)//将表头为head2的链表合并到head1
{
LNode *p1 = head1;
while(p1->next != NULL)
{
p1 = p1->next;
}
p->next = head2;
}
这样就可以。

‘玖’ C语言链表的合并代码

#include<stdio.h>
#include<stdlib.h>

typedefstructstudent
{
int num;
floatscore;
structstudent*next;
}STUD;

STUD*creat() //建立链表函数
{
STUD*h,*p,*q;
inti;

h=NULL;
printf("Pleaseinputthedata: ");
for(i=1;i<=3;i++)
{
p=(STUD*)malloc(sizeof(STUD));//申请空间
scanf("%d,%f",&p->num,&p->score);//此处用逗号分隔

if(h==NULL) //将链表的节点连接起来
h=p;
else
q->next=p;

q=p;
}
q->next=NULL;

return(h);
}

voidprint(STUD*h) //输出链表函数
{
printf(" ");
while(h!=NULL)
{
printf("%-5d%-5f ",h->num,h->score);
h=h->next;
}
}

STUD*connect(STUD*h1,STUD*h2) //连接链表
{ //以下代码大修改
STUD*head;

head=h1;
while(h1->next!=NULL) //先让第一个链表循环到最都一个节点
{
h1=h1->next;
}

h1->next=h2; //将最后一个节点连接第二个链表

returnhead; //返回连接好的链表
}

intmain(void) //voidmain()你自己上网搜搜是对的还是错的!
{
STUD*head1,*head2;

head1=creat(); //创建第一个链表

print(head1); //输出第一个链表

head2=creat(); //创建第二个链表

print(head2); //输出第二个链表

head1=connect(head1,head2); //连接链表

print(head1); //输出连接后的链表

return0;
}

‘拾’ c语言如何实现两链表合并

只要让第一个链表的尾部元素 的 next 指针,指向第二个链表的第一个元素就可以了
如果是双向链表,则除上面操作外,再让第二个链表的 prev指针指向第一个链表最后一个元素

热点内容
斗地主源码开发 发布:2025-05-11 02:24:07 浏览:364
云服务器怎么设置攻击 发布:2025-05-11 02:22:09 浏览:824
python嵌套for循环 发布:2025-05-11 01:51:44 浏览:227
安卓怎么取消后台限制 发布:2025-05-11 01:45:45 浏览:257
一键搭建sk5服务器 发布:2025-05-11 01:40:09 浏览:513
鸿业acs加密锁模拟器 发布:2025-05-11 01:38:49 浏览:937
神庙逃亡2安卓版怎么玩 发布:2025-05-11 01:38:05 浏览:161
凯杰都什么配置 发布:2025-05-11 01:38:04 浏览:471
php微信开源系统源码 发布:2025-05-11 01:37:54 浏览:813
pythonfor多个参数 发布:2025-05-11 01:12:32 浏览:74