以下是实现代码,代码仅供参考。
#include<malloc.h>
#include<assert.h>
typedef int DataType;
typedef struct SeqList
{
DataType* _a;
size_t _size; // 有效数据个数
size_t _capacity; // 容量
}SeqList;
void SeqInit(SeqList* pSeq);//初始化表
void Checkcapacity(SeqList* pSeq);//检查容量/增容
void SeqPushBack(SeqList* pSeq, DataType x);//尾插
bool SeqPopBack(SeqList* pSeq);//尾删
void SeqPushFront(SeqList* pSeq, DataType x);//头插
void SeqPopFront(SeqList* pSeq);//头删
bool SeqInsert(SeqList* pSeq, size_t pos, DataType x);//节点前插入
bool SeqErase(SeqList* pSeq, size_t pos);//删除节点
bool SeqAt(SeqList* pSeq, size_t pos, DataType x);//修改
void SeqDestory(SeqList* pSeq);//销毁表
int SeqFind(SeqList* pSeq, DataType x);//查找
int BinarySearch(SeqList* pSeq, DataType x); //二分查找节点
void Swap(int* a, int *b);//交换
void BubbleSort(SeqList* pSeq);//冒泡排序
void SelectSort(SeqList* pSeq);// 选择排序
void Mergelist(SeqList* pSeq1, SeqList* pSeq2, SeqList* pSeq);//合并两个非递减有序表
void SeqInit(SeqList* pSeq)//初始化表
{
pSeq->_capacity = 10;
pSeq->_a = (DataType*)malloc(sizeof(DataType)*pSeq->_capacity);
assert(pSeq->_a);
pSeq->_size = 0;
}
void Checkcapacity(SeqList* pSeq)//检查容量/增容
{
assert(pSeq->_a);
if (pSeq->_size >= pSeq->_capacity){
pSeq->_a = (DataType*)realloc(pSeq->_a, sizeof(DataType)*(pSeq->_capacity * 2));
assert(pSeq->_a);
pSeq->_capacity *= 2;
}
}
void SeqPushBack(SeqList* pSeq, DataType x)//尾插
{
assert(pSeq->_a);
Checkcapacity(pSeq);
pSeq->_a[pSeq->_size] = x;
++pSeq->_size;
}
bool SeqPopBack(SeqList* pSeq)//尾删
{
assert(pSeq->_a);
if (pSeq->_size == 0){
return false;
}
else{
pSeq->_a[pSeq->_size - 1] = 0;
pSeq->_size--;
return true;
}
}
void SeqPushFront(SeqList* pSeq, DataType x)//头插
{
assert(pSeq->_a);
Checkcapacity(pSeq);
for (size_t index = pSeq->_size; index > 0; --index) {
pSeq->_a[index] = pSeq->_a[index - 1];
}
pSeq->_a[0] = x;
++pSeq->_size;
}
void SeqPopFront(SeqList* pSeq)//头删
{
assert(pSeq->_a);
Checkcapacity(pSeq);
for (size_t index = 0; index < pSeq->_size - 1; ++index){
pSeq->_a[index] = pSeq->_a[index + 1];
}
--pSeq->_size;
}
bool SeqInsert(SeqList* pSeq, size_t pos, DataType x)//节点前插入
{
assert(pSeq->_a);
Checkcapacity(pSeq);
if (pos > pSeq->_size){
return false;
}
else{
size_t index = pSeq->_size;
for (size_t count = 0; count < pSeq->_size - pos; ++count){
pSeq->_a[index] = pSeq->_a[index - 1];
--index;
}
pSeq->_a[pos] = x;
++pSeq->_size;
return true;
}
}
bool SeqErase(SeqList* pSeq, size_t pos)//删除节点
{
assert(pSeq->_a);
if (pSeq->_size == 0 || pos >= pSeq->_size){
return false;
}
else{
for (size_t index = pos; index < pSeq->_size - 1; ++index){
pSeq->_a[index] = pSeq->_a[index + 1];
}
--pSeq->_size;
return true;
}
}
bool SeqAt(SeqList* pSeq, size_t pos, DataType x)//修改节点
{
assert(pSeq->_a);
if (pSeq->_size == 0 || pos >= pSeq->_size){
return false;
}
else {
pSeq->_a[pos] = x;
return true;
}
}
void SeqDestory(SeqList* pSeq)//销毁表
{
assert(pSeq->_a);
free(pSeq->_a);
pSeq->_size = 0;
pSeq->_capacity = 0;
}
int SeqFind(SeqList* pSeq, DataType x)//查找节点
{
assert(pSeq->_a);
if (pSeq->_size == 0){
return -1;
}
else{
for (size_t index = 0; index < pSeq->_size; index++){
if (x == pSeq->_a[index]){
return index;
}
}
return -1;
}
}
int BinarySearch(SeqList* pSeq, DataType x)//二分查找节点
{
assert(pSeq->_a);
int left = 0;
int right = pSeq->_size - 1;
while (left <= right){
int mid = left + ((right - left) >> 1);
if (pSeq->_a[mid] > x){
right = mid - 1;
}
else if (pSeq->_a[mid] < x){
left = mid + 1;
}
else{
return mid;
}
}
return -1;
}
void Swap(DataType*a, DataType* b)//交换
{
DataType tmp = *a;
*a = *b;
*b = tmp;
}
void BubbleSort(SeqList* pSeq)//冒泡排序
{
assert(pSeq->_a);
for (size_t end = pSeq->_size - 1; end > 0; --end){
for (size_t begin = 0; begin < end; ++begin){
if (pSeq->_a[begin] > pSeq->_a[begin + 1]){
Swap(&pSeq->_a[begin], &pSeq->_a[begin + 1]);
}
}
}
}
void SelectSort(SeqList* pSeq)//选择排序
{
assert(pSeq->_a);
for (size_t begain = 0; begain < pSeq->_size - 1; ++begain){
for (size_t index = begain + 1; index < pSeq->_size; ++index){
if (pSeq->_a[begain] > pSeq->_a[index]){
Swap(&pSeq->_a[begain], &pSeq->_a[index]);
}
}
}
}
void Mergelist(SeqList* pSeq1, SeqList* pSeq2, SeqList* pSeq)//合并两个非递减有序表
{
size_t i, j;
i = j = 0;
while (i < pSeq1->_size && j < pSeq2->_size){
if (pSeq1->_a[i] <= pSeq2->_a[j]){
SeqPushBack(pSeq, pSeq1->_a[i]);
i++;
}
else{
SeqPushBack(pSeq, pSeq2->_a[j]);
j++;
}
}
while (i <= pSeq1->_size - 1){
SeqPushBack(pSeq, pSeq1->_a[i]);
i++;
}
while (j <= pSeq2->_size - 1){
SeqPushBack(pSeq, pSeq2->_a[j]);
j++;
}
}