数据结构与算法C++描述(3)---间接寻址

来源:互联网 发布:seo营销工具 编辑:程序博客网 时间:2024/05/15 21:41

间接寻址就是线性表和链表的组合,它利用了线性表中的公式化描述,同时也利用了链表中的链接指针。。采用这种描述方法,可以保留公式化描述方法的许多优点——可以根据索引在Θ( 1 )的时间内访问每个元素、可采用二叉搜索方法在对数时间内对一个有序表进行搜索等等。与此同时,也可以获得链表描述方法的重要特色——在诸如插入和删除操作期间不必对元素进行实际的移动。因此,大多数间接寻址链表操作的时间复杂性都与元素的总数无关。


1、 对间接寻址的操作有:

这里写图片描述

2、C++实现间接寻址

2.1 创建

通过实现IndirectList类,实现间接寻址的相关操作。创建一个间接寻址是通过IndirectList类的构造函数实现的。代码如下(其中,maxListSize是最大的容量,大于此值,便会抛出“溢出”异常):

//新建一个列表template <class T>IndirectList<T>::IndirectList(int maxListSize=10){    MaxSize = maxListSize;    table = new T*[MaxSize];    length = 0;}

2.2 删除

通过析构函数实现,由于table是指针类型的数组,即数组中每个元素都是是个指向列表中下一个元素的指针,因此需要首先删除每个指针后,在释放数组。:

template <class T>IndirectList<T>::~IndirectList(){    //删除表    for (int i = 0; i < length; i++)        delete table[i];    delete[] table;}

2.3 寻找第k个位置的元素

寻找第k个位置的元素,若存在,则将值赋给x,并返回true;否则,返回false。

template <class T> bool IndirectList<T>::Find(int k, T &x) const{    if (k > length || k<1)    {        throw OutOfRange();        return false;    }    else    {        x = *table[k-1];        return true;    }}

2.4 寻找列表中值为x的元素

寻找列表中值为x的元素,若存在,则返回位置,否则,返回0

template <class T>int IndirectList<T>::Search(const T&x) const{    int i = 1;    for (i = 1; i <= length; i++)    {        if (*table[i - 1] == x)            return i;        else            continue;    }    return 0;}

2.5 删除表中第k个元素

删除表中第k个元素,并將值赋给x,并返回操作后的列表

template <class T>IndirectList<T>& IndirectList<T>::Delete(int k, T &x){    if (Find(k, x))    {        //往前移        for (int i = k; i <= length; i++)            table[i - 1] = table[i];        length--;        return *this;    }    else        throw OutOfRange();}

2.6 在第k个位置之后插入元素x

在第k个位置之后插入元素x,并并返回操作后的列表

template <class T>IndirectList<T>& IndirectList<T>::Insert(int k, const T &x){    if (k<0 || k>length)        throw OutOfRange();    else if (k == MaxSize)        throw NoMerm();    else    {        //往后移        for (int i = 0; i <= length - k; i++)            table[length - i + 1] = table[length - i];        table[k] = new T;        *table[k] = x;        length++;        return *this;    }}

2.7 输出

通过重载“<<”运算符实现

//重载“<<”template <class T>ostream &operator<<(ostream &out, const IndirectList<T> &x){    x.Output(out);    return out;}//输出列表函数template <class T>void IndirectList<T>::Output(ostream &out) const{    for (int i = 0; i < length; i++)        out << *table[i] << " ";}

3、测试样例

IndirectList<int> inList;        //测试构造函数        cout <<"建立初始间接寻址:   " <<inList.Length() << endl;        //测试Insert函数        cout << "Insert(1,1).Insert(2,2).Insert(3,3):   " << inList.Insert(0, 1).Insert(1,2).Insert(2,3) << endl;        cout << "Insert(2,5):    " << inList.Insert(2, 5) << endl;        //测试Delete函数        int x;        inList.Delete(2, x);        cout << "Delete(2,x):   " << x <<"   "<<inList<< endl;        //测试Find函数        inList.Find(2, x);        cout << "Find(2,x):   " << x << "   " << inList << endl;        //cout << "Find(5,x):   " << x << "   " << inList.Find(5, x) << endl;        //测试Search函数        cout << "Search(2):   " << inList.Search(2) << "   " << inList << endl;        cout << "Search(1):   " << inList.Search(1) << "   " << inList << endl;

参考文献:
[1] 数据结构算法与应用:C++描述(Data Structures, Algorithms and Applications in C++ 的中文版)

原创粉丝点击