题目:已知存在两个非递减的有序链表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. 006-Python函数

    Python函数(def) 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段.Python提供了许多内建函数,比如print().但你也可以自己创建函数,这被叫做用户自定义函数.函数能 ...

  2. (Access denied for user 'root'@'slaver1' (using password: YES))

    1.问题描述,启动azkaban的时候报如下所示的错误.之前使用azkaban是root用户,今天使用hadoop用户进行配置和使用,报这个错,说是root连接mysql拒绝了. [hadoop@sl ...

  3. Hadoop数据分析平台项目实战(基于CDH版本集群部署与安装)

    1.Hadoop的主要应用场景: a.数据分析平台. b.推荐系统. c.业务系统的底层存储系统. d.业务监控系统. 2.开发环境:Linux集群(Centos64位)+Window开发模式(win ...

  4. Centos6安装SaltStack

    rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm yum install ...

  5. SQL Server中自定义函数:用指定的分隔符号分割字符串

    微软SQL Server数据库中包含了很多内置的函数,入下图: 它们用于处理日期.数学.元数据.字符串等. 其中最为常用的就是处理字符串,里面包含了CharIndex()等函数,非常方便使用. 但是对 ...

  6. Flink--sink到kafka

    package com.flink.DataStream import java.util.Properties import org.apache.flink.api.common.serializ ...

  7. day8.登陆注册简单实现

    username = input('请输入注册的用户名:') password = input('请输入注册名的密码:') with open('list_of_info',mode='w',enco ...

  8. Linux用过的命令集合

    1,查看是否安装过openssl:(openssl version -a)(rpm -qa|grep -i openssl) 2,安装gcc:(yum install gcc-c++) 3,查看主机名 ...

  9. themeleaf引入公共页面

    <div th:include="/top :: html"></div>//引用公共页面 <div th:replace="head&qu ...

  10. 基于Docker的服务器搭建

    -----------基于Docker的多种服务器搭建----------- 开发环境 本机上的虚拟机 Centos7.4 Docker1.13.1 Openssl1.1.1 1 Nginx 1.1 ...