头文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define LIST_INIT_SIZE 10 //初始化顺序表的长度
typedef int DataType;
typedef struct SeqListD
{
DataType* _elem; //数据块指针
int _size; //当前数据的有效个数
int _capicity; //容量
}SeqLD,*PSD;
//动态顺序表功能实现
void SeqListDInit(PSD psd); //顺序表初始化
void SeqListDPushBack(PSD psd, DataType data); //尾插
void SeqListDPopBack(PSD psd); //尾删
void SeqListDInsert(PSD ps, int pos, DataType data); //任意位置插入一个元素
void SeqListDErase(PSD ps, int pos); //任意位置删除一个元素
void SeqListDAdd(PSD psd); //动态增容
void SeqListDClear(PSD psd); //清空当前顺序表
void SeqListDDestroy(PSD psd); //清除顺序表
int SeqListDEmpty(PSD psd); //对顺序表判空
void SeqListDSize(PSD psd); //获取顺序表的元素个数
void SeqListDCapicity(PSD psd);//获取当前顺序表的容量
void SeqListPrint(PSD psd); //打印顺序表当前的内容
函数功能文件
#include"Apr_10_List.h"
//顺序表初始化
void SeqListDInit(PSD psd)
{
psd->_elem = (DataType*)malloc(LIST_INIT_SIZE * sizeof(DataType));
if (NULL == psd->_elem)
{
printf("内存申请失败\n");
return;
}
psd->_size = 0;
psd->_capicity = LIST_INIT_SIZE;
}
//顺序表尾插
void SeqListDPushBack(PSD psd, DataType data)
{
if (NULL == psd)
return;
if (psd->_size == psd->_capicity)
SeqListDAdd(psd);
*(psd->_elem + psd->_size) = data;
++psd->_size;
}
//顺序表尾删
void SeqListDPopBack(PSD psd)
{
if (NULL == psd)
return;
if (0 == psd->_size)
{
printf("顺序表已空\n");
return;
}
--psd->_size;
}
//任意位置插入元素
void SeqListDInsert(PSD psd, int pos, DataType data)
{
if (NULL == psd)
{
return;
}
if (pos < 0||pos>psd->_size)
{
printf("插入非法\n");
return;
}
if (psd->_size == psd->_capicity)
SeqListDAdd(psd);
int i = psd->_size;
for (; i > pos; i--)
{
*(psd->_elem+ i) = *(psd->_elem+i-1);
}
*(psd->_elem + pos) = data;
++psd->_size;
}
//任意位置删除元素
void SeqListDErase(PSD psd, int pos)
{
if (NULL == psd)
{
return;
}
if (pos<0 || pos>psd->_size || 0==psd->_size)
{
printf("删除失败\n");
return;
}
int i = pos;
for (; i < psd->_size - 1; i++)
{
*(psd->_elem + i) = *(psd->_elem + i + 1);
}
--psd->_size;
}
//顺序表动态增容
void SeqListDAdd(PSD psd)
{
if (NULL == psd)
return;
int* newbase = (DataType*)realloc(psd->_elem,psd->_capicity * 2 * sizeof(DataType));
if (NULL == newbase)
{
printf("增容失败\n");
return;
}
psd->_capicity = psd->_capicity * 2;
psd->_elem = newbase;
}
//清空顺序表
void SeqListDClear(PSD psd)
{
if (NULL == psd)
{
return;
}
psd->_size = 0;
}
int SeqListDEmpty(PSD psd)
{
if (NULL == psd)
{
return -1;
}
if (0 == psd->_size)
{
printf("已初始化,且顺序表为空\n");
return 1;
}
else
{
return -1;
}
}
//清除顺序表
void SeqListDDestroy(PSD psd)
{
if (NULL == psd)
{
return;
}
if (NULL == psd->_elem)
{
printf("顺序表不存在\n");
return;
}
psd->_size = 0;
free(psd->_elem);
psd->_capicity = 0;
psd->_elem = NULL;
}
//获取当前顺序表的Size
void SeqListDSize(PSD psd)
{
if (NULL == psd)
{
return;
}
printf("Size:%d\n", psd->_size);
}
//获取当前顺序表的容量
void SeqListDCapicity(PSD psd)
{
if (NULL == psd)
{
return;
}
printf("Capicity:%d\n", psd->_capicity);
}
//打印顺序表当前的内容
void SeqListPrint(PSD psd)
{
if (NULL == psd)
{
return;
}
int i = 0;
for (; i < psd->_size; i++)
{
printf("%d ", *(psd->_elem + i));
}
printf("\n");
}
主函数及测试文件
#define _CRT_SECURE_NO_WARNINGS 1
#include"Apr_10_List.h"
void test()
{
SeqLD s;
SeqListDInit(&s);
SeqListDPushBack(&s, 1);
SeqListDPushBack(&s, 3);
SeqListDPushBack(&s, 5);
SeqListDPushBack(&s, 4);
SeqListDPushBack(&s, 2);
SeqListPrint(&s);
SeqListDInsert(&s, 3, 16);
SeqListDClear(&s);
SeqListDEmpty(&s);
SeqListPrint(&s);
SeqListDInsert(&s, -1, 16);
SeqListDPushBack(&s, 6);
SeqListDPushBack(&s, 7);
SeqListDPushBack(&s, 9);
SeqListDPushBack(&s, 8);
SeqListDPushBack(&s, 11);
SeqListDPushBack(&s, 123);
SeqListPrint(&s);
SeqListDPopBack(&s);
SeqListPrint(&s);
SeqListDErase(&s, 6);
SeqListPrint(&s);
SeqListDSize(&s);
SeqListDCapicity(&s);
SeqListDDestroy(&s);
}
int main()
{
test();
system("pause");
return 0;
}