C语言 将两个非递减的单链表合成一个

作者&投稿:闽辉 (若有异议请与网页底部的电邮联系)
c语言合并两个有序单链表,使合并后的单链表非递增(或非递减)~

#include
#include
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->datadata){
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#include 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;idata);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;i1data);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->datadata){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 #include typedef int Elemtype;typedef struct node {Elemtype data;struct node *next;}NODE,*LinkList,*pNode;LinkList GetNewList() {pNode head = (pNode)malloc(sizeof(NODE));if(head == NULL) return NULL;head->data = 0;head->next = NULL;return head;}LinkList merge(LinkList LA,LinkList LB) {pNode a,b,c,head;a = LA;b = LB;c = head = GetNewList();head->data = LA->data + LB->data;while(a->next && b->next) {c->next = (pNode)malloc(sizeof(NODE));if(c->next == NULL) {printf("内存分配失败!
");return NULL;}if(a->next->data >= b->next->data) {c->next->data = a->next->data;a = a->next;}else {c->next->data = b->next->data;b = b->next;}c = c->next;}while(a->next) {c->next = (pNode)malloc(sizeof(NODE));if(c->next == NULL) {printf("内存分配失败!
");return NULL;}c->next->data = a->next->data;a = a->next;c = c->next;}while(b->next) {c->next = (pNode)malloc(sizeof(NODE));if(c->next == NULL) {printf("内存分配失败!
");return NULL;}c->next->data = b->next->data;b = b->next;c = c->next;}c->next = NULL;return head;}LinkList Creat(LinkList head,int a[],int n) {int i;pNode p = head;head->data = n;for(i = 0; i next = (pNode)malloc(sizeof(NODE));if(p->next == NULL) {printf("内存分配失败!
");return NULL;}p->next->data = a[i];p = p->next;}p->next = NULL;return head;}void Show(LinkList head) {pNode p = head->next;while(p) {printf("%d ",p->data);p = p->next;}printf("
");}int main( ) {Elemtypea[] = {98,89,86,80,76,69,68,54,50,29};int m = sizeof(a)/sizeof(a[0]);Elemtype b[] = {96,85,74,69,68,67,65,60,58};int n = sizeof(b)/sizeof(b[0]);LinkList LA = GetNewList();LA = Creat(LA,a,m);LinkList LB = GetNewList();LB = Creat(LB,b,n);LinkList LC;printf("LA: ");Show(LA);printf("LB: ");Show(LB);LC = merge(LA,LB);printf("LC: ");Show(LC);return 0;}

#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct LNode
{ int data;
 struct LNode *next;
}LNode,*LinkList;
void Creatlist(LinkList *L)
{ LinkList p,q;
 int num;
 *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;
 }
void PrintList(LinkList L)
{ LinkList p;
 p = L->next;
 while(p)
 { printf("%d ",p->data);
 p=p->next;
 }
 printf("
");
 }
void MergeList(LinkList la,LinkList lb,LinkList *lc)
{ LinkList pa,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);
}
int main(void)
{ LinkList LA,LB,LC;
 Creatlist(&LA);
 printf("LA: "); PrintList(LA);
 Creatlist(&LB);
 printf("LB: "); PrintList(LB);
 MergeList(LA,LB,&LC);
 printf("LC: "); PrintList(LC);
 return 0;
}

#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct LNode
{
  int data;
  struct LNode *next;
} LNode,*LinkList;
void Creatlist(LinkList *L)
{
  LinkList p,q;
  int num;
  *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;
}
void PrintList(LinkList L)
{
  LinkList p;
  p = L->next;
  while(p)
  {
    printf("%d ",p->data);
    p=p->next;
  }
  printf("
");
}
void MergeList(LinkList la,LinkList lb,LinkList *lc)
{
  LinkList pa,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;
  }
}
int main(void)
{
  LinkList LA,LB,LC;
  Creatlist(&LA);
  printf("LA: ");
  PrintList(LA);
  Creatlist(&LB);
  printf("LB: ");
  PrintList(LB);
  MergeList(LA,LB,&LC);
  printf("LC: ");
  PrintList(LC);
  return 0;
}



榆次区18337334642: C语言 将两个非递减的单链表合成一个 -
芮厕兰菌: #include<stdio.h> #include<string.h> #include<malloc.h> typedef struct LNode { int data;struct LNode *next; }LNode,*LinkList; void Creatlist(LinkList *L) { LinkList p,q;int num;*L=(LinkList)malloc(sizeof(LNode));q=*L;printf("输入若干整数,...

榆次区18337334642: 如何用C语言创建两个非递减的有序链表,并合并成一非递增的有序链表?? -
芮厕兰菌: 将其中一个链表插入到另一个链表就可以了,只需要修改指针,仍使用原来的存储空间. 可参考这里:http://zhidao.baidu.com/link?url=sI3yQkTWjmfSEfYqP9_s9CSrmt8D6yHQvP_EbHIqbsoIcDshn4E8VlzRnZ2XPZc7J8voSpuzyGaeDp4I3dTBaEv0-z3BpvRtX71yIw6qave

榆次区18337334642: 如何使用C语言实现两个链表的连接 -
芮厕兰菌: 以前学数据结构做过一个“非递减的链表合并一一个非递增的链表” 程序如下: #include #include typedef struct node{int data;struct node *next; }LinkList;/* 建立链表 */LinkList *create_link(int m) {LinkList *head,*s,*p;int i;head=(LinkList *...

榆次区18337334642: c语言合并两个有序单链表,使合并后的单链表非递增(或非递减) -
芮厕兰菌: #include 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...

榆次区18337334642: 数据结构(C语言) 单链表的合并(100分) -
芮厕兰菌: #include<stdio.h>#include<time.h>#include<stdlib.h> typedef struct list { long value; struct list * next; }list; void CreatList(list * p,int length) {int i,value; list *r,*s; for(i=0;i<length;i++) { value=rand();r=p; while(1) { if(r->next==NULL) { r->next=(list*)malloc(...

榆次区18337334642: 各位高手,请教,c语言中怎样将任意的的两个单链表合并到另一个链表中 -
芮厕兰菌: 拿指针将第2个链表的头插到第1个的尾上去!有序是靠指针来当针头,将一个个小圈连起来!你也可以打乱所有的数据,重头开始...

榆次区18337334642: C语言链表合并:将两个有序单向链表合并为一个单向有序链表,要求分别用两种方式实现~急~求大神帮忙 -
芮厕兰菌: 小意思!有个前提,两个链表的数据类型都是一样的哦 第一种:先新建一个链表,然后遍历第一链表,同时把它的值都赋给新建的链表,然后,开始第二个链表,也是同样的办法.加上第二个的时候,先找到新建链表的表尾,再表尾处开始添加第二个 第二种:首先遍历第一个链表,找到表尾,然后去掉第二个链表的表头,把第二个链表的头部赋给第一个链表的尾部 //当然,如果没有表头什么的就直接把第一个节点赋给第一个就行了. 第二种方法之后,两个链表就合成一个了.

榆次区18337334642: C语言将两个链表合并为一个 -
芮厕兰菌: 形参不应该是指针类型吗?取地址是个啥?void Merge(TxILink *T,TxILink *L) { struct TxILink* p = T; while(p->next) { p = p->next ; } p->next = L; } 这样就可以了.

榆次区18337334642: 求创建链表并实现两个链表的合并的程序? -
芮厕兰菌: 如果两个单向链表并且没有什么要求(b合并到a后)的话是比较好实现的 typedef struct listb{ int sum; struct listb *next; }LISTB; typedef struct lista{ int num; //add other number; struct lista *next; struct listb *link; }LISTA; LISTA * linklistfunc(LISTA *...

榆次区18337334642: 设计链表合并算法,将两个已排序(升序)的单链表,合并成一个链表而不改变其有序性.用c语言编写. -
芮厕兰菌: #include <stdio.h> #include <malloc.h> typedef struct node { int data; struct node *next; }List; List *create(List *head,int n)//创建链表 { List *p,*q; q=(List *)malloc(sizeof(List)); q->data=n; q->next=NULL; p=head; while(p->next!=NULL)p=p->next; p->...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网