题目:已知存在两个非递减的有序链表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建表范例

    建表范例:支持update和delete create table aaa( id string, visitor_name string ) clustered by(id) into bucket ...

  2. 下载离线VS2017

    1.下载工具 版本 文件 Visual Studio Enterprise (企业版) vs_enterprise.exe Visual Studio Professional (专业版) vs_pr ...

  3. MyBatis - 8.MyBatis工作原理

    Mybatis 配置 1.SQLSessionFactory的初始化 根据配置文件获取 SqlSessionFactory 2.openSession获取SqlSession对象 3.getMappe ...

  4. .Net分布式锁

    项目中一般使用lock作为锁,以便于多线程操作确保库内数据统一.但是如果分布式部署项目,则这种锁就是去了意义,这时可以使用redis或memcache的add方法作为分布式锁. 栗子

  5. 如何保证Redis的高并发

    单机的redis几乎不太可能说QPS超过10万+,一般在几万. 除非一些特殊情况,比如你的机器性能特别好,配置特别高,物理机,维护做的特别好,而且你的整体的操作不是太复杂. Redis通过主从架构,实 ...

  6. 3.ELK 之elasticsearch CRUD

    名词介绍 index: type: document: 数据类型: 索引(index)创建示例 .   type的创建(7.x之后)将会去掉该内容,点我看为什么? 参考脚本: mapping使用 其他 ...

  7. 【Android】Android自定义属性,attr format取值类型

    1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name = "名称"> <attr name = &quo ...

  8. net core体系-web应用程序-4net core2.0大白话带你入门-6asp.net core配置文件

    asp.net core配置文件   读取配置文件 asp.net core使用appsettings.json代替传统.net framework的web.config中的<appSettin ...

  9. IE8兼容问题

    最近做的网站,需要兼容IE8,在这里记录一下,碰到的问题,方便以后查看补充 1.CSS选择器nth-child 不兼容 ul li:nth-child(2){ background-image: ur ...

  10. js,JQuery 生成二维码

    代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...