题目:已知存在两个非递减的有序链表List1和List2,现在需要你将两个链表合并成一个有序的非递增序列链表List3,请用C++编码实现。(所有链表均为单链表结构)

思路:此处链表是否都有表头并没有规定。

1:将链表List1和List2合并成一个非递减有序链表List3;

2:再将链表List3进行逆序,即可。

#include <iostream>
using namespace std;
typedef int ElemType;
typedef struct LNode
{
 ElemType data;
 struct LNode *next;
}LinkList;
//采用尾插法创建表
void CreateListR(LinkList *&L,ElemType a[],int n)
{
 LinkList *s,*r;//将数组中的数值存在结点s,再插入到链表L中
 int i;
 L=(LinkList *)malloc(sizeof(LinkList));//链表L头结点
 r=L;//起初指向链表头结点
 for(i=0;i<n;i++)
 {
  s=(LinkList *)malloc(sizeof(LinkList));
  s->data=a[i];
  r->next=s;
  r=s;
 }
 r->next=NULL;//结束标识
}

LinkList *Merge(LinkList *&L1,LinkList *&L2)
{
 if(L2==NULL)
  return L1;
 if(L1==NULL)
  return L2;
 LinkList *L3;
 //L3=(LinkList *)malloc(sizeof(LinkList));
 if(L1->data<=L2->data)
 {
  L3=L1;
  L3->next=Merge(L1->next,L2);
 }
 else
 {
  L3=L2;
  L3->next=Merge(L1,L2->next);
 }
 return L3;
}

LinkList *ListReverse(LinkList *&L)
{
 LinkList *s,*q,*p;
 //s指向逆序后的链表中的当前元素,q指向待逆序链表的当前元素,p指向待逆序链表中当前结点的下一个结点
 if(L==NULL||L->next==NULL)
  return L;
 else
 {
  //s=L;//以链表无头结点的逆序
  s=L->next;//s指向链表头结点的下一个结点
  p=s->next;
  s->next=NULL;
  while(p)
  {
   q=p->next;
   p->next=s;
   s=p;
   p=q;
  }
  //L=s;//p为NULL,s指向逆序元素的当前元素,即链表的第一个元素(若链表无表头,则注释掉这句)
        L->next=s;//若有,链表头结点下一个指向s
 }
 return L;
}
void DispList(LinkList *&L)
{
 LinkList *p=L->next;
 while(p)
 {
  cout<<p->data<<" ";
  p=p->next;
 }
 cout<<endl;
}
int main()
{
 ElemType a1[3]={1,2,3};
 ElemType a2[3]={2,3,4};
 LinkList *L1,*L2;
 int n=3;
 CreateListR(L1,a1,n);
    CreateListR(L2,a2,n);
 DispList(L1);
 DispList(L2);
    LinkList *L3;
 //L3=(LinkList *)malloc(sizeof(LinkList));
 L1=L1->next;//除去头结点
 //L2=L2->next;//除去头结点
    L3=Merge(L1,L2);//以链表1的头结点开始
 DispList(L3);
 L3=ListReverse(L3);
 DispList(L3);
 system("pause");
 return 0;
}

编程菜鸟的日记-初学尝试编程-易传媒笔试题(C++实现)的更多相关文章

  1. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习9

    #include <iostream> #include <fstream> #include <cstdlib> #include <string> ...

  2. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习8

    #include <iostream> #include <fstream> #include <cstdlib> const int SIZE=20; using ...

  3. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习7

    #include <iostream> #include <string> #include <cctype> using namespace std; int m ...

  4. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习6

    #include <iostream> #include <string> using namespace std; const int MSIZE=100; struct j ...

  5. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习5

    #include <iostream> using namespace std; const double N1=35000; const int N2=15000; const int ...

  6. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习4

    #include <iostream> using namespace std; const int strsize=30; const int BOPSIZE=5; void showm ...

  7. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习3

    #include <iostream> using namespace std; void showmenu(void) { cout<<"Please enter ...

  8. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习2

    #include <iostream> #include <cctype> using namespace std; const int MAXSIZE=10; int mai ...

  9. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习1

    #include <iostream>#include <cctype>using namespace std;int main(){ char ch; while((ch=c ...

随机推荐

  1. hive表中字段显示为NULL时,HDFS文件中存储为\N

    hive数据落地到hdfs,null会默认用'\N'存储 解决方式1:利用命令(这个我没起效果) alter table adl_cici_test_fdt set serdeproperties(' ...

  2. System.Data.Entity.Internal.AppConfig"的类型初始值设定项引发异常

    在学习EF code First的小案例的时候,遇见了这个异常 <configSections> <!-- For more information on Entity Framew ...

  3. C#使用Emit构造拦截器动态代理类

    在AOP编程概念介绍中,常见的示例为拦截对象,并在对象的某方法执行前和执行后分别记录日志. 而最常用的拦截方式是使用动态代理类,用其封装一个日志拦截器,当方法被执行时进行日志记录. 日志拦截器类 1 ...

  4. ubuntud安装Adobe Flash Player / Plugin

    1.https://get.adobe.com/flashplayer/ , select tar.gz for other Linux, download 2.Unpack the tar.gz f ...

  5. nginx 域名泛解析

    部分应用场景下要求服务器根据客户输入的二级域名地址自动访问不同的页面,比如一个服务器放置了不同的业务,商城.官网等多个业务,又不想一个个配置server, 网站目录结构入戏: html 网站根目录 m ...

  6. Windows下80端口被进程System占用的解决方法

    最近电脑时不时就发生了80端口被占用的情况,简单百度解决后,当重启电脑的时候又发生被占用的情况.今天非常幸运的是,发生了80端口和8080端口都被占用了情况,忍无可忍决定下定决心解决这个坑爹的问题,经 ...

  7. google hacking

    Google是一个强大的搜索引擎:而对于黑客而言,则可能是一款绝佳的黑客工具.正因为google的检索能力强大,黑客可以构造特殊的关键字,使用Google搜索互联网上的相关隐私信息.通过Google, ...

  8. docker compose启动服务超时重启记录

    一.停docker systemctl stop docker 然后ps -aux grep docker发现有些docker进程还是存在,此时强杀存在的docker进程:ps -aux|grep d ...

  9. BZOJ2480 Spoj3105 Mod 数论 扩展BSGS

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2480.html 题目传送门 - BZOJ2480 题意 已知数 $a,p,b$ ,求满足 $a^x≡b ...

  10. 数学建模:2.监督学习--分类分析- KNN最邻近分类算法

    1.分类分析 分类(Classification)指的是从数据中选出已经分好类的训练集,在该训练集上运用数据挖掘分类的技术,建立分类模型,对于没有分类的数据进行分类的分析方法. 分类问题的应用场景:分 ...