题目描述

请在上题SortList类的基础上添加成员函数Merge,实现两个有序表的合并。部分代码已经给出,请勿改动。注意:合并算法效率要求为O(m+n),不能借助排序算法实现。
//有序表类
template
class SortList{
public:
SortList(){length=0;}
~SortList(){}
void Insert(T x); //有序表的插入,使序列仍有序
void DispList(); //输出表
/成员函数Merge实现两个有序表的合并,使序列仍有序,
将A表和B表合并到当前类中,要求:A表,B表的元素保持不变
/
void Merge(SortList &A,SortList &B);
private:
T data[MaxSize]; //存储元素
int length; //顺序表实际长度
};
//构造有序表A:函数声明
template
void CreateSort(SortList &A);
int main(){

SortList A,B,C;
//生成一个有序表A
CreateSort(A);
//生成一个有序表B
CreateSort(B);
try{
C.Merge(A,B);//合并A,B表为C表
}
catch(const char wrong){
cout << wrong; //如失败提示失败信息
}
A.DispList();
B.DispList();
C.DispList(); //显示合并后的结果
return 0;
}

//构造有序表A:函数定义
template
void CreateSort(SortList &A){
int i,n;
T x;
cin>>n;
for (i=1;i<=n;i++){
cin>>x;
try{
A.Insert(x);
}
catch(char
wrong){
cout< }
}
}

输入

数据输入格式:第一个为所创建表的元素个数,之后是各个元素值,例如,下例输入,两个表的元素个数分别为5,6。
5 4 24 2 42 3
6 78 36 34 24 64 43

输出

样例输入

5 4 24 2 42 3
6 78 36 34 24 64 43

样例输出

The length:5
The elements:
2 3 4 24 42
The length:6
The elements:
24 34 36 43 64 78
The length:11
The elements:
2 3 4 24 24 34 36 42 43 64 78

提示

来源

提交

  1. import java.util.Scanner;
  2. class SqList {
  3. Object[] listElem;
  4. int curLen;
  5. String type;
  6. public SqList(int maxSize,String type) {
  7. curLen = 0;
  8. listElem = new Object[maxSize];
  9. this.type = type;
  10. }
  11. public int length() {
  12. return curLen;
  13. }
  14. public void insert(Object x) throws Exception {
  15. if (curLen == listElem.length)
  16. throw new Exception("顺序表已满");
  17. int i;
  18. for(i = 0;i<curLen;i++){
  19. if(type.equals("int")){
  20. if((int)listElem[i]>(int)x)break;
  21. }else{
  22. if((char)listElem[i]>(char)x)break;
  23. }
  24. }
  25. for (int j = curLen; j > i; j--)
  26. listElem[j] = listElem[j - 1];
  27. listElem[i] = x; // 插入 x
  28. curLen++; // 表长加1
  29. }
  30. public void merge(SqList A,SqList B) throws Exception {
  31. listElem = new Object[A.length()+B.length()];
  32. int ai = 0, bi = 0;
  33. while (ai < A.curLen && bi < B.curLen) {
  34. if ((int)A.listElem[ai] < (int)B.listElem[bi]) {
  35. listElem[curLen++] = A.listElem[ai++];
  36. } else {
  37. listElem[curLen++] = B.listElem[bi++];
  38. }
  39. }
  40. while (ai < A.curLen) listElem[curLen++] = A.listElem[ai++];
  41. while (bi < B.curLen) listElem[curLen++] = B.listElem[bi++];
  42. }
  43. public void display() {
  44. System.out.println("The length:"+length());
  45. System.out.println("The elements:");
  46. for (int j = 0; j < curLen; j++) {
  47. System.out.print(listElem[j]+ " ");
  48. }
  49. System.out.println();
  50. }
  51. }
  52. public class Main {
  53. public static void main(String[] args) throws Exception {
  54. Scanner sc = new Scanner(System.in);
  55. SqList A,B,C ;
  56. int l = sc.nextInt();
  57. A = new SqList(l,"int");
  58. for (int i = 0; i < l; i++) {
  59. try {
  60. A.insert(sc.nextInt());
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. l = sc.nextInt();
  66. B = new SqList(l,"int");
  67. for (int i = 0; i < l; i++) {
  68. try {
  69. B.insert(sc.nextInt());
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. C = new SqList(0,"int");
  75. C.merge(A,B);
  76. A.display();
  77. B.display();
  78. C.display();
  79. }
  80. }