顺序表ADT模板简单应用算法设计:在给定的顺序表中找出最大和最小的元素

该文提供了一个模板实现,用于在顺序表(ADT)和STL的vector中寻找最大和最小元素的算法。输入包括数据类型和顺序表元素,输出为最大值和最小值。代码实现了从输入流创建和初始化顺序表,以及打印、交换元素和找交集的功能。时间复杂度为O(n)。
摘要由CSDN通过智能技术生成


前言:多刷题,多总结,你还有梦想啊!

题目

顺序表ADT模板简单应用算法设计:在给定的顺序表中找出最大和最小的元素
时间限制: 1S类别: DS:线性表->顺序表–简单

题目描述

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

应用8:试设计一个算法,找出顺序表A中最大和最小的元素(输出在屏幕上),并保持原顺序表不变。

(1)顺序表ADT版本

参考函数原型:

template
bool Search_Max_Min( const SqList &A, ElemType &max, ElemType &min );

(2)vector版本

参考函数原型:

template
bool Search_Max_Min( const vector &A, ElemType &max, ElemType &min );

输入范例

第一行:顺序表A的数据元素的数据类型标记(0:int,1:double,2:char,3:string)

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

输出范例

如第一行输入值为0、1、2、3之外的值,直接输出“err”

否则:

第一行:顺序表A的遍历结果

第二行:最大值max

第三行:最小值min

AC代码

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <vector>
using namespace std;

template <class Elemtype>
void printff(vector<Elemtype>a);

template<class Elemtype>
void init(vector<Elemtype>& a) {
    int i = 0;
    for (int i = 0; i < a.size() - 1; i++) {
        if (a[i] == a[i + 1])
            a.erase(a.begin() + 1);
        else continue;
    }

}
template<class Elemtype>
void creat(stringstream& input, vector<Elemtype>& a) {
    Elemtype str;
    while (input >> str) {
        a.push_back(str);
    }
    //printff(a);
    //init(a);
}

template<class Elemtype>
void printff(vector<Elemtype> a) {
    for (int i = 0; i < a.size(); i++) {
        cout << a[i];
        if (i < a.size() - 1)
            cout << ",";
    }
    cout << endl;
}

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

    for (int i = m; i < A.size(); i++) {
        cout << A[i];
        if (i < A.size())
            cout << ",";
    }
    for (int j = 0; j < m; j++) {
        cout << A[j];
        if (j < m - 1)
            cout << ",";
    }
    cout << endl;
}
template<class ElemType>
void Intersect_Sq_OL_C(const vector<ElemType>& A, const vector<ElemType>& B) {
    vector<ElemType> C;
    cout << endl;
    auto i = A.begin();//定位当前位置
    for (int j = 0; j < B.size(); j++) {
        auto it = find(A.begin(), A.end(), B[j]);//遍历A查找元素B[j]
        if (it != A.end()) {//找到了,放进c
            C.push_back(*it);
            i = it;//更新当前位置
        }
    }
    printff(C);
    cout << endl;
}

template<class ElemType>
void Search_Max_Min(const vector<ElemType>& A, ElemType& max, ElemType& min) {
    max = A.front();
    min = A.front();
    printff(A);
    auto it = A.begin();
    while (it != A.end()) {
        if (max < *it) {
            max = *it;
        }
        if (min > *it) {
            min = *it;
        }
        it++;
    }
    cout << max<<endl;
    cout << min;
}

int main() {
    int n;
    string s1;
    cin >> n;
    cin.get();
    getline(cin, s1);
    stringstream input1(s1);

    vector<int>Int1, Int2;
    vector<char>Char1, Char2;
    vector<double>Double1, Double2;
    vector<string>S1, S2;
    
    if (n < 0 || n > 3) {
        cout << "err" << endl;
        return 0;
    }
    if (n == 0) {//int情况
        creat(input1, Int1);
        int max = 0, min = 0;
        Search_Max_Min(Int1, max, min);
    }
    if (n == 1) {//double情况
        creat(input1, Double1);
        double max, min;
        Search_Max_Min(Double1,max,min);
    }
    if (n == 2) {//char情况
        creat(input1, Char1);
        char max, min;
        Search_Max_Min(Char1, max, min);
    }
    if (n == 3) {//string情况
        creat(input1, S1);
        string max, min;
        Search_Max_Min(S1, max, min);
    }
    return 0;
}

时间复杂度

O(n)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值