<strong style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">前几天写了一个静态顺序表,但是觉得一开始开辟很大一部分空间却不一定可以完全用得上,会造成很大的内存浪费,于是写了一个可以自动开辟内存空间的动态顺序表作为改进。</strong>
"DynamicSeqllist.h"
#pragma once #define __SEQ_LIST__ #ifdef __SEQ_LIST__ #include <stdio.h> #include <assert.h> #include <string.h> #include <malloc.h> typedef int DataType; #define DEFAULT_CAPACITY 3 typedef struct SeqList { DataType* array; size_t size; // 当前的有效数据个数 size_t capacity; // 容量 }SeqList; typedef enum Tag { TRUE, // 真 FALSE, // 假 }Tag; typedef struct FindRet { Tag isFind; // 是否找到的标示 size_t index; // 找到数据的下标 }FindRet; void InitSeqList(SeqList* pSeq); void PrintSeqList(SeqList* pSeq); void CheckExpandCapacity(SeqList* pSeq); void DestorySeqList(SeqList* pSeq); void PushBack(SeqList* pSeq, DataType x); void PopBack(SeqList* pSeq); void PushFront(SeqList* pSeq, DataType x); void PopFront(SeqList* pSeq); void Insert(SeqList* pSeq, size_t index, DataType x); void Modified(SeqList* pSeq, size_t index, DataType x); void Remove(SeqList* pSeq, size_t index); FindRet Find(SeqList* pSeq, DataType x, size_t index); Tag Erase(SeqList* pSeq, DataType x, Tag all); void BubbleSort(SeqList* pSeq); void SelectSort(SeqList* pSeq); FindRet BinarySearch(SeqList * pSeq, DataType x); #endif // __SEQ_LIST__
"DynamicSeqlist.c"
#include "DynamicSeqlist.h" //对顺序表初始化 void InitSeqList(SeqList* pSeq) { assert(pSeq); pSeq->array = (DataType*)malloc(sizeof(DataType)*DEFAULT_CAPACITY); pSeq->size = 0; pSeq->capacity = DEFAULT_CAPACITY; } //打印顺序表 void PrintSeqList(SeqList* pSeq) { assert(pSeq); int i = 0; for (; i < pSeq->size; i++) { printf("%d ",pSeq->array[i]); } printf("\n"); } //检测内存空间是否足够,若不够则自动分配二倍空间存储数据 void CheckExpandCapacity(SeqList* pSeq) { assert(pSeq); if (pSeq->size == pSeq->capacity) { DataType* tmp = (DataType*)malloc(pSeq->capacity * 2*sizeof(DataType)); memcpy(tmp, pSeq->array, sizeof(DataType)*pSeq->size); free(pSeq->array);//当开辟出新空间存储数据时要将旧空间释放掉 pSeq->array = tmp; pSeq->capacity = pSeq->capacity * 2; } } //释放空间,销毁顺序表 void DestorySeqList(SeqList* pSeq) { if (pSeq) { free(pSeq->array); pSeq->capacity = 0; pSeq->size = 0; } } void PushBack(SeqList* pSeq, DataType x) { assert(pSeq); CheckExpandCapacity(pSeq); pSeq->array[pSeq->size++] = x; } void PopBack(SeqList* pSeq) { assert(pSeq); --pSeq->size; } void PushFront(SeqList* pSeq, DataType x) { assert(pSeq); CheckExpandCapacity(pSeq); int i = pSeq->size; for (; i >0 ; --i) { pSeq->array[i] = pSeq->array[i -1]; } pSeq->array[0] = x; pSeq->size++; } void PopFront(SeqList* pSeq) { assert(pSeq); int i = 0 ; if (pSeq->size == 0) { printf("array is empty!"); } for (; i <pSeq->size; ++i) { pSeq->array[i] = pSeq->array[i + 1]; } pSeq->size--; } void Insert(SeqList* pSeq, size_t index, DataType x) { assert(pSeq); assert(index<pSeq->size); int i = pSeq->size; for (; i>index ; --i) { pSeq->array[i] = pSeq->array[i - 1]; } pSeq->array[index] = x; pSeq->size++; } void Modified(SeqList* pSeq, size_t index, DataType x) { assert(pSeq); assert(index<pSeq->size); pSeq->array[index] = x; } void Remove(SeqList* pSeq, size_t index) { assert(pSeq); assert(index<pSeq->size ); for (; pSeq->size>index; index++) { pSeq->array[index] = pSeq->array[index + 1]; } pSeq->size--; } FindRet Find(SeqList* pSeq, DataType x, size_t index) { assert(pSeq); FindRet ret = { FALSE, NULL }; for (; index < pSeq->size; index++) { if (x == pSeq->array[index]) { Tag isFind = TRUE; ret.isFind = TRUE; ret.index = index; return ret; } } Tag isFind = FALSE; return ret; } void Swap(DataType *left,DataType *right) { DataType tmp = *left; *left = *right; *right = tmp; } //冒泡排序(升序) //X1和X2进行比较,如果X1<X2,则交换位置,直到X1是余下的数中最大的 void BubbleSort(SeqList* pSeq) { assert(pSeq); size_t index,end; int count = 0; int exchange = 0; for (end = pSeq->size - 1; end > 0; --end) { exchange = 0; for (index=0; index < end; ++index) { count++; if (pSeq->array[index] > pSeq->array[index + 1]) { Swap(pSeq->array +index,pSeq->array +index+1); exchange = 1; } if (exchange == 0) break; } } printf("%d\n",count); } void SelectSort(SeqList* pSeq) { assert(pSeq); size_t index, minIndex, begin; for (begin = 0; begin < pSeq->size; ++begin) { minIndex = begin; for (index = begin + 1; index < pSeq->size; ++index) { if (pSeq->array[minIndex]>pSeq->array[index]) { minIndex = index; } } if (minIndex != begin) { Swap(pSeq->array +minIndex,pSeq->array +begin); } } } FindRet BinarySearch(SeqList * pSeq, DataType x) { assert(pSeq); FindRet ret; ret.isFind = FALSE; size_t right, mid ,left; left = 0; right = pSeq->size - 1; while (left <= right) { mid = (right + left) / 2; if (pSeq->array[mid] == x) { ret.isFind = TRUE; ret.index = mid; return ret; } else if (pSeq->array[mid] > x) { right = mid - 1; } else left = mid + 1; } return ret; } Tag Erase(SeqList* pSeq, DataType x, Tag all) { Tag success = FALSE; FindRet ret; assert(pSeq); ret = Find(pSeq, x, 0); while(ret.isFind == TRUE) { success = TRUE; Remove(pSeq, ret.index); if (all == FALSE) { break; } ret = Find(pSeq, x, ret.index); } return success; }
"main.c"
#include "DynamicSeqlist.h" void Test1() { SeqList s; InitSeqList(&s); CheckExpandCapacity(&s); PushBack(&s, 1); PushBack(&s, 2); PushBack(&s, 3); PushBack(&s, 4); PrintSeqList(&s); PopBack(&s); PrintSeqList(&s); PushFront(&s,0); PrintSeqList(&s); PopFront(&s); PrintSeqList(&s); DestorySeqList(&s); } void Test2() { SeqList s; InitSeqList(&s); CheckExpandCapacity(&s); PushBack(&s, 1); PushBack(&s, 2); PushBack(&s, 3); PushBack(&s, 4); PrintSeqList(&s); Insert(&s,1,8); PrintSeqList(&s); Modified(&s,2,5); PrintSeqList(&s); Remove(&s, 1); PrintSeqList(&s); Find(&s,3,0); DestorySeqList(&s); } void Test3() { SeqList s; InitSeqList(&s); CheckExpandCapacity(&s); PushBack(&s, 9); PushBack(&s, 4); PushBack(&s, 6); PushBack(&s, 2); PrintSeqList(&s); BubbleSort(&s); PrintSeqList(&s); SelectSort(&s); PrintSeqList(&s); DestorySeqList(&s); } void Test4() { SeqList s; InitSeqList(&s); CheckExpandCapacity(&s); PushBack(&s, 9); PushBack(&s, 4); PushBack(&s, 6); PushBack(&s, 2); PushBack(&s, 4); PushBack(&s, 3); PushBack(&s, 1); PrintSeqList(&s); Erase(&s,4,TRUE); PrintSeqList(&s); DestorySeqList(&s); } int main() { Test1(); printf("\n"); Test2(); printf("\n"); Test3(); printf("\n"); Test4(); getchar(); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-10 10:57:49