编程菜鸟的日记-初学尝试编程-易传媒笔试题(C++实现)
题目:已知存在两个非递减的有序链表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++实现)的更多相关文章
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习9
#include <iostream> #include <fstream> #include <cstdlib> #include <string> ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习8
#include <iostream> #include <fstream> #include <cstdlib> const int SIZE=20; using ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习7
#include <iostream> #include <string> #include <cctype> using namespace std; int m ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习6
#include <iostream> #include <string> using namespace std; const int MSIZE=100; struct j ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习5
#include <iostream> using namespace std; const double N1=35000; const int N2=15000; const int ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习4
#include <iostream> using namespace std; const int strsize=30; const int BOPSIZE=5; void showm ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习3
#include <iostream> using namespace std; void showmenu(void) { cout<<"Please enter ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习2
#include <iostream> #include <cctype> using namespace std; const int MAXSIZE=10; int mai ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习1
#include <iostream>#include <cctype>using namespace std;int main(){ char ch; while((ch=c ...
随机推荐
- MariaDB修改默认字符集
MariaDB修改默认字符集,以及创建数据库授权 1 . 修改server默认字符集utf8 [root@aws my.cnf.d]# vim /etc/my.cnf.d/server.cnf [my ...
- MYSQL数据仓库infobright【备忘】
Infobright是一个基于MySQL开发的开源数据仓库(Data Warehouse)软件,可作为MySQL的一个存储引擎来使用,SELECT查询与普通MySQL无区别. 优点:查询性能高:百万. ...
- CentOS6.9安装Filebeat监控Nginx的访问日志发送到Kafka
一.下载地址: 官方:https://www.elastic.co/cn/downloads/beats/filebeat 百度云盘:https://pan.baidu.com/s/1dvhqb0 二 ...
- std::string 是什么
#include "stdafx.h" #include <iostream> #include <string> using std::cout; usi ...
- A - Exposition CodeForces - 6E
题目链接:https://vjudge.net/contest/202699#problem/A 题意 给一个n个元素的序列,从中挑出最长的子序列,要求子序列中元素差的最大值不超过k.问有几个最长子序 ...
- 需求:lr需要在一串数字中随机位置插入一个新数字的实现方式
效果如下: 需要用到sscanf()函数: 从一个字符串中读进与指定格式相符的数据. Action() { ],s2[],s3[]; int n=atoi(lr_eval_string(" ...
- python manage.py runserver指定端口和ip
python manage.py runserver 0.0.0.0:8000 在本地运行程序,python manager.py runserver打开http://127.0.0.1:5000端口 ...
- maven启动tomcat访问报404(url中没有项目名)
[INFO] Running war on http://localhost:8080/js_001(红色部分是项目名,要是没有的话是不能访问项目资源的) 但是我仍然不知道为什么有些maven项目却有 ...
- Excel ——多表关联查询-vlookup
一.分组 需求: 在B列的右侧添加一列[消费分组]对B列的[月分组水平]进行分组,原始数据如下: 公式:在 C2 输入:“=VLOOKUP(B2,$E$1:$G$4,2,1)”,下拉填充. 提示:VL ...
- day20 模块-sys,time,collection
所有常用模块的用法: http://www.cnblogs.com/Eva-J/articles/7228075.html 前情回顾: # 常用模块 # 常用模块 —— 东西多 # 异常处理 # 什 ...