【顺序表】15 顺序表ADT模板简单应用算法设计:删除顺序表中的冗余元素

问题描述 :

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

 

应用3:试设计一个算法,删除非空顺序表L中的冗余元素,即使得操作之后的顺序表中只保留操作之前表中所有值都不相同的元素(提纯)。

 

参考函数原型:

(1)顺序表ADT版本

template<class ElemType>
void Purge_Sq( Sqlist<ElemType> &L );

 

(2)vector版本

template<class ElemType>
void Purge_Sq( vector<ElemType> &L );

 

输入说明 :

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

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

 

输出说明 :

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

空行

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

输入范例 :

0
13 5 13 9 32 51 76 5 9 8

输出范例 :

13,5,13,9,32,51,76,5,9,8

13,5,9,32,51,76,8

解题代码: 

#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>
#include <set>
using namespace std;
template<class ElemType>
void output(vector<ElemType>& A)
{
    int i;
    if (A.size() == 0)
    {
        cout << "NULL" << endl;
        return;
    }
    for (i = 0; i < A.size() - 1; i++)
        cout << A[i] << ',';
    cout << A[i];
    cout << endl;
    return;
}
vector<string> depart_string(string data, int kinds)
{
    int i, j;
    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(str_cahe, "%g", num_cahe);
            part[i] = str_cahe;
        }
    }
    return part;
}
template<class ElemType>
void Purge_Sq(vector<ElemType>& L)
{
    int i, j;
    for (i = 0; i < L.size(); i++)
    {
        L.erase(remove(L.begin()+i+1,L.end(), L[i]), L.end());
    }
    output(L);
}
int main()
{
    int i, j;
    int kinds;
    string s1,s2;
    int m;
    //数值类型输入判断
    cin >> kinds;
    if (kinds != 0 && kinds != 1 && kinds != 2 && kinds != 3)
    {
        cout << "err" << endl;
        return 0;
    }
    cin.get();
    //---------------
    getline(cin, s1);
    //---------------
    vector<string> data1;
    data1 = depart_string(s1, kinds);
    output(data1);
    //--------------
    cout << endl;
    Purge_Sq(data1);
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值