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

合并链表c语言

发布时间: 2023-01-21 05:18:04

1. c语言链表合并函数的问题

创建链表的函数有些问题吧

student *p;
student *q=I;
for (j=0;j<i;j++)
{
p=(student*)malloc(sizeof(student));
p->next=Null;
q->next=p;
q=p;
}

2. 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后面即可。

3. 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;}}

4. 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***********/

5. c语言如何实现两链表合并

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

6. c语言编程,合并两个循环链表

首先要以head1为开始点,找到链表一中的最后一个节点;
然后以最后一个节点为开始点,指向head2;
最后将链表二中的最后一个节点指向链表一的开始节点;
这样就将两个循环链表合并成一个循环链表了。

7. 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;
}

8. 有序链表的合并,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;

  • }

  • }

9. 用C语言编一个程序:两个递增有序链表合并成一个递减链表,

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

typedef struct node
{
int data;
struct node *next;
}node,*list;
void init(list &head)
{
head=(list)malloc(sizeof(node));
head->next=NULL;
}

void input(list &h)
{
list p,q;
q=h;
printf("输入数据的个数 n : ");
int n;
scanf("%d",&n);
printf("请输入 %d 个有序递增数据:\n",n);
for (int i=0;i<n;i++)
{
// printf("第 %d 个: ",i+1);
p=(list)malloc(sizeof(node));
scanf("%d",&p->data);
p->next=q->next;
q->next=p;
q=p;
}
}

void output(list h)
{
list p;
p=h->next;
printf("输出数据\n");
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}

void combine(list &a,list &b,list &c)
{
list p,q,t;
p=a->next;
q=b->next;
free(b);
b=q;
c=a;
a=p;
c->next=NULL;
while(p&&q)
{
if (p->data<=q->data)
{
a=a->next;
p->next=c->next;
c->next=p;
p=a;
}
else
{
b=q->next;
q->next=c->next;
c->next=q;
q=b;
}
}
if (p!=NULL)
{
while(p)
{
a=a->next;
p->next=c->next;
c->next=p;
p=a;
}
}
if (q!=NULL)
{
while(q)
{
b=q->next;
q->next=c->next;
c->next=q;
q=b;
}
}

}
void main()
{
list a,b,c;
init(a);init(b);
printf("\n输入链表A :\n");
input(a);
printf("\n输入链表B :\n");
input(b);

printf("输出合并后的链表:\n");
combine(a,b,c);
output(c);
}
结果:
输入链表A :
输入数据的个数 n : 3
请输入 3 个有序递增数据:
4 5 9

输入链表B :
输入数据的个数 n : 4
请输入 4 个有序递增数据:
2 3 7 10

输出合并后的链表:
输出数据
10 9 7 5 4 3 2
Press any key to continue

10. 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;
}

热点内容
云服务器搭建需要什么工具 发布:2025-07-18 20:51:08 浏览:321
如何提高手机缓存速度 发布:2025-07-18 20:24:48 浏览:236
vba读取数据库数据 发布:2025-07-18 20:24:48 浏览:607
shell解压zip 发布:2025-07-18 20:20:36 浏览:858
安卓泰拉瑞亚去哪里买 发布:2025-07-18 20:01:05 浏览:694
flash编译器 发布:2025-07-18 19:49:38 浏览:487
memcached源码分析 发布:2025-07-18 19:22:42 浏览:866
android展示图片 发布:2025-07-18 19:21:24 浏览:594
一台服务器5个IP怎么分配 发布:2025-07-18 19:12:34 浏览:862
贵阳分布式存储行情 发布:2025-07-18 19:12:31 浏览:361