关于动态顺序表的所有操作代码

定义如下:

#pragma once

typedef int DataType;

#define LIST_SIZE 1
#define EMPTY_SIZE 0

typedef struct SqListD
{
    DataType *arry;
    size_t iSize;
    size_t iCapacity;
}SqListD,*PSeqList;

操作代码如下:

#define _CRT_SECURE_NO_WARININGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "SqListD.h"



// 实现以下函数
// 初始化动态顺序表
void InitSeqListD(PSeqList pSeqList)
{
    pSeqList->arry = (DataType*)malloc(LIST_SIZE*sizeof(DataType));
    //pSeqList->arry = NULL;
    pSeqList->iCapacity = LIST_SIZE;
    pSeqList->iSize = 0;    
}

// 空间不够时动态增容
void _CheckCapacity(PSeqList pSeqList)
{
    int iCapTemp = 0;
    DataType *pTemp;
    assert(pSeqList);
    if (pSeqList->iSize >= pSeqList->iCapacity)
    {
        iCapTemp = 2 * pSeqList->iCapacity;
        pTemp = (DataType*)realloc(pSeqList->arry, iCapTemp*sizeof(DataType));
        if (pSeqList->arry == NULL)
        {
            printf("扩容失败\n");
            return;
        }
        pSeqList->iCapacity = iCapTemp;
        pSeqList->arry = pTemp;
    }
}
// 尾部插入
void PushBack(PSeqList pSeqList, DataType data)
{
    assert(pSeqList);
    _CheckCapacity(pSeqList);
    pSeqList->arry[pSeqList->iSize] = data;
    pSeqList->iSize++;
}
// 尾部删除
void PopBack(PSeqList pSeqList)
{
    assert(pSeqList);
    if (pSeqList->iSize == 0)
    {
        printf("顺序表为空,不能进行删除操作\n");
        return;
    }
    pSeqList->iSize--;

}
// 头部插入
void PushFront(PSeqList pSeqList, DataType data)
{
    int iBegin = 0;
    assert(pSeqList);
    _CheckCapacity(pSeqList);
    for (iBegin = pSeqList->iSize ; iBegin >= 0; iBegin--)
    {
        pSeqList->arry[iBegin] = pSeqList->arry[iBegin - 1];
    }
    pSeqList->arry[0] = data;
    pSeqList->iSize++;
}
// 头部删除
void PopFront(PSeqList pSeqList)
{
    int iBegin = 0;
    assert(pSeqList);
    if (pSeqList->iSize == 0)
    {
        printf("该顺序表为空,不能再进行删除操作\n");
        return;
    }
    for (; iBegin < pSeqList->iSize - 1; iBegin++)
    {
        pSeqList->arry[iBegin] = pSeqList->arry[iBegin + 1];
    }
    pSeqList->iSize--;
}
// 销毁动态顺序表
void DestroyList(PSeqList pSeqList)
{
    assert(pSeqList);
    free(pSeqList->arry);
    pSeqList->arry = NULL;
    pSeqList->iCapacity = EMPTY_SIZE;
    pSeqList->iSize = EMPTY_SIZE;
}
// 改变顺序表中元素的个数,Size为元素的个数
void ReSize(PSeqList pSeqList, int Size)
{
    assert(pSeqList);
    if (Size < pSeqList->iSize)
    {
        //新分配元素个数小于顺序表长度,为缩小,顺序表长度,容量均改变
        pSeqList->arry = (DataType*)realloc(pSeqList->arry, Size*sizeof(DataType));
        pSeqList->iSize = Size;
        pSeqList->iCapacity = Size;
    }
    else
    {
        //如果新分配元素个数大于顺序表长度,顺序表长度不变,容量改变
        pSeqList->arry = (DataType*)realloc(pSeqList->arry, Size*sizeof(DataType));
        pSeqList->iCapacity = Size;
    }
}


// 顺序表中查找数据data,返回该元素在顺序表中的位置
int Find(SqListD* pSqListD, DataType data)
{
    int iBegin = 0;
    int iPos = 0;
    assert(pSqListD);
    if (pSqListD->iSize == 0)
    {
        printf("该顺序表为空\n");
        return -1;
    }
    for (; iBegin < pSqListD->iSize; iBegin++)
    {
        if (pSqListD->arry[iBegin] == data)
        {
            iPos = iBegin + 1;
            return iPos;
        }
    }
    if (!iPos)
    {
        printf("没有找到%d元素\n", data);
        return 0;
    }
}

// 在顺序表的ipos位置上插入元素data
void Insert(SqListD* pSqListD, size_t pos, DataType data)
{
    int iBegin = 0;
    assert(pSqListD);
    _CheckCapacity(pSqListD);
    for (iBegin = pSqListD->iSize ; iBegin > pos - 1; iBegin--)
    {
        pSqListD->arry[iBegin] = pSqListD->arry[iBegin - 1];
    }
    pSqListD->arry[pos - 1] = data;
    pSqListD->iSize++;
}

// 删除顺序表pos位置上的元素
void Erase(SqListD* pSqListD, size_t pos)
{
    int iBegin = 0;
    assert(pSqListD);
    if (pSqListD->iSize == 0)
    {
        printf("该顺序表为空,不能进行删除操作\n");
        return;
    }
    for (iBegin = pos - 1; iBegin < pSqListD->iSize - 1; iBegin++)
    {
        pSqListD->arry[iBegin] = pSqListD->arry[iBegin + 1];
    }
    pSqListD->iSize--;
}
// 移除顺序表中的元素data
void Remove(SqListD* pSqListD, DataType data)
{
    int iBegin = 0;
    assert(pSqListD);
    if (pSqListD->iSize == 0)
    {
        printf("该顺序表为空,不能进行移除操作\n");
        return;
    }
    for (; iBegin < pSqListD->iSize; iBegin++)
    {
        if (pSqListD->arry[iBegin] == data)
        {
            Erase(pSqListD, iBegin + 1);
        }
    }
}

// 移除顺序表中的所有值为data的元素
void RemoveAll(SqListD* pSqListD, DataType data)
{
    int iBegin = 0;
    int iCount = 0;
    assert(pSqListD);
    for (; iBegin < pSqListD->iSize; iBegin++)
    {
        if (pSqListD->arry[iBegin] == data)
            iCount++;
        else
        {
            pSqListD->arry[iBegin - iCount] = pSqListD->arry[iBegin];
        }
    }
    pSqListD->iSize -= iCount;
}

// 打印顺序表
void PrintSqListD(SqListD* pSqListD)
{
    int iBegin = 0;
    assert(pSqListD);
    if (pSqListD->iSize == 0)
    {
        printf("该顺序表为空\n");
        return;
    }
    for (; iBegin < pSqListD->iSize; iBegin++)
    {
        printf("%d\t", pSqListD->arry[iBegin]);
    }
    printf("\n");
}

你可能感兴趣的:(struct)