当前位置:首页 » 《随便一记》 » 正文

[C语言][数据结构][动态内存空间的开辟]顺序表的实现!

6 人参与  2024年04月09日 13:07  分类 : 《随便一记》  评论

点击全文阅读


目录

零.必备知识

a.顺序表的底层是数组.

b.数组在内存中是连续存放的.

c.动态内存空间的开辟(malloc,calloc,realloc).

一.顺序表的定义与实现 

        1.1 顺序表的定义 

        1.2 顺序表的初始化 

        1.3 顺序表的销毁 

        1.4 顺序表容量的检查与调整(最关键的部分)

        1.5 顺序表的尾插

        1.6 顺序表的头插 

        1.7 顺序表的尾删

        1.8 顺序表的头删

         1.9 顺序表中在指定位置之前插入数据

        1.10 删除指定位置的数据 

        1.11 顺序表中查找指定数据

二.顺序表源码 

        SeqList.h

        SeqList.c

 


零.必备知识


a.顺序表的底层是数组.

b.数组在内存中是连续存放的.

c.动态内存空间的开辟(malloc,calloc,realloc).

一.顺序表的定义与实现 

注:具体解释都在注释中(在代码中具体分析!)

        1.1 顺序表的定义 

        1.2 顺序表的初始化 

        1.3 顺序表的销毁 

        1.4 顺序表容量的检查与调整(最关键的部分)

 

        1.5 顺序表的尾插

 

        1.6 顺序表的头插 

 

        1.7 顺序表的尾删

 

        1.8 顺序表的头删

 

         1.9 顺序表中在指定位置之前插入数据

 

        1.10 删除指定位置的数据 

 

        1.11 顺序表中查找指定数据

 

二.顺序表源码 

        SeqList.h

#pragma once#include <stdio.h>#include <stdlib.h>#include <assert.h> 静态顺序表//struct SeqList//{//int arr[100];//int size;//};// 动态顺序表的定义typedef int DateType; //数据类型typedef struct SeqList{DateType* arr;int size; //数组中的有效元素个数int capacity; //数组的总容量}SL;// 顺序表的初始化void SLInit(SL* psl);// 顺序表的销毁void SLDestroy(SL* psl);// 打印检查void SLprint(SL sl);// 容量检查与调整void CheckCapacity(SL* psl);// 尾插-头插void SLPushBack(SL* psl, DateType x);void SLPushFront(SL* psl, DateType x);// 尾删-头删void SLPopBack(SL* psl);void SLPopFront(SL* psl);// 在指定位置之前插入数据void SLInsert(SL* psl, int pos, DateType x); //pos:点// 删除指定位置的数据void SLErase(SL* psl, int pos);// 顺序表的查找void SLFind(SL psl, DateType x);

        SeqList.c

 

#define  _CRT_SECURE_NO_WARNINGS#include "SeqList.h"// 顺序表的初始化void SLInit(SL* psl){psl->arr = NULL;psl->size = psl->capacity = 0;}// 顺序表的销毁void SLDestroy(SL* psl){if (psl->arr){free(psl->arr); //释放动态开辟的内存空间}psl->arr = NULL;psl->size = psl->capacity = 0;}// 容量检查与调整void CheckCapacity(SL* psl){if (psl->size == psl->capacity) //空间不够了,需要进行扩容{int newCapacity = psl->capacity == 0 ? 4 : psl->capacity * 2; //避免 psl->capacity初始容量为空DateType* temp = (DateType*)realloc(psl->arr, newCapacity * sizeof(DateType));if (temp == NULL) //避免因为realloc调整空间失败,而导致psl->arr中的数据被清除{perror("realloc fail!");exit(1);}psl->capacity = newCapacity;psl->arr = temp;}}// 尾插void SLPushBack(SL* psl, DateType x){CheckCapacity(psl);// 插入psl->arr[psl->size] = x;psl->size++;}// 头插void SLPushFront(SL* psl, DateType x){CheckCapacity(psl);for (int i = psl->size; i > 0; i--){psl->arr[i] = psl->arr[i - 1]; //psl->arr[1] = psl->arr[0]}psl->arr[0] = x;(psl->size)++;}// 尾删void SLPopBack(SL* psl){assert(psl);assert(psl->size); //判断是否为空(psl->size)--;}// 头删void SLPopFront(SL* psl){assert(psl);assert(psl->size); //判断是否为空for (int i = 0; i < psl->size - 1; i++){psl->arr[i] = psl->arr[i + 1]; //psl->arr[psl->size - 1] = psl->arr[psl->size - 2]}(psl->size)--;}// 在指定位置之前插入数据void SLInsert(SL* psl, int pos, DateType x){assert(psl);assert(pos >= 0 && pos <= psl->size);CheckCapacity(psl);for (int i = psl->size; i > pos; i--){psl->arr[i] = psl->arr[i - 1]; //arr[pos + 1] = arr[pos]}psl->arr[pos] = x;psl->size++;}// 删除指定位置的数据void SLErase(SL* psl, int pos){assert(psl);assert(psl->size != 0);assert(pos >= 0 && pos < psl->size);for (int i = pos; i < psl->size - 1; i++){psl->arr[i] = psl->arr[i + 1]; //arr[i - 2] = arr[i - 1]}psl->size--;}// 顺序表的查找void SLFind(SL* psl, DateType x){assert(psl);assert(psl->size != 0);for (int i = 0; i < psl->size; i++){if (psl->arr[i] == x){printf("找到了! 下标是%d\n", i);return;}}printf("找不到!\n");}// 打印void SLprint(SL sl){for (int i = 0; i < sl.size; i++){printf("%d ", sl.arr[i]);}printf("\n");}


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/92746.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 热文推荐沈棠眠宋闻璟折寒枝簪起霜思小说大结局(折寒枝簪起霜思)书荒必看列表
  • 我的爱,逾期不候(沈书言江映月)全书免费_(沈书言江映月)我的爱,逾期不候后续(沈书言江映月)
  • 婆婆住豪宅开豪车,却直播卖惨说我逼她睡桥洞全书沈威许静在线
  • 情深错付,在回忆里溺亡林小满江叙白_情深错付,在回忆里溺亡林小满江叙白
  • 未婚驸马表妹骑马撞我驴,我直接取消赐婚知乎热门_王法赵大人火麒麟超长版_小说后续在线阅读_无删减免费完结_
  • 老婆将我价值千万的游艇,送给了她的男助理知乎热门_妹妹林以棠念念后续_小说后续在线阅读_无删减免费完结_
  • 家宴过后,我捉奸了庶妹和我老公后续+番外_庶妹老公侍卫读者推荐_小说后续在线阅读_无删减免费完结_
  • 首富老公为三姐妹点天灯挥金如土,我换嫁贺总轰动全国强推_胥淮岁岁过敏最新阅读_小说后续在线阅读_无删减免费完结_
  • 未婚夫和同事孕期领证,我转身嫁入豪门推荐_沈泽光安南拿泽光小编推荐_小说后续在线阅读_无删减免费完结_
  • 重回奥运种子选手陷害我的这天番茄热门_林子李梦冷笑在线看_小说后续在线阅读_无删减免费完结_
  • 知微向海完结全文_陆知衍纪瑶奶奶最新阅读_小说后续在线阅读_无删减免费完结_
  • 被污蔑送错外卖后,我杀疯了阅读_小徐胡闹阅读_小说后续在线阅读_无删减免费完结_

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1