我是靠谱客的博主 合适寒风,这篇文章主要介绍DS顺序表--合并操作DS顺序表–合并操作,现在分享给大家,希望可以做个参考。

DS顺序表–合并操作

题目描述

建立顺序表的类,属性包括:数组、实际长度、最大长度(设定为1000)

已知两个递增序列,把两个序列的数据合并到顺序表中,并使得顺序表的数据递增有序

输入

第1行先输入n表示有n个数据,接着输入n个数据,表示第1个序列,要求数据递增互不等

第2行先输入m表示有m个数据,接着输入m个数据,表示第2个序列,要求数据递增互不等

输出

顺序表内容包括顺序表的实际长度和数据,数据之间用空格隔开

第1行输出创建后的顺序表内容

样例输入

3 11 33 55
5 22 44 66 88 99

样例输出

8 11 22 33 44 55 66 88 99

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream> using namespace std; class SeqList{ int *list; int len; int maxLen; public: SeqList(int n); ~SeqList(); void Init(); SeqList combineList(SeqList &li); //注意,这里要传引用,不然传进去的对象会被析构,list内存会被重复delete void outPut(); }; SeqList::SeqList(int n) { len = n; maxLen = 1000; list = new int[maxLen]; } SeqList::~SeqList() { delete []list; } SeqList SeqList::combineList(SeqList &li) { SeqList combine(len+li.len); int i=0,j=0,k=0; while (i<len && j<li.len) { if(list[i]<li.list[j]) { combine.list[k] = list[i]; i++; } else { combine.list[k] = li.list[j]; j++; } k++; } while (i<len) { combine.list[k] = list[i]; i++,k++; } while (j<li.len) { combine.list[k] = li.list[j]; j++,k++; } return combine; } void SeqList::Init() { for(int i=0;i<len;i++) cin>>list[i]; } void SeqList::outPut() { cout<<len<<' '; for(int i=0;i<len;i++) cout<<list[i]<<' '; cout<<endl; } int main() { int n,m; cin>>n; SeqList firstList(n); firstList.Init(); cin>>m; SeqList secondList(m); secondList.Init(); SeqList thirdList = firstList.combineList(secondList); thirdList.outPut(); return 0; }

最后

以上就是合适寒风最近收集整理的关于DS顺序表--合并操作DS顺序表–合并操作的全部内容,更多相关DS顺序表--合并操作DS顺序表–合并操作内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部