公式化描述

公式化描述採用數組來表示一個對象的實例,數組中的每個位置被稱之爲單元或節點。

可用一個數學公式來確定每個元素在數組中的位置。

實現一個線性表類

#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;
    }
}

以上內容整理自網絡電子資料,僅供學習交流用,勿作商業用途。轉載請註明來源。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章