线性表----->顺序表----->基本操作---->增、删
上图引自http://www.52c3.com/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>
#include <string.h>
//定义结构体
struct Dongtaishuzu
{
int* pShuzutou; //首地址
unsigned int iRong; //容量
unsigned int iShuliang; //已存储数量
};
//初始化动态数组
void Chushihua(struct Dongtaishuzu* pstshuzu); //首地址
//尾巴上添加一个节点
void ZengJia(struct Dongtaishuzu* pstshuzu, int ishuju); //增加元素
//中间增加一个节点
void ZenJiaZhong(struct Dongtaishuzu* pstshuzu, int ishuju, unsigned int iXiabiao); //首地址,插入元素,插入地址
//输出函数
void print(struct Dongtaishuzu* pstshuzu);
void ZenJiaKongJian(struct Dongtaishuzu* pstshuzu);
//删除尾部元素
void ShanChuWei(struct Dongtaishuzu* pstshuzu);
//删除中间的元素 将后面的向前挪动一个
void ShanChuZhongJian(struct Dongtaishuzu* pstshuzu,unsigned int iWeiZhi);
//全删除:重置
void ShanChuAll(struct Dongtaishuzu* pstshuzu);
int main(void)
{
struct Dongtaishuzu stDongshuzu; //动态数组命名
Chushihua(&stDongshuzu); //初始化动态数组
for (int i = 1; i <= 25; i++)
{
ZengJia(&stDongshuzu, i);
}
print(&stDongshuzu);
printf("\n");
ZenJiaZhong(&stDongshuzu, 999, 8);
print(&stDongshuzu);
printf("\n");
ShanChuWei(&stDongshuzu);
print(&stDongshuzu);
printf("\n");
ShanChuZhongJian(&stDongshuzu, 5);
print(&stDongshuzu);
printf("\n");
ShanChuAll(&stDongshuzu);
print(&stDongshuzu);
printf("\n");
free(stDongshuzu.pShuzutou);
return 0;
}
//初始化动态数组
void Chushihua(struct Dongtaishuzu* pstshuzu) //首地址
{
pstshuzu->iRong = 5; //可存储的数据总量
pstshuzu->pShuzutou = (int*)malloc(sizeof(int) * (pstshuzu->iRong));
pstshuzu->iShuliang = 0; //已存储的数据总量
}
//尾巴上添加一个节点
void ZengJia(struct Dongtaishuzu* pstshuzu, int ishuju) //增加元素
{
//检测参数合法性
if (NULL == pstshuzu)
{
printf("参数错误\n");
return;
}
ZenJiaKongJian(pstshuzu);
//数据装入其中
pstshuzu->pShuzutou[pstshuzu->iShuliang] = ishuju;
//已存储数量++
pstshuzu->iShuliang++;
}
//中间增加一个节点
void ZenJiaZhong(struct Dongtaishuzu* pstshuzu, int ishuju, unsigned int iXiabiao) //首地址,插入元素,插入地址
{
//检测参数合法性
if (NULL == pstshuzu)
{
printf("动态数组参数错误\n");
return;
}
ZenJiaKongJian(pstshuzu);
//下标过大
if (iXiabiao > pstshuzu->iShuliang)
{
iXiabiao = pstshuzu->iShuliang;
}
//向后挪
for (unsigned int i = pstshuzu->iShuliang; i >= iXiabiao; i--)
{
pstshuzu->pShuzutou[i] = pstshuzu->pShuzutou[i - 1];
}
//把数据赋值到指定位置(插入数据)
pstshuzu->pShuzutou[iXiabiao] = ishuju;
//动态元素数据增加1;
pstshuzu->iShuliang++;
}
//输出函数
void print(struct Dongtaishuzu* pstshuzu)
{
//检测参数合法性
if (NULL == pstshuzu)
{
printf("参数错误\n");
return;
}
printf("容量 :%u 已存储数量 :%u\n", pstshuzu->iRong, pstshuzu->iShuliang);
printf("数据\n");
for (unsigned int i = 0; i < pstshuzu->iShuliang; i++)
{
printf("%d ", pstshuzu->pShuzutou[i]);
}
printf("\n");
}
void ZenJiaKongJian(struct Dongtaishuzu* pstshuzu)
{
if (NULL == pstshuzu)
{
printf("动态数组参数错误\n");
return;
}
if (pstshuzu->iRong == pstshuzu->iShuliang) //当容量与数量相等,即装满
{
//容量变大
pstshuzu->iRong += 10;
//申请新空间
int* pTemp = (int*)malloc(sizeof(int) * (pstshuzu->iRong));
memset(pTemp, 0, sizeof(int) * pstshuzu->iRong);
//将原数据赋值进新空间
for (unsigned int i = 0; i < pstshuzu->iShuliang; i++)
{
pTemp[i] = pstshuzu->pShuzutou[i];
}
//释放原空间
free(pstshuzu->pShuzutou);
//动态数组头指向新空间
pstshuzu->pShuzutou = pTemp;
}
}
//删除尾部元素
void ShanChuWei(struct Dongtaishuzu* pstshuzu)
{
//检测参数合法性
if (NULL == pstshuzu)
{
printf("参数错误\n");
return;
}
pstshuzu->iShuliang--;
}
//全删除:重置0
void ShanChuAll(struct Dongtaishuzu* pstshuzu)
{
if (NULL == pstshuzu)
{
printf("参数错误\n");
return;
}
pstshuzu->iShuliang = 0;
}
//删除中间的元素 将后面的向前挪动一个
void ShanChuZhongJian(struct Dongtaishuzu* pstshuzu, unsigned int iWeiZhi)
{
if (NULL == pstshuzu)
{
printf("参数错误\n");
return;
}
//下标过大
if (iWeiZhi >= pstshuzu->iShuliang)
{
printf("数据不存在\n");
}
//后面的向前挪
for (unsigned int i = iWeiZhi-1; i < pstshuzu->iShuliang-1; i++)
{
pstshuzu->pShuzutou[i] = pstshuzu->pShuzutou[i+1];
}
//动态元素数据减1;
pstshuzu->iShuliang--;
}