首页 > 代码库 > 动态链表增删改查及排序功能
动态链表增删改查及排序功能
主要功能的实现:
#include "SeqList.h" void InitSeqList(SeqList * pSeq)//初始化 { assert(pSeq); pSeq->array = (DataType*)malloc(sizeof(DataType)*DEFAULT_CAPICITY); pSeq->size = 0; pSeq->capicity = DEFAULT_CAPICITY; } void PrintSeqList(SeqList* pSeq)//打印 { assert(pSeq); size_t i = 0; for (; i < pSeq->size; i++) { printf("%d", pSeq->array[i]); } printf("\n"); } void CheckExpandCapicity(SeqList* pSeq)//检查容量 { assert(pSeq); if (pSeq->size == pSeq->capicity) { DataType *tmp = (DataType *)malloc(pSeq->capicity * 2 * sizeof(DataType)); memcpy(tmp, pSeq->array, sizeof(DataType)*pSeq->size); free(pSeq->array); pSeq->array = tmp; pSeq->capicity = pSeq->capicity * 2; } } void PushFront(SeqList* pSeq, DataType x)//头插 { int i = 0; assert(pSeq); CheckExpandCapicity(pSeq); for (i = pSeq->size; i >= 1; i--) { pSeq->array[i] = pSeq->array[i - 1]; } pSeq->array[0] = x; pSeq->size++; } void PopFront(SeqList* pSeq)//头删 { size_t i = 0; assert(pSeq); for (; i < pSeq->size - 1; i++) { pSeq->array[i] = pSeq->array[i + 1]; } pSeq->size--; } void PushBack(SeqList* pSeq, DataType x)//尾插 { assert(pSeq); CheckExpandCapicity(pSeq); pSeq->array[pSeq->size] = x; pSeq->size++; } void PopBack(SeqList* pSeq)//尾删 { assert(pSeq); pSeq->size--; } void Insert(SeqList* pSeq, size_t index, DataType x)//在index位置插入 { size_t i = pSeq->size; assert(pSeq); assert(index < pSeq->size); CheckExpandCapicity(pSeq); for (; i >index; i--) { pSeq->array[i] = pSeq->array[i - 1]; } pSeq->array[index] = x; pSeq->size++; } void Modify(SeqList* pSeq, size_t index, DataType x)//改动 { assert(pSeq); assert(index < pSeq->size); pSeq->array[index] = x; } void Remove(SeqList* pSeq, size_t index)//删除index位置的数 { size_t i = index; assert(pSeq); assert(index < pSeq->size); for (; i < pSeq->size - 1; i++) { pSeq->array[i] = pSeq->array[i + 1]; } pSeq->size--; } void Swap(DataType* left, DataType* right) { DataType tmp = *left; *left = *right; *right = tmp; } void BubbleSort(SeqList* pSeq)//冒泡排序 { size_t index, end; int exchange = 0; assert(pSeq); for (end = pSeq->size - 1; end > 0; --end) { exchange = 0; for (index = 0; index < end; index++) { if (pSeq->array[index]>pSeq->array[index + 1]) { Swap(pSeq->array + index, pSeq->array + index + 1); exchange = 1; } } if (exchange == 0) { break; } } } void SelectSort(SeqList* pSeq)//选择排序 { size_t MinIndex, index, begin; assert(pSeq); for (begin = 0; begin < pSeq->size - 1; 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)//二分查找 { size_t left=0; size_t right = pSeq->size - 1;; size_t middle; FindRet ret; ret.isFind = FALSE; assert(pSeq); while (left<=right) { middle = (left + right) / 2; if (x == pSeq->array[middle]) { ret.isFind = TRUE; ret.index = middle; return ret; } else if (x>pSeq->array[middle]) left = middle + 1; else right = middle - 1; } return ret; }
头文件:
#pragma once #define DEFAULT_CAPICITY 3 typedef int DataType; #include<stdio.h> #include<string.h> #include<assert.h> #include<malloc.h> typedef struct SeqList { DataType *array; size_t size; size_t capicity;//当前的容量 }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 CheckExpandCapicity(SeqList* pSeq); void PushFront(SeqList *pSeq, DataType x); void PopFront(SeqList *pSeq); void PushBack(SeqList *pSeq, DataType x); void PopBack(SeqList *pSeq); void Insert(SeqList *pSeq, size_t index, DataType x); void Modify(SeqList *pSeq, size_t index, DataType x); void Remove(SeqList *pSeq, size_t index); void Swap(DataType* left, DataType* right); void BubbleSort(SeqList* pSeq); void SelectSort(SeqList* pSeq); FindRet BinarySearch(SeqList* pSep, DataType x);
測试程序部分:
#include "SeqList.h" void test1()//測试初始化、打印、尾插/尾删 { SeqList s; InitSeqList(&s); PushBack(&s, 1); PushBack(&s, 2); PushBack(&s, 3); PushBack(&s, 4); PrintSeqList(&s); PopBack(&s); PrintSeqList(&s); } void test2()//測试头插、头删 { SeqList s; InitSeqList(&s); PushFront(&s, 3); PushFront(&s, 4); PushFront(&s, 5); PushFront(&s, 6); PrintSeqList(&s); PopFront(&s); PrintSeqList(&s); } void test3()//測试在index位置插入、改动index位置的值、删除index位置的值 { SeqList s; InitSeqList(&s); PushBack(&s, 1); PushBack(&s, 2); PushBack(&s, 3); PushBack(&s, 4); PrintSeqList(&s); Insert(&s, 2, 8); PrintSeqList(&s); Modify(&s,2,5); PrintSeqList(&s); Remove(&s, 2); PrintSeqList(&s); } void test4()//測试冒泡排序 { SeqList s; InitSeqList(&s); PushBack(&s, 3); PushBack(&s, 1); PushBack(&s, 5); PushBack(&s, 4); PushBack(&s, 2); PrintSeqList(&s); BubbleSort(&s); PrintSeqList(&s); } void test5()//測试选择排序 { SeqList s; InitSeqList(&s); PushBack(&s, 3); PushBack(&s, 1); PushBack(&s, 5); PushBack(&s, 4); PushBack(&s, 2); PrintSeqList(&s); SelectSort(&s); PrintSeqList(&s); } void test6()//測试二分搜索 { DataType x; FindRet ret; SeqList s; InitSeqList(&s); PushBack(&s, 1); PushBack(&s, 2); PushBack(&s, 3); PushBack(&s, 4); PushBack(&s, 5); x = 4; ret = BinarySearch(&s, x); if (ret.isFind == TRUE) { printf("%d %d\n",x,ret.index); } else printf("find failed!\n"); x = 8; ret = BinarySearch(&s, x); if (ret.isFind == TRUE) { printf("%d %d", x, ret.index); } else printf("find failed!\n"); x = 1; ret = BinarySearch(&s, x); if (ret.isFind == TRUE) { printf("%d %d\n", x, ret.index); } else printf("find failed!\n"); x = 5; ret = BinarySearch(&s, x); if (ret.isFind == TRUE) { printf("%d %d\n", x, ret.index); } else printf("find failed!\n"); } int main() { test1(); test2(); //test3(); test4(); test5(); test6(); getchar(); return 0; }
动态链表增删改查及排序功能
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。