之前,我们已经实现了顺序表的静态实现方式,而动态的顺序表与静态顺序表的区别就是“容量问题”,我们再也不用担心自己给的容量不够的问题,只需要调用检查容量函数就行,当顺序表的容量不够时,会自动扩容。
1.定义结构体
#pragma once//避免头文件的重复引入,作用于预编译指令的作用几乎相同
//动态顺序表
typedef struct SeqList
{
DataType* _a;
size_t _size;
size_t _capacity;
}SeqList;
2.实现的接口
void SeqListInit(SeqList* pSeq);//初始化
void CheckCapaticy(SeqList* pSeq);//扩容
void SeqListDestory(SeqList* pSeq);//销毁
void SeqListPrint(SeqList* pSeq);//打印
void SeqListPushBack(SeqList* pSeq, DataType x);//尾插
void SeqListPushFront(SeqList* pSeq, DataType x);//头插
void SeqListPopFront(SeqList* pSeq);//头删
void SeqListPopBack(SeqList* pSeq);//尾删
void SeqListInsert(SeqList* pSeq, size_t pos, DataType x);//任意位置pos的插入
void SeqListErase(SeqList* pSeq, size_t pos);//任意位置的删除
int SeqListFind(SeqList* pSeq, DataType x);//查找
void SeqListModify(SeqList* pSeq, size_t pos, DataType x);//修改pos位置的元素
void SeqListRemove(SeqList* pSeq, DataType x);//删除x FIND+ERASE
void SeqListRemoveAll(SeqList* pSeq, DataType x);//将所有的x删掉
void SeqListBubbleSort(SeqList* pSeq);//冒泡排序
void SeqListSelectSort(SeqList* pSeq);//选择排序
void SeqListBinarySearch(SeqList* pSeq, DataType x);//二分查找
void Swap(DataType* l, DataType* r);//交换
3.具体实现
//动态初始化
void SeqListInit(SeqList* pSeq, size_t capacity)
{
assert(pSeq&&capacity > 0);
pSeq->_a = (DataType*)malloc(capacity*sizeof(DataType));
assert(pSeq->_a);
pSeq->_size = 0;
pSeq->_capacity = capacity;
}
void CheckCapaticy(SeqList* pSeq)
{
if (pSeq->_size = pSeq->_capacity)
{
DataType* a = (DataType*)realloc(pSeq->_a, pSeq->_capacity * 2);
assert(a);
pSeq->_a;
pSeq->_capacity *= 2;
}
}
void SeqListPrint(SeqList* pSeq)
{
assert(pSeq);
size_t index = 0;
for (index; index < pSeq->_size; index++)
{
printf("%d ", pSeq->_a[index]);
}
printf("\n");
}
void SeqListDestory(SeqList* pSeq)
{
assert(pSeq);
free(pSeq->_a);
pSeq->_a = NULL;
pSeq->_size = pSeq->_capacity = 0;
}
void SeqListPushBack(SeqList* pSeq, DataType x)
{
assert(pSeq);
CheckCapacity();
pSeq->_a[pSeq->_size] = x;
pSeq->_size++;
}
void SeqListPushFront(SeqList* pSeq, DataType x)
{
assert(pSeq);
CheckCapacity();
size_t index = pSeq->_size;
for (index; index > 0; index--)
{
pSeq->_a[index] = pSeq->_a[index - 1];
}
pSeq->_a[index] = x;
pSeq->_size++;
}
void SeqListPopFront(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->_size == 0)
{
printf("顺序表里面没有数据元素!\n");
}
size_t index = 0;
for (index; index < pSeq->_size - 1; index++)
{
pSeq->_a[index] = pSeq->_a[index + 1];
}
pSeq->_size--;
}
void SeqListPopBack(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->_size == 0)
{
printf("顺序表里面没有数据元素!\n");
}
else
pSeq->_size--;
}
void SeqListInsert(SeqList* pSeq, size_t pos, DataType x)
{
assert(pSeq&&pos);
size_t index = pSeq->_size;
if (pos > pSeq->_size)
{
printf("插入位置不合法!\n");
}
CheckCapacity();
for (index; index >= pos; index--)
{
pSeq->_a[index] = pSeq->_a[index - 1];
}
pSeq->_a[index] = x;
pSeq->_size++;
}
void SeqListErase(SeqList* pSeq, size_t pos)
{
assert(pSeq&&pos);
size_t index = pos - 1;
if (pSeq->_size == 0)
{
printf("顺序表中没有数据元素!\n");
return;
}
for (index; index < pSeq->_size - 1; index++)
{
pSeq->_a[index] = pSeq->_a[index + 1];
}
pSeq->_size--;
}
int SeqListFind(SeqList* pSeq, DataType x)
{
assert(pSeq);
size_t index = 0;
for (index; index < pSeq->_size; index++)
{
if (pSeq->_a[index] == x)
{
return index + 1;
}
}
return -1;
}
void SeqListModify(SeqList* pSeq, size_t pos, DataType x)
{
assert(pSeq&&pos);
size_t index = 0;
if (pSeq->_size == 0)
{
printf("顺序表中没有数据元素!\n");
return;
}
for (index; index < pSeq->_size; index++)
{
if (pSeq->_a[index] == pSeq->_a[pos])
{
pSeq->_a[pos] = x;
}
}
}
void SeqListRemove(SeqList* pSeq, DataType x)
{
assert(pSeq);
size_t index = 0;
size_t tmp = 0;
for (index; index < pSeq->_size; index++)
{
if (pSeq->_a[index] == x)
{
for (tmp = index; tmp < pSeq->_size - 1; tmp++)
{
pSeq->_a[index] = pSeq->_a[index + 1];
}
pSeq->_size--;
return 0;
}
}
}
void SeqListRemoveAll(SeqList* pSeq, DataType x)
{
assert(x);
size_t index = 0;
size_t tmp = 0;
for (index; index < pSeq->_size; index++)
{
if (pSeq->_a[index] == x)
{
for (tmp = index; tmp < pSeq->_size - 1; tmp++)
{
pSeq->_a[index] = pSeq->_a[index + 1];
}
pSeq->_size--;
index--;
}
}
}
void SeqListBubbleSort(SeqList* pSeq)//冒泡排序--从小到大
{
assert(pSeq);
size_t index = 0;
size_t num = 0;
for (num; num < pSeq->_size - 1; num++)
{
for (index; index < pSeq->_size - num - 1; index++)
{
if (pSeq->_a[index]>pSeq->_a[index + 1])
{
Swap(&pSeq->_a[index], &pSeq->_a[index + 1]);
}
}
}
}
void SeqListSelectSort(SeqList* pSeq)
{
assert(pSeq);
size_t num = 0;
size_t index = 0;
size_t end = pSeq->_size;
for (index; index < end / 2; index++)
{
size_t min = num;
size_t max = num;
for (index = num + 1; index < end; index++)
{
min = pSeq->_a[min] < pSeq->_a[index] ? min : index;
max = pSeq->_a[max]>pSeq->_a[index] ? max : index;
}
Swap(&pSeq->_a[min], &pSeq->_a[num]);
Swap(&pSeq->_a[max], &pSeq->_a[num]);
}
}
void SeqListBinarySearch(SeqList* pSeq, DataType x)
{
assert(pSeq);
int left = 0;
int right = pSeq->_size - 1;
if (pSeq->_size == 0)
{
return -1;
}
while (left <= right)
{
int mid = left + (right - left) / 2;
if (pSeq->_a[mid] == x)
{
return mid;
}
else if (pSeq->_a[mid] > x)
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
return -1;
}
void Swap(DataType* l, DataType* r)
{
size_t temp = *l;
*l = *r;
*r = temp;
}
4.部分测试用例
SeqList seqlist;
void Text1()
{
SeqListInit(&seqlist,5);
SeqListPushBack(&seqlist, 1);
SeqListPushBack(&seqlist, 2);
SeqListPushBack(&seqlist, 3);
SeqListPushBack(&seqlist, 4);
SeqListPrint(&seqlist);
SeqListPopBack(&seqlist);
}
int main()
{
Text1();
}