国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【數(shù)據(jù)結(jié)構(gòu)】1.線性表的數(shù)組描述和鏈?zhǔn)矫枋?/h1>

這篇具有很好參考價(jià)值的文章主要介紹了【數(shù)據(jù)結(jié)構(gòu)】1.線性表的數(shù)組描述和鏈?zhǔn)矫枋?。希望?duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

1. 線性表抽象類

#pragma once
template <class T>
class LinearList
{
public:
    // 線性表是否為空
    virtual bool empty() const = 0;
    // 線性表大小
    virtual int size() const = 0;
    // 根據(jù)ID獲取線性表元素
    virtual T& get(int theIndex) const = 0;
    // 根據(jù)元素獲取元素對(duì)應(yīng)ID
    virtual int indexOf(const T& theElement) const = 0;
    // 刪除ID處的元素
    virtual void erase(int theIndex) = 0;
    // 在ID處插入元素
    virtual void insert(int theIndex, const T& theElement) = 0;
    // 輸出線性表
    virtual void output() = 0;
};

2. 線性表的數(shù)組描述

#include"linearList.h"
#include<iostream>
using namespace std;

template<class T>
class ArrayList :public LinearList<T>
{
protected:
    T* element;                                // 線性表元素指針
    int arrayLength;                        // 容量
    int listSize;                            // 元素個(gè)數(shù)
    bool checkIndex(int theIndex) const;    // 檢查索引是否有效
    void changeLength();                    // 擴(kuò)充數(shù)組長(zhǎng)度

public:
    // 構(gòu)造函數(shù)
    ArrayList(int initialCapacity = 10);
    // 拷貝構(gòu)造函數(shù)
    ArrayList(const ArrayList<T>& theList);
    // 析構(gòu)函數(shù)
    ~ArrayList() 
    {
        delete[] element;
    }

    // 線性表是否為空
    bool empty() const { return listSize == 0; }

    // 線性表大小
    int size() const { return listSize; }

    // 線性表容量
    int capacity() const { return arrayLength; }

    // 根據(jù)ID獲取線性表元素
    T& get(int theIndex) const;

    // 根據(jù)元素獲取元素對(duì)應(yīng)ID
    int indexOf(const T& theElement) const;

    // 刪除ID處的元素
    void erase(int theIndex);

    // 在ID處插入元素
    void insert(int theIndex, const T& theElement);

    // 輸出線性表
    void output();

};


// 構(gòu)造函數(shù)
template<class T>
ArrayList<T>::ArrayList(int initialCapacity)
{
    if (initialCapacity < 1) 
    {
        cout << "初始化容量必須大于0" << endl;
        return;
    }
    this->arrayLength = initialCapacity;
    this->element = new T[arrayLength];
    listSize = 0;
}

// 復(fù)制構(gòu)造函數(shù)
template<class T>
ArrayList<T>::ArrayList(const ArrayList<T>& theList)
{
    this->arrayLength = theList.arrayLength;
    this->listSize = theList.listSize;
    this->element = new T[arrayLength];
    copy(theList.element, theList.element + listSize, element);
}

// 越界, false 表示越界, true 表示沒(méi)有越界
template<class T>
bool ArrayList<T>::checkIndex(int theIndex) const
{
    bool ret = !(theIndex < 0 || theIndex > listSize);
    return ret;
}


// 獲取元素
template<class T>
T& ArrayList<T>::get(int theIndex) const
{
    if (checkIndex(theIndex))
    {
        return element[theIndex];
    }
}

// 根據(jù)元素獲取ID
template<class T>
int ArrayList<T>::indexOf(const T& theElement) const
{
    int theIndex = (int)find(element, element + listSize, theElement);
    return theIndex == listSize ? -1 : (theIndex-(int)element)/sizeof(T);
}

// 刪除ID處元素
template<class T>
void ArrayList<T>::erase(int theIndex)
{
    if (checkIndex(theIndex))
    {
        copy(element + theIndex + 1, element + listSize, element + theIndex);
        element[--listSize].~T();
    }
}

// 擴(kuò)充數(shù)組長(zhǎng)度
template<class T>
void ArrayList<T>::changeLength()
{
    T* temp = new T[arrayLength * 2];
    copy(element, element + arrayLength, temp);
    delete[] element;
    element = temp;
    arrayLength = 2 * arrayLength;
}


// 在ID處插入元素
template<class T>
void ArrayList<T>::insert(int theIndex, const T& theElement)
{
    if (!checkIndex(theIndex))
    {
        cout << "無(wú)效索引" << endl;
        return;
    }
    if (listSize == arrayLength)
    {
        changeLength();
    }
    copy_backward(element + theIndex, element + listSize, element + listSize + 1);
    element[theIndex] = theElement;
    listSize++;
}

// 輸出線性表
template<class T>
void ArrayList<T>::output()
{
    for (int i = 0; i < listSize; i++)
    {
        cout << element[i] << " ";
    }
    cout << endl;
}

3. 線性表的鏈?zhǔn)矫枋?/h2>

3.1 結(jié)點(diǎn)結(jié)構(gòu)體

