C语言实现动态顺序表
来源:互联网 发布:java线程状态 编辑:程序博客网 时间:2024/06/05 12:04
功能实现,主要实现动态顺序表的增、删、查、改、找。
静态顺序表是存储在一个连续的数组中的,因此对其数据类型有一个结构体声明。
typedef int DataType;typedef struct SeqList{ DataType* _a; size_t _size; // 有效数据个数 size_t _capacity; // 容量 }SeqList;
- 初始化
//初始化void SeqInit(SeqList* pSeq){ assert(pSeq); pSeq->_a = NULL; pSeq->_size = 0; pSeq->_capacity = 0;}
- 注销
//注销void SeqDestory(SeqList* pSeq){ assert(pSeq); if (pSeq->_a) { free(pSeq->_a); }}
- 增容(内存开辟)
const void CheckCapacity(SeqList *pSeq){ //增容 assert(pSeq); if (pSeq->_size >= pSeq->_capacity) { pSeq->_capacity = 2 * pSeq->_capacity + 3; pSeq->_a = (DataType*)realloc(pSeq->_a, pSeq->_capacity * sizeof(DataType)); }}
- 尾插、尾删
//尾插void SeqPushBack(SeqList* pSeq, DataType x){ assert(pSeq); CheckCapacity(pSeq); pSeq->_a[pSeq->_size] = x; pSeq->_size++;}//尾删 void SeqPopBack(SeqList* pSeq){ assert(pSeq); assert(pSeq->_a); pSeq->_size--;}
测试截图
- 头插、头删
//头插void SeqPushFront(SeqList* pSeq, DataType x){ int end; assert(pSeq); CheckCapacity(pSeq); end = pSeq->_size; while (end > 0) { pSeq->_a[end] = pSeq->_a[end - 1]; end--; } pSeq->_a[0] = x; pSeq->_size++;}//头删void SeqPopFront(SeqList* pSeq){ assert(pSeq); int begin = 0; if (pSeq->_size <= 0) { printf("空数组\n"); return; } while (begin < pSeq->_size-1) { pSeq->_a[begin] = pSeq->_a[begin + 1]; begin++; } pSeq->_size--;}
测试截图
- 任意位置的插入、删除
//任意位置插入void SeqInsert(SeqList* pSeq, size_t pos, DataType x){ int end; assert(pSeq); if (pos > pSeq->_size) { printf("pos超出范围\n"); return; } CheckCapacity(pSeq); end = pSeq->_size; while (end > pos) { pSeq->_a[end] = pSeq->_a[end - 1]; end--; } pSeq->_a[pos] = x; pSeq->_size++;}//任意位置删除void SeqErase(SeqList* pSeq, size_t pos){ int begin; assert(pSeq); if (pos > pSeq->_size) { printf("pos超出范围\n"); return; } begin = pos; while (begin < pSeq->_size) { pSeq->_a[begin] = pSeq->_a[begin + 1]; begin++; } pSeq->_size--;}
测试截图
- 查找、修改
//查找int SeqFind(SeqList* pSeq, DataType x){ assert(pSeq); int i = 0; while (i < pSeq->_size) { if (pSeq->_a[i] == x) return i; i++; } return -1;}//修改void SeqAt(SeqList* pSeq, size_t pos, DataType x){ assert(pSeq); if (pos >= pSeq->_size) { printf("pos超出范围\n"); return; } int i = 0; pSeq->_a[pos] = x;}
- 排序
//从小到大排序//使用冒泡法排序void BubbleSort(SeqList* pSeq){ assert(pSeq); if (pSeq->_a == NULL) { return; } for (int i = 0; i < pSeq->_size; i++) { for (int j = 0; j < pSeq->_size-i-1; j++) { if (pSeq->_a[j]>pSeq->_a[j + 1]) { DataType temp = pSeq->_a[j]; pSeq->_a[j] = pSeq->_a[j + 1]; pSeq->_a[j + 1] = temp; } } }}//数据交换const void Swap(DataType *d1, DataType *d2){ DataType temp = *d1; *d1 = *d2; *d2 = temp;}//从大到小排序//使用二元选择排序方式void SelectSort(SeqList* pSeq){ size_t end; size_t begin; assert(pSeq); begin = 0; end = pSeq->_size - 1; while (begin < end) { DataType min = begin; DataType max = begin; DataType i = begin; while (i <= end) { if (pSeq->_a[i] < pSeq->_a[min]) { min = i; } if (pSeq->_a[i] > pSeq->_a[max]) { max = i; } i++; } Swap(&pSeq->_a[begin], &pSeq->_a[max]); if (min == begin) min = max; Swap(&pSeq->_a[end], &pSeq->_a[min]); begin++; end--; }}
测试截图
- 二分法查找(递归)
const int _BinarySearch(DataType* a, size_t begin, size_t end, DataType x){ size_t mid = begin + ((end - begin) >> 1); while (begin <= end) { if (x>a[mid]) { return _BinarySearch(a, mid + 1, end, x); } else if (x < a[mid]) { return _BinarySearch(a, begin, mid - 1, x); } else { return mid; } } return -1;}//二分法查找int BinarySearch(SeqList* pSeq,DataType x){ assert(pSeq); return _BinarySearch(pSeq->_a, 0, pSeq->_size - 1, x);}
测试截图
阅读全文
0 0
- C语言实现动态顺序表
- c语言实现动态顺序表
- 利用C语言实现动态顺序表
- C语言实现动态顺序表
- c语言顺序表动态实现
- 动态顺序表----C语言实现
- C语言实现动态顺序表
- C语言动态顺序表的实现
- C语言实现动态顺序表
- C语言实现动态顺序表
- 动态顺序表(C语言实现)
- C语言实现动态顺序表
- c语言动态顺序表的实现
- C语言简单实现动态顺序表
- 【C语言】动态顺序表的实现
- 动态顺序表-c语言实现
- 【C语言】实现动态顺序表
- C语言实现动态顺序表
- Arduino语音天气预报(一)
- Windowing Functions In Hive
- Spark学习笔记1-RDD编程
- 信数金服决策引擎分享(三):如何实现决策的协同维护?
- 移动web(三)touch事件详解
- C语言实现动态顺序表
- 两个人聪明人的空城——《司马懿之虎啸龙吟》
- 学习笔记—bootstrap(2)
- HTTP方法
- Long类型转String JS丢失
- netty获取ip
- Maven学习-目录结构
- MD5加密
- LUOGUP1631---序列合并