您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
头文件如下
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
// 顺序表的动态存储
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int SLDataType;
typedef struct SeqList {
SLDataType* array; // 指向动态开辟的数组
size_t size; // 有效数据个数
size_t capacity; // 容量空间的大小
}SeqList;
// 基本增删查改接口
void SeqListInit(SeqList* psl, size_t capacity);
void SeqListDestory(SeqList* psl);
void CheckCapacity(SeqList* psl);
void SeqListPushBack(SeqList* psl, SLDataType x);
void SeqListPopBack(SeqList* psl);
void SeqListPushFront(SeqList* psl, SLDataType x);
void SeqListPopFront(SeqList* psl);
int SeqListFind(SeqList* psl, SLDataType x);
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x);
void SeqListErase(SeqList* psl, size_t pos);
void SeqListRemove(SeqList* psl, SLDataType x);
void SeqListModify(SeqList* psl, size_t pos, SLDataType x);
void SeqListPrint(SeqList* psl);
void SeqListBubbleSort(SeqList* psl);
int SeqListBinaryFind(SeqList* psl, SLDataType x);
void SeqListRemoveAll(SeqList* psl, SLDataType x);
#endif /*_SEQLIST_H_*/
以下是具体功能实现
void SeqListInit(SeqList* psl, size_t capacity)//初始化
{
psl->array = (SLDataType*)calloc(capacity, sizeof(SLDataType));
psl->capacity = capacity;
psl->size = 0;
}
void SeqListDestory(SeqList* psl)
{
if (psl->array)
{
free(psl->array);
psl->array = NULL;
psl->capacity = 0;
psl->size = 0;
}
}
void CheckCapacity(SeqList* psl)//检查和动态开辟
{
if (psl->size >= psl->capacity)
{
psl->capacity *= 2;
psl->array = (SLDataType*)realloc(psl->array, sizeof(SLDataType)*psl->capacity);
}
}
void SeqListPushBack(SeqList* psl, SLDataType x)//尾插
{
CheckCapacity(psl);
psl->array[psl->size] = x;
psl->size++;
}
void SeqListPopBack(SeqList* psl)//尾删
{
psl->size--;
}
void SeqListPushFront(SeqList* psl, SLDataType x)//首插
{
int i;
CheckCapacity(psl);
for (i = psl->size - 1; i >= 0; i--)
{
psl->array[i+1] = psl->array[i];
}
psl->array[0] = x;
psl->size++;
}
void SeqListPopFront(SeqList* psl)//首删
{
size_t i;
for (i = 0; i < psl->size-1; i++)
{
psl->array[i] = psl->array[i + 1];
}
psl->size--;
}
int SeqListFind(SeqList* psl, SLDataType x)//查询
{
size_t i;
for (i = 0; i < psl->size - 1; i++)
{
if (psl->array[i] == x)
{
return i;
}
}
return -1;
}
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x)//插入
{
size_t i;
for (i=psl->size-1; i>=pos; i--)
{
psl->array[i+1] = psl->array[i];
}
psl->array[pos] = x;
psl->size++;
}
void SeqListErase(SeqList* psl, size_t pos)//删除指定下标
{
size_t i;
for (i = pos; i < psl->size - 1; i++)
{
psl->array[i] = psl->array[i + 1];
}
psl->size--;
}
void SeqListRemove(SeqList* psl, SLDataType x)//筛选删除
{
SeqListErase(psl, SeqListFind(psl, x));
}
void SeqListModify(SeqList* psl, size_t pos, SLDataType x)
{
psl->array[pos] = x;
}
void SeqListPrint(SeqList* psl)
{
size_t i;
for (i = 0; i <=psl->size - 1; i++)
{
if (psl->array == NULL)
{
printf("NULL");
return;
}
printf("%d->", psl->array[i]);
}
putchar('\n');
}
void SeqListBubbleSort(SeqList* psl)
{
size_t i, j;
SLDataType tmp;
for (i = 0; i < psl->size - 1; i++)
{
for (j = 0; j < psl->size - 1; j++)
{
if (psl->array[j] > psl->array[j + 1])
{
tmp = psl->array[j];
psl->array[j] = psl->array[j + 1];
psl->array[j + 1] = tmp;
}
}
}
}
int SeqListBinaryFind(SeqList* psl, SLDataType x)
{
int left = 0, right = psl->size;
int mid = (left + right) / 2;
while (left <= right)
{
if (x < psl->array[mid])
{
right = mid-1;
}
if (x>psl->array[mid])
{
left = mid+1;
}
else
{
return mid;
}
}
return -1;
}
void SeqListRemoveAll(SeqList* psl, SLDataType x)
{
size_t i, gap = 0;
for (i = 0; i < psl->size; i++)
{
if (psl->array[i] == x)
{
gap++;
}
else
{
psl->array[i - gap] = psl->array[i];
}
}
psl->size -= gap;
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。