数据结构C++版 王红梅 OJ习题

1001: 顺序表(1)

Description

已知顺序表类的定义如下,实现各个成员函数。主函数中输入数据(以0结束)利用Insert函数依次将数据插入到表的1号位置,利用DispList按照逻辑次序输出表中元素,再输入一个要查找的元素,利用查找函数Locate查找其在表中的位置,最后利用Reverse函数将数据逆序,再利用DispList输出。

template <class T>

class SeqList{

       public:

              SeqList(); //构造函数,将表置空

              ~SeqList(){} //析构

              int Locate(T x); //查找元素x在表中的位置,成功返回x在表中的位置,否则返回0

              void Insert(int i, T x); //在表的i位置插入元素x

              void Reverse(); //逆序表中数据

              void DispList();

       private:

              T data[MaxSize];    //存储元素

          int length;              //顺序表实际长度

};

Input

输入样例说明:例如输入数据为:1 2 3 4 5 6 0 3,即将1,2,3,4,5,6插入表中的1号位置,得到逻辑次序为6,5,4,3,2,1的顺序表,3为在表中待查找的数据,3的位置为4。

输入:1 2 3 4 5 6 0 3

Output

输出:

The length:6
The elements:
6 5 4 3 2 1
Found position:4
The length:6
The elements:
1 2 3 4 5 6

若查找的数据不存在,则输出:No found,例如下例

输入:1 2 3 4 5 6 0 9

输出:

The length:6
The elements:
6 5 4 3 2 1
No found
The length:6
The elements:
1 2 3 4 5 6

Sample Input

1 2 3 4 5 6 0 3

Sample Output

The length:6
The elements:
6 5 4 3 2 1 
Found position:4
The length:6
The elements:
1 2 3 4 5 6 
#include <iostream>

using namespace std;
const int MaxSize = 1000;

template<class T>
class SeqList {
public:
    SeqList(); //构造函数,将表置空
    ~SeqList() {} //析构
    int Locate(T x); //查找元素x在表中的位置,成功返回x在表中的位置,否则返回0
    void Insert(int i, T x); //在表的i位置插入元素x
    void Reverse(); //逆序表中数据
    void DispList();

private:
    T data[MaxSize];    //存储元素
    int length;              //顺序表实际长度
};

template<class T>
SeqList<T>::SeqList() {
    length = 0;//将表置为空
}

template<class T>
int SeqList<T>::Locate(T x) {
    for (int i = 0; i < length; ++i) {
        if (data[i] == x) {
            return i + 1;
        }
    }
    return 0;
}

template<class T>
void SeqList<T>::Reverse() {
    for (int i = 0; i < length / 2; i++) {
        swap(data[i], data[length - i - 1]);
    }
}

template<class T>
void SeqList<T>::Insert(int i, T x) {
    if (length >= MaxSize) throw "上溢";
    if (i < 1 || i > length + 1) throw "位置";
    for (int j = length; j >= i; j--) {
        data[j] = data[j - 1];
    }
    data[i - 1] = x;
    length++;
}

template<class T>
void SeqList<T>::DispList() {
    for (int i = 0; i < length; ++i) {
        cout << data[i] << " ";
    }
    cout << endl;
}

int main() {
    SeqList<int> list;
    int num, i = 0;
    while (cin >> num, num) {
        list.Insert(1, num);
        i++;
    }
    int temp;
    cin >> temp;
    cout << "The length:" << i << "\nThe elements:\n";
    list.DispList();
    int locate = list.Locate(temp);
    if (locate > 0) {
        cout << "Found position:" << locate << endl;
    } else {
        cout << "No found" << endl;
    }
    list.Reverse();
    cout << "The length:" << i << "\nThe elements:\n";
    list.DispList();
    return 0;
}

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值