两个单链表合并c语言
㈠ c语言将两个链表合并为一个
形参不应该是指针类型吗?
取地址是个啥?版
void Merge(TxILink *T,TxILink *L)
{
struct TxILink* p = T;
while(p->next)
{ p = p->next ; }
p->next = L;
}
这样就可以权了。
㈡ C语言实现的合并两个单链表的程序,高手帮忙看下错在哪儿了呢
/*错的地方还不少,主要有以下几点
1.使用malloc函数没有包含其头文件。
2.s_union最后缺个“}”。
3. printf_s函数里面printf("%c",p->ch;p=p->next;);应改为printf("%c",p->ch);p=p->next;
4.某些地方s对象的成员x写成了其他字符串。
以上是语法错误,还存在编译不报错的逻辑错误,如下
5.s_union合并算法错误。
6. s_create采用链表前插法,这样得到的字符序列与输入相反。
此外还有一些其他编写不太好的地方,一并修正,得到如下结果
*/
#include<stdio.h>
#include<malloc.h>
typedef struct node
{char x;node* next;}s;
s* s_create(int* length)
{
s *p,*head,*tail;
char ch;
head=tail=(s*)malloc(sizeof(s));
head->next=NULL;
scanf("%c",&ch);
(*length)++;
while(ch!='\n')
{
p=(s*)malloc(sizeof(s));
p->x=ch;
p->next=NULL;
tail=tail->next=p;
(*length)++;
scanf("%c",&ch);
}
return head;
}
void s_union(s* head1,s* head2)
{
s *p=head1->next,*q=head2->next,*temp1,*temp2;
while(q!=NULL)
{
temp1=p->next;
temp2=q->next;
p->next=q;
p=q->next=temp1;
q=temp2;
}
}
void printf_s(s* head)
{
s *p=head->next;
while(p){printf("%c",p->x);p=p->next;}
printf("\n");
}
int main()
{
s *head1,*head2;
int length1=0,length2=0;
head1=s_create(&length1);
head2=s_create(&length2);
printf_s(head1);
printf_s(head2);
if(length1>length2)s_union(head1,head2);
else s_union(head2,head1);
printf_s(head1);
return 0;
}
㈢ 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;
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;
}
p=L->next;//得到首原节点
p1=L1->next;//得到首原节点
L2=(structlist*)malloc(sizeof(structlist));
L2=L;//指向已有链表空间
L2->next=NULL;
q=(structlist*)malloc(sizeof(structlist));
//循环里主要是头插法原理
while(p||p1){
if(!p){
q=p1;
p1=p1->next;
}
elseif(!p1){
q=p;
p=p->next;
}elseif(p->data<=p1->data){
q=p;
p=p->next;
}else{
q=p1;
p1=p1->next;
}
q->next=L2->next;
L2->next=q;
}
free(L1);
p=L2->next;
while(p){
printf("%d",p->data);
p=p->next;
}
}
- #include<stdio.h>
#include<malloc.h>
typedef struct list {
int data;
struct list * next; //下一个节点地址
}list;
//第一条链表
struct list * L=NULL;//头
struct list * head=NULL;//首
struct list * p=NULL;
//第二条链表
struct list * L1=NULL;//头
struct list * head1=NULL;//首
struct list * p1=NULL;
//代理链表
struct list * L2=NULL;//头
struct list * q=NULL;
int main(){
int i=0,length;
printf("请输入链表的长度 ");
scanf("%d",&length);
head=(struct list *)malloc(sizeof(struct list));
L=head;
printf("请依次输入链表的内容 ");
for(i;i<length;i++){
p = (struct list *)malloc(sizeof(struct list));
scanf("%d",&p->data);
p->next=NULL;
head->next=p;
head=p;
}
int i1=0,length1;
printf("请输入链表的长度 ");
scanf("%d",&length1);
head1=(struct list *)malloc(sizeof(struct list));
L1=head1;
printf("请依次输入链表的内容 ");
for(i1;i1<length1;i1++){
p1= (struct list *)malloc(sizeof(struct list));
scanf("%d",&p1->data);
p1->next=NULL;
head1->next=p1;
head1=p1;
}
p=L->next;//得到首原节点
p1=L1->next;//得到首原节点
L2=(struct list *)malloc(sizeof(struct list));
L2=L;//指向已有链表空间
L2->next=NULL;
q=(struct list *)malloc(sizeof(struct list));
//循环里主要是 头插法原理
while(p||p1){
if(!p){
q=p1;
p1=p1->next;
}
else if(!p1){
q=p;
p=p->next;
}else if(p->data<=p1->data){
q=p;
p=p->next;
}else{
q=p1;
p1=p1->next;
}
q->next = L2->next;
L2->next=q;
}
free(L1);
p=L2->next;
while(p){
printf("%d ",p->data);
p=p->next;
}
}
㈣ C语言程序题:两个有序单链表的合并 合并之后仍然有序。 如第一个链表13579 第二个链表
ListNode*ReNewCombineList(ListNode*p1,ListNode*p2)//合并两个链表,,生成第三个链表递归
{
ListNode*pNewList=NULL;
//ListNode*p3=NULL;
if(p1==NULL)
return p2;
if(p2==NULL)
return p1;
if(p1->data<p2->data)
{
pNewList=p1;
pNewList->next=ReNewCombineList(p1->next,p2);
}
else
{
pNewList=p2;
pNewList->next=ReNewCombineList(p1,p2->next);
}
return pNewList;
}
(4)两个单链表合并c语言扩展阅读:
return
C++的关键字,它提供了终止函数执行的一种方式。当return语句提供了一个值时,这个值就成为函数的返回值.
说到return,有必要提及主函数的定义,下面是从网络上找到的资料,好好消化吧,对了解主函数中返回值的理解有很大的帮助.
很多人甚至市面上的一些书籍,都使用了void main(),其实这是错误的。C/C++中从来没有定义过void main()。
C++之父Bjarne Stroustrup在他的主页上的FAQ中明确地写着The definition void main(){/*...*/}is not and never has been C++,
nor has it even been C.(void main()从来就不存在于C++或者C)。下面我分别说一下C和C++标准中对main函数的定义。
1.C
在C89中,main()是可以接受的。Brian W.Kernighan和Dennis M.Ritchie的经典巨著The C programming Language 2e(《C程序设计语言第二版》)用的就是main()。不过在最新的C99标准中,只有以下两种定义方式是正确的:
int main(void)
int main(int argc,char*argv[])
(参考资料:ISO/IEC 9899:1999(E)Programming languages—C 5.1.2.2.1 Program startup)
当然,我们也可以做一点小小的改动。例如:char*argv[]可以写成char**argv;argv和argc可以改成别的变量名(如intval和charval),不过一定要符合变量的命名规则。
如果不需要从命令行中获取参数,请用int main(void);否则请用int main(int argc,char*argv[])。
main函数的返回值类型必须是int,这样返回值才能传递给程序的激活者(如操作系统)。
如果main函数的最后没有写return语句的话,C99规定编译器要自动在生成的目标文件中(如exe文件)加入return 0;,表示程序正常退出。不过,我还是建议你最好在main函数的最后加上return语句,虽然没有这个必要,但这是一个好的习惯。
注意,vc6不会在目标文件中加入return 0;,大概是因为vc6是98年的产品,所以才不支持这个特性。现在明白我为什么建议你最好加上return语句了吧!不过,gcc3.2(Linux下的C编译器)会在生成的目标文件中加入return 0;。
㈤ C语言 将两个非递减的单链表合成一个
#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedefstructLNode
{intdata;
structLNode*next;
}LNode,*LinkList;
voidCreatlist(LinkList*L)
{LinkListp,q;
intnum;
*L=(LinkList)malloc(sizeof(LNode));
q=*L;
printf("输入若干整数,输入-1表示结束
");
while(scanf("%d",&num),num!=-1)
{p=(LinkList)malloc(sizeof(LNode));
p->data=num;
q->next=p;
q=q->next;
}
p->next=NULL;
}
voidPrintList(LinkListL)
{LinkListp;
p=L->next;
while(p)
{printf("%d",p->data);
p=p->next;
}
printf("
");
}
voidMergeList(LinkListla,LinkListlb,LinkList*lc)
{LinkListpa,pb,pc;
pa=la->next;
pb=lb->next;
*lc=pc=la;
while(pa&&pb)
{if(pa->data<=pb->data)
{pc->next=pa;
pc=pa;
pa=pa->next;
}
else
{pc->next=pb;
pc=pb;
pb=pb->next;
}
}
pc->next=pa?pa:pb;
free(lb);
}
intmain(void)
{LinkListLA,LB,LC;
Creatlist(&LA);
printf("LA:");PrintList(LA);
Creatlist(&LB);
printf("LB:");PrintList(LB);
MergeList(LA,LB,&LC);
printf("LC:");PrintList(LC);
return0;
}
#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedefstructLNode
{
intdata;
structLNode*next;
}LNode,*LinkList;
voidCreatlist(LinkList*L)
{
LinkListp,q;
intnum;
*L=(LinkList)malloc(sizeof(LNode));
q=*L;
printf("输入若干整数,输入-1表示结束
");
while(scanf("%d",&num),num!=-1)
{
p=(LinkList)malloc(sizeof(LNode));
p->data=num;
q->next=p;
q=q->next;
}
p->next=NULL;
}
voidPrintList(LinkListL)
{
LinkListp;
p=L->next;
while(p)
{
printf("%d",p->data);
p=p->next;
}
printf("
");
}
voidMergeList(LinkListla,LinkListlb,LinkList*lc)
{
LinkListpa,pb,pc,pt;
pa=la->next;
pb=lb->next;
*lc=pc=la;
pc->next=NULL;
free(lb);
while(pa&&pb)
{
if(pa->data<=pb->data)
{
pt=pa->next;
pa->next=pc->next;
pc->next=pa;
pa=pt;
}
else
{
pt=pb->next;
pb->next=pc->next;
pc->next=pb;
pb=pt;
}
}
if(!pa)pa=pb;
while(pa)
{
pt=pa->next;
pa->next=pc->next;
pc->next=pa;
pa=pt;
}
}
intmain(void)
{
LinkListLA,LB,LC;
Creatlist(&LA);
printf("LA:");
PrintList(LA);
Creatlist(&LB);
printf("LB:");
PrintList(LB);
MergeList(LA,LB,&LC);
printf("LC:");
PrintList(LC);
return0;
}
㈥ c语言,如何使两个链表合并,求解答
void test(LNodeL *head1,Node *head2)//将表头为head2的链表合并到head1
{
LNode *p1 = head1;
while(p1->next != NULL)
{
p1 = p1->next;
}
p->next = head2;
}
这样就可以。
㈦ c语言如何实现两链表合并
只要让第一个链表的尾部元素 的 next 指针,指向第二个链表的第一个元素就可以了
如果是双向链表,则除上面操作外,再让第二个链表的 prev指针指向第一个链表最后一个元素
㈧ C语言程序,两个单链表合并问题,跪求大神解答~~在线等
#include <stdio.h>#include <stdlib.h>typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
ElementType Data;
PtrToNode Next;
};
typedef PtrToNode List;List Read(); /* 链表读取 */void Print( List L ); /*链表输出;空链表将输出NULL */List Merge( List L1, List L2 ); /*链表合并*/int main()
{
List L1, L2, L;
L1 = Read(); //读取链表1 L2 = Read(); //读取链表2 L = Merge(L1, L2);
Print(L);
Print(L1);
Print(L2);
return 0;
}List Merge( List L1, List L2 )
{
List L, p1, p2, r ;
p1 = L1->Next;
p2 = L2->Next;
L = (List)malloc(sizeof(struct Node));
L->Next = NULL;
r = L;
while (p1 && p2){
if(p1->Data < p2->Data){
r->Next = p1;
r = p1;
p1 = p1->Next;
}
else{
r->Next = p2;
r = p2;
p2 = p2->Next;
}
}
if(p1)
r->Next = p1;
else r->Next = p2;
L1->Next = NULL;
L2->Next = NULL;
return L;
}List Read()
{
int n, i;
List L, p, s;
scanf("%d",&n);
L = (List)malloc(sizeof(struct Node));
L->Next = NULL;
p = L;
for(i = 0;i < n; ++i){
s = (List)malloc(sizeof(struct Node));
scanf("%d",&s->Data);
s->Next = p->Next;
p->Next = s;
p = s;
}
return L;
}void Print(List L)
{
List p;
p = L->Next;
if (L->Next==NULL)
{
printf("NULL");
}
while(p)
{
printf("%d ", p->Data);
p = p->Next;
}
printf("\n");
}
㈨ C语言链表合并:将两个有序单向链表合并为一个单向有序链表,要求分别用两种方式实现~急~求大神帮忙
小意思!有个前提,两个链表的数据类型都是一样的哦
第一种:先新建一专个链表,然后属遍历第一链表,同时把它的值都赋给新建的链表,然后,开始第二个链表,也是同样的办法。加上第二个的时候,先找到新建链表的表尾,再表尾处开始添加第二个
第二种:首先遍历第一个链表,找到表尾,然后去掉第二个链表的表头,把第二个链表的头部赋给第一个链表的尾部 //当然,如果没有表头什么的就直接把第一个节点赋给第一个就行了。
第二种方法之后,两个链表就合成一个了。
㈩ 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;}}