公式化描述

公式化描述采用数组来表示一个对象的实例,数组中的每个位置被称之为单元或节点。

可用一个数学公式来确定每个元素在数组中的位置。

实现一个线性表类

#pragma once
#include <iostream>
using namespace std;
template<class T>
class LinearList
{
public:
    LinearList(int MaxListSize = 10); //构造函数
    ~LinearList() { delete[] element; } //析构函数
    bool IsEmpty() const { return length == 0; }
    int Length() const { return length; }
    bool Find(int k, T& x) const; //返回第k个元素至x中
    int Search(const T& x) const; // 返回x所在位置
    LinearList<T>& Delete(int k, T& x); // 删除第k个元素并将它返回至x中
    LinearList<T>& Insert(int k, const T& x); // 在第k个元素之后插入x
    void Output(ostream& out) const;
private:
    int length;
    int MaxSize;
    T *element; // 一维动态数组
};

template<class T>
LinearList<T>::LinearList(int MaxListSize)
{
    // 基于公式描述法的线性表的构造函数
    MaxSize = MaxListSize;
    element = new T[MaxSize];
    length = 0;
}

template<class T>
bool LinearList<T>::Find(int k, T& x) const
{
    //把第k个元素取至x中
    //如果不存在第k个元素则返回false,否则返回true
    if (k < 1 || k > length) return false; // 不存在第k个元素
    x = element[k - 1];
    return true;
}

template<class T>
int LinearList<T>::Search(const T& x) const
{
    // 查找x,如果找到,则返回x所在的位置
    // 如果x不在表中,则返回0
    for (int i = 0; i < length; i++)
        if (element[i] == x) return ++i;
    return 0;
}

template<class T>
LinearList<T>& LinearList<T>::Delete(int k, T& x)
{
    // 把第k个元素放入x中,然后删除第k个元素
    // 如果不存在第k个元素,则引发异常OutOfBounds
    if (Find(k, x)) 
    {
        // 把元素k+l, ...向前移动一个位置
        for (int i = k; i < length; i++)
            element[i - 1] = element[i];   //第k个元素就在k-1的位置,k>0
        length--;
        return *this;
    }
    else throw OutOfBounds();
}

template<class T>
LinearList<T>& LinearList<T>::Insert(int k, const T& x)
{
    // 在第k个元素之后插入x
    // 如果不存在第k个元素,则引发异常OutOfBounds
    // 如果表已经满,则引发异常NoMem
    if (k < 0 || k > length) throw OutOfBounds();
    if (length == MaxSize) throw NoMem();
    //向后移动一个位置
    for (int i = length - 1; i >= k; i--)  //第k个位置后面一位的下标就是k
        element[i + 1] = element[i];
    element[k] = x;  //插入的位置是第k位之后
    length++;
    return *this;
}

template<class T>
void LinearList<T>::Output(ostream& out) const
{
    //把表输送至输出流
        for (int i = 0; i < length; i++)
            out << element[i] << " ";
}

// 重载< <
template <class T>
ostream& operator<<(ostream& out, const LinearList<T>& x)
{
    x.Output(out);
    return out;
}
#pragma once
#include <iostream>
using namespace std;
// 内存不足
class NoMem 
{
public:
    NoMem() {}
};
// 使new引发NoMem异常而不是xalloc异常
void my_new_handler()
{
    throw NoMem();  //什么意思?调用构造函数,创建了一个对象。
}
new_handler Old_Handler_ = set_new_handler(my_new_handler);

class OutOfBounds
{
public:
    OutOfBounds() {}
};
#include <iostream>
#include "LinearList.h"
#include "Xcept.h"
void main(void)
{
    try {
        LinearList<int> L(5);
        cout << "Length = " << L.Length() << endl;
        cout << "IsEmpty = " << L.IsEmpty() << endl;
        L.Insert(0, 2).Insert(1, 6);
        cout << "List is " << L << endl;
        cout << "IsEmpty = " << L.IsEmpty() << endl;
        int z;
        L.Find(1, z);
        cout << "First element is " << z << endl;
        cout << "Length = " << L.Length() << endl;
        L.Delete(1, z);
        cout << "Deleted element is " << z << endl;
        cout << "List is " << L << endl;
    }
    catch (...) {
        cerr << "An exception has occurred" << endl;
    }
}

以上内容整理自网络电子资料,仅供学习交流用,勿作商业用途。转载请注明来源。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值