动态数组模型分析
#include <stdio.h>
#include <malloc.h>
typedef unsigned int UI;
struct Donshuzu {
int* pShuzuTou;
UI iRonliang;
UI iShuliang;
};
void ChuShiHua(struct Donshuzu*);
void KaipiKJ(struct Donshuzu*);
void Zengjia(struct Donshuzu*, int);
void ShuChu(struct Donshuzu);
void Zengjiazhong(struct Donshuzu*, int, UI);
void ShanChuWei(struct Donshuzu*);
void ShanChuAll(struct Donshuzu*);
void ShiFangSZ(struct Donshuzu*);
void ShanChuZhong(struct Donshuzu*, UI);
void ChuShiHua(struct Donshuzu* pstshuzu)
{
pstshuzu->iRonliang = 5;
pstshuzu->pShuzuTou = (int*)malloc(sizeof(int) * pstshuzu->iRonliang);
pstshuzu->iShuliang = 0;
}
void KaipiKJ(struct Donshuzu* pstshuzu)
{
if (pstshuzu->iRonliang == pstshuzu->iShuliang)
{
pstshuzu->iRonliang += 10;
int* pTemp = (int*)malloc(sizeof(int) * pstshuzu->iRonliang);
for (UI i = 0; i < pstshuzu->iShuliang; i++)
pTemp[i] = pstshuzu->pShuzuTou[i];
free(pstshuzu->pShuzuTou);
pstshuzu->pShuzuTou = pTemp;
}
}
void Zengjia(struct Donshuzu* pstshuzu, int iShujv)
{
KaipiKJ(pstshuzu);
pstshuzu->pShuzuTou[pstshuzu->iShuliang] = iShujv;
pstshuzu->iShuliang++;
}
void ShuChu(struct Donshuzu pstshuzu)
{
if (&pstshuzu == NULL)
{
printf("参数错误\n");
return;
}
printf("容量:%u 已存数量:%u\n", pstshuzu.iRonliang, pstshuzu.iShuliang);
printf("数据:");
for (UI i = 0; i < pstshuzu.iShuliang; i++)
printf("%d ", pstshuzu.pShuzuTou[i]);
printf("\n");
}
void Zengjiazhong(struct Donshuzu* pstshuzu, int iShujv, UI iXiabiao)
{
if (NULL == pstshuzu) {
printf("参数错误\n");
return;
}
KaipiKJ(pstshuzu);
if (iXiabiao > pstshuzu->iShuliang)
iXiabiao = pstshuzu->iShuliang;
for (UI i = pstshuzu->iShuliang; i > iXiabiao; i--)
pstshuzu->pShuzuTou[i] = pstshuzu->pShuzuTou[i - 1];
pstshuzu->pShuzuTou[iXiabiao] = iShujv;
pstshuzu->iShuliang++;
}
void ShanChuWei(struct Donshuzu* pstshuzu)
{
if (NULL == pstshuzu)
return;
pstshuzu->iShuliang--;
}
void ShanChuAll(struct Donshuzu* pstshuzu)
{
if (NULL == pstshuzu)
return;
pstshuzu->iShuliang = 0;
}
void ShiFangSZ(struct Donshuzu* pstshuzu)
{
if (NULL == pstshuzu)
return;
pstshuzu->iShuliang = 0;
pstshuzu->iRonliang = 0;
free(pstshuzu->pShuzuTou);
pstshuzu->pShuzuTou = NULL;
}
void ShanChuZhong(struct Donshuzu* pstshuzu, UI iWeiZhi)
{
if (NULL == pstshuzu)
return;
if (pstshuzu->iShuliang <= iWeiZhi)
{
ShanChuWei(pstshuzu);
return;
}
for (UI i = iWeiZhi - 1; i < pstshuzu->iShuliang - 1; i++)
pstshuzu->pShuzuTou[i] = pstshuzu->pShuzuTou[i + 1];
pstshuzu->iShuliang--;
}
int main()
{
struct Donshuzu stDshuzu;
ChuShiHua(&stDshuzu);
Zengjia(&stDshuzu, 1);
Zengjia(&stDshuzu, 2);
Zengjia(&stDshuzu, 3);
Zengjia(&stDshuzu, 4);
Zengjia(&stDshuzu, 5);
Zengjia(&stDshuzu, 6);
Zengjia(&stDshuzu, 7);
Zengjia(&stDshuzu, 8);
Zengjia(&stDshuzu, 9);
Zengjiazhong(&stDshuzu, 3, 24);
ShuChu(stDshuzu);
ShanChuZhong(&stDshuzu, 3);
ShuChu(stDshuzu);
ShanChuWei(&stDshuzu);
ShanChuWei(&stDshuzu);
ShuChu(stDshuzu);
ShanChuAll(&stDshuzu);
ShuChu(stDshuzu);
ShiFangSZ(&stDshuzu);
ShuChu(stDshuzu);
ChuShiHua(&stDshuzu);
Zengjia(&stDshuzu, 4);
Zengjia(&stDshuzu, 6);
ShuChu(stDshuzu);
free(stDshuzu.pShuzuTou);
return 0;
}