#pragma once
#include<iostream>
using namespace std;
template <class T>
struct ChainNode
{
    // 數(shù)據(jù)成員
    T element;
    ChainNode<T>* next;

    // 方法
    ChainNode() {}
    ChainNode(const T& element)
    {
        this->element = element;
    }
    ChainNode(const T& element, ChainNode<T>* next)
    {
        this->element = element;
        this->next = next;
    }
};

3.2 線性表實(shí)現(xiàn)

#include"linearList.h"
#include"chianNode.h"
#include<iostream>
using namespace std;
template<class T>
class Chain :public LinearList<T>
{
protected:
    ChainNode<T>* firstNode;
    int listSize;
    bool checkIndex(int theIndex) const;

public:
    Chain(int initialCapacity = 10);
    Chain(const Chain<T>&);
    ~Chain();

    bool empty() const { return listSize == 0; };
    // 線性表大小
    int size() const { return listSize; }
    // 根據(jù)ID獲取線性表元素
    T& get(int theIndex) const;
    // 根據(jù)元素獲取元素對(duì)應(yīng)ID
    int indexOf(const T& theElement) const;
    // 刪除ID處的元素
    void erase(int theIndex);
    // 在ID處插入元素
    void insert(int theIndex, const T& theElement);
    // 輸出線性表
    void output();
};

// 普通的構(gòu)造函數(shù)
template<class T>
Chain<T>::Chain(int initialCapacity)
{
    if (initialCapacity < 1)
    {
        cout << "初始容量設(shè)置必須大于0" << endl;
    }
    firstNode = NULL;
    listSize = 0;
}

// 拷貝構(gòu)造函數(shù)
template<class T>
Chain<T>::Chain(const Chain<T>& theList)
{
    listSize = theList.listSize;
    if (listSize == 0)
    {
        firstNode = NULL;
        return;
    }
    ChainNode<T>* sourceNode = theList.firstNode;
    firstNode = new ChainNode<T>(sourceNode->element);
    sourceNode = sourceNode->next;
    ChainNode<T>* targetNode = firstNode;
    while (sourceNode != NULL)
    {
        targetNode->next = new ChainNode<T>(sourceNode->element);
        targetNode = targetNode->next;
        sourceNode = sourceNode->next;
    }
    targetNode->next = NULL;
}

// 析構(gòu)函數(shù)
template<class T>
Chain<T>::~Chain()
{
    while (firstNode != NULL)
    {
        ChainNode<T>* nextNode = firstNode->next;
        delete firstNode;
        firstNode = nextNode;
    }
}

template<class T>
T& Chain<T>::get(int theIndex) const
{
    if (checkIndex(theIndex))
    {
        ChainNode<T>* currentNode = firstNode;
        for (int i = 0; i < theIndex; i++)
        {
            currentNode = currentNode->next;
        }
        return currentNode->element;
    }
}

template<class T>
int Chain<T>::indexOf(const T& theElement) const
{
    ChainNode<T>* currentNode = firstNode;
    int index = 0;
    while (currentNode->element != theElement && currentNode != NULL)
    {
        currentNode = currentNode->next;
        index++;
    }
    return currentNode == NULL ? -1 : index;
}

template<class T>
void Chain<T>::erase(int theIndex)
{
    if (checkIndex(theIndex))
    {
        ChainNode<T>* deleteNode;
        if (theIndex == 0)
        {
            deleteNode = firstNode;
            firstNode = firstNode->next;
        }
        else if (theIndex < listSize - 1)
        {
            ChainNode<T>* p = firstNode;
            for (int i = 0; i < theIndex - 1; i++)
            {
                p = p->next;
            }
            deleteNode = p->next;
            p->next = p->next->next;
        }
        else
        {
            ChainNode<T>* p = firstNode;
            for (int i = 0; i < theIndex; i++)
            {
                p = p->next;
            }
            deleteNode = p;
            p->next = NULL;
        }
        listSize--;
        delete deleteNode;
    }
}

template<class T>
void Chain<T>::insert(int theIndex, const T& theElement)
{
    if (checkIndex(theIndex))
    {
        if (theIndex == 0)
        {
            firstNode = new ChainNode<T>(theElement, firstNode);
        }
        else
        {
            ChainNode<T>* p = firstNode;
            for (int i = 0; i < theIndex - 1; i++)
            {
                p = p->next;
            }
            p->next = new ChainNode<T>(theElement, p->next);
        }
        listSize++;
    }
}


template<class T>
void Chain<T>::output()
{
    ChainNode<T>* currentNode = firstNode;
    while (currentNode != NULL)
    {
        cout << currentNode->element << " ";
        currentNode = currentNode->next;
    }
    cout << endl;
}

template<class T>
bool Chain<T>::checkIndex(int theIndex) const
{
    bool ret = !(theIndex < 0 || theIndex > listSize);
    return ret;
}

?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-711778.html

到了這里,關(guān)于【數(shù)據(jù)結(jié)構(gòu)】1.線性表的數(shù)組描述和鏈?zhǔn)矫枋龅奈恼戮徒榻B完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包