【顺序表】13 顺序表ADT模板设计及简单应用:将顺序表中前 m 个元素和后 n 个元素进行互换

该博客介绍了如何利用自定义的顺序表ADT或STL中的vector模板,设计并实现一种算法,该算法能够在不使用大量辅助空间的情况下,将顺序表中前m个元素与后n个元素进行交换。通过三次反转操作,实现了从原始序列到目标序列的转换。示例代码展示了如何处理字符串输入,并提供了具体的输入输出范例。
摘要由CSDN通过智能技术生成

问题描述 :

目的:使用自行设计的顺序表ADT或STL中的vector模板,设计并实现顺序表应用场合的一些简单算法设计。

应用1:试设计一个算法,用尽可能少的辅助空间将非空顺序表中前 m 个元素和后 n 个元素进行互换,即将线性表(a1,a2,…,am,b1,b2,…,bn) 改变成(b1,b2,…,bn,a1,a2,…,am)。假定m始终是有效值。

参考函数原型:

(1)顺序表ADT版本

template<class ElemType>

void Exchange( SqList<ElemType> &A, int m ); // 本算法实现顺序表中前 m 个元素和后 n 个元素的互换

(2)vector版本

template<class ElemType>
void Exchange( vector<ElemType> &A, int m );// 本算法实现顺序表中前 m 个元素和后 n 个元素的互换

输入说明 :

第一行:顺序表的数据元素类型标记(0:int;1:double;2:char;3:string;其余值:输出err)

第二行:待处理顺序表的数据元素(数据元素之间以空格分隔)

第三行:逆置位置m

输出说明 :

第一行:逆置前顺序表的遍历结果(数据元素之间以“,”分隔)

空行

第三行:逆置后顺序表的遍历结果(数据元素之间以“,”分隔)

 

输入范例 :

0
13 5 27 9 32 123 76 98 54 87
5

输出范例 :

13,5,27,9,32,123,76,98,54,87

123,76,98,54,87,13,5,27,9,32

解题思路: 

这题变态的地方在于它是数据是一行……我实在搞不明白为啥出题人想这么折磨人,但是sstream可以轻松秒杀。

当然,如果你想用ADT来写,自己手写一个vector的对象,挑战一下人类极限,我也不会阻拦的。

反正就是你把数据存到vector里之后,只要三次反转就可以了。

原理如下:

原来的序列:

a1,a2,a3,b1,b2,b3,b4,b5    (m=3)

第一步反转整个序列,得出结果:

b5,b4,b3,b2,b1,a3,a2,a1

第二步反转1-(L-m)的序列,得出结果:

b1,b2,b3,b4,b5,a3,a2,a1

第三步反转(L-m)-L,得出答案:

b1,b2,b3,b4,b5,a1,a2,a3

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <sstream>
#include <stack>
#include <map>
#include <ctime>
#include <array>
using namespace std;
template<class ElemType>
void output(vector<ElemType>& A)
{
    int i;
    for (i = 0; i < A.size() - 1; i++)
        cout << A[i] << ',';
    cout << A[i];
    cout << endl;
    return;
}
template<class ElemType>
void Exchange(vector<ElemType>& A, int m)
{
    output(A);
    cout << endl;
    int n = A.size() - m;
    reverse(A.begin(), A.end());
    reverse(A.begin(), A.begin() + n);
    reverse(A.begin() + n, A.end());
    output(A);
    return;
}
int main()
{
    int i, j;
    int kinds;
    string data;
    int m;
    cin >> kinds;
    if (kinds != 0 && kinds != 1 && kinds != 2 && kinds != 3)
    {
        cout << "err" << endl;
        return 0;
    }
    cin.get();
    getline(cin, data);
    cin >> m;
    vector<string> part;
    string A_part;
    stringstream room;
    room.str(data);
    while (room >> A_part)
        part.push_back(A_part);
    if (kinds == 1)
    {
        for (i = 0; i < part.size(); i++)
        {
            double num_cahe;
            char str_cahe[20];
            num_cahe = atof(part[i].c_str());
            sprintf_s(str_cahe,"%g", num_cahe);
            part[i] = str_cahe;
        }
    }
    Exchange(part, m);
    return 0;
}

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值