數據結構之【實現數組】

首先需要定義一個存放數據的結構體(如果是C++的話可以使用類)

//定義數據結構體
struct MyArry
{
    int* pBase;  //存儲數組第一個元素的地址
    int nCnt;    //當前有效元素的個數
    int nLen;    //數組能夠存放元素最大的個數
};

接著實現一下數組的追加、插入、刪除、排序、顯示等方法

bool append_arry(MyArry * pArr, int nValue); //追加
bool insert_arry(MyArry * pArr, int nPos, int nValue); //插入
bool delete_arry(MyArry * pArr, int nPos, int *pValue); //刪除數組內容
bool isEmpty(MyArry * pArr); //是否為空
bool isFull(MyArry * pArr); //是否滿了
void sort_arry(MyArry* pArr);
void show_arry(MyArry * pArr);
void inversion_arry(MyArry * pArr);                    //反轉數組元素
void init_arry(MyArry* pArr, int nLength);

在定義一個結構體變量的時候,需要對結構體的內容進行初始化,那么初始化的函數實現是這么來實現的
形參列表為指向數組地址的指針變量,一個初始化數組大小的長度

具體代碼如下:

void init_arry(MyArry* pArr, int nLength)
{
    pArr->pBase = (int*)malloc(sizeof(int) * nLength);

    if (nullptr == pArr->pBase)
    {
        printf_s("%s \n", "內存分配失敗");
        exit(-1);
    }
    else
    {
        pArr->nCnt = 0;
        pArr->nLen = nLength;
    }
}

接下來實現一下追加的函數

bool append_arry(MyArry* pArr, int nValue)
{
    if (isFull(pArr))
    {
        return false;
    }

    pArr->pBase[pArr->nCnt] = nValue;
    ++(pArr->nCnt);

    return true;
}

那么較為復雜的為插入、刪除元素,重點講解一下

比如數組中已有5個元素

存儲5個元素的數組

假設在第三個位置插入一個元素,那么可以傳入一個數組位置,假設定義為:

int nPos = 3;

在第三個數組位置插入元素

那么插入的方法是這樣的,需要把位置為3的內容移動到后面,在移動的時候需要從最后一個元素進行移動

大概是這樣的:

插入元素移動示意圖

在插入的過程中,是有一些約束條件的,比如傳入的nPos是不能小于1的,并且nPos不能大與當前有效元素個數+1

因為傳入的nPos是從1開始的,而數組元素是從0開始的,當nPos大與當前有效元素的個數的時候,造成了跨越插入

因為數組是連續存儲的,所以不支持這么插入

還有一個條件是當數組已經滿的時候也是無法插入的

具體代碼實現如下:

bool insert_arry(MyArry* pArr, int nPos, int nValue)
{
    if (isFull(pArr))
    {
        return false;
    }

    if (nPos < 1 || nPos > pArr->nCnt + 1)
    {
        return false;
    }

    //插入數據
    for (int i = pArr->nCnt - 1; i >= nPos - 1; --i)
    {
        pArr->pBase[i + 1] = pArr->pBase[i];
    }

    pArr->pBase[nPos - 1] = nValue;
    ++(pArr->nCnt);

    return true;
}

刪除某一個位置的元素應該怎么實現?

舉個例子,假如這里還有一個元素為5個的數組

存儲5個元素的數組

假設需要刪除第三個元素

刪除第三個元素

那么是不是只需要把后面的元素移動到刪除的位置就可以了

示例圖:


移動元素示意圖

具體的代碼實現如下:

bool delete_arry(MyArry* pArr, int nPos, int* pValue)
{
    if (isEmpty(pArr))
    {
        return false;
    }

    if (nPos < 1 || nPos > pArr->nCnt)
    {
        return false;
    }

    *pValue = pArr->pBase[nPos - 1];

    for (int i = nPos; i < pArr->nCnt; i++)
    {
        pArr->pBase[i - 1] = pArr->pBase[i];
    }

    --(pArr->nCnt);

    return true;
}

其他函數實現:

//判斷數組是不是為空
bool isEmpty(MyArry* pArr)
{
    if (0 == pArr->nCnt)
    {
        return true;
    }

    return false;
}

//判斷數組是不是滿了
bool isFull(MyArry* pArr)
{
    if (pArr->nCnt == pArr->nLen)
    {
        return true;
    }

    return false;
}

//使用冒泡排序
void sort_arry(MyArry* pArr)
{
    int k = 0;

    for (int i = 0; i < pArr->nCnt; i++)
    {
        for (int j = i + 1; j < pArr->nCnt; j++)
        {
            //升序
            if (pArr->pBase[i] > pArr->pBase[j])
            {
                k = pArr->pBase[i];
                pArr->pBase[i] = pArr->pBase[j];
                pArr->pBase[j] = k;
            }
        }
    }
}

void show_arry(MyArry* pArr)
{
    if (isEmpty(pArr))
    {
        printf_s("%s \n", "數組為空");
    }
    else
    {
        for (int i = 0; i < pArr->nCnt; i++)
        {
            printf_s("%d \n", pArr->pBase[i]);
        }
    }
}

void inversion_arry(MyArry* pArr)
{
    int i = 0;
    int j = pArr->nCnt - 1;

    int t = 0;

    while (i < j)
    {
        t = pArr->pBase[i];
        pArr->pBase[i] = pArr->pBase[j];
        pArr->pBase[j] = t;

        ++i;
        --j;
    }
}

int main()
{
    MyArry arr;
    init_arry(&arr, 6);
    
    show_arry(&arr);

    append_arry(&arr, 1);
    append_arry(&arr, 2);
    append_arry(&arr, 80);
    append_arry(&arr, 4);
    append_arry(&arr, 5);
    //append_arry(&arr, 6);

    insert_arry(&arr, 6, 99);

    inversion_arry(&arr);

    sort_arry(&arr);

    /*int nDeleteValue = -1;
    if (delete_arry(&arr, 6, &nDeleteValue))
    {
        printf_s("%s \n", "刪除成功");
        printf_s("刪除的值是:%d \n", nDeleteValue);
    }
    else
    {
        printf_s("%s \n", "刪除失敗");
    }*/
    

    show_arry(&arr);
    //printf_s("%d \n", arr.nCnt);
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容