找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1940|回复: 3
打印 上一主题 下一主题
收起左侧

C语言动态顺序表源程序

[复制链接]
跳转到指定楼层
楼主
ID:170498 发表于 2019-4-1 17:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
最近在学习stm32的时候发现自己的c语言基础还停留在c51的水平,因此做一些练习提高自己的c语言水平。
我参考了 《妙趣横生的算法-c语言实现》这本书,建议大家从书 店入手

  1. #include <stdio.h>

  2. #include <mallco.h>              // 使用malloc函数要包含这个文件
  3. #include <assert.h>


  4. #define INIT_CAPACITY 10  // 初始化时分配的容量


  5. typedef struct seqlist{
  6.     int *array;        // 存储空间的基地址
  7.     int size;          // 有效元素个数
  8.     int capacity;      // 当前分配的存储容量
  9. }SEQLIST;


  10. void seqlist_init(SEQLIST *list);          //初始化顺序表
  11. void check_capacity(SEQLIST *list);        // 检查是否满

  12. void push_front(SEQLIST *list, int value);  // 在顺序表前头部插入元素
  13. void push_back(SEQLIST *list, int value);   // 在顺序表尾部插入元素
  14. void insert_list(SEQLIST *list, int pos, int value);        // 在pos这个位置插入元素

  15. void pop_front(SEQLIST *list);              // 删除顺序表第一个元素
  16. void pop_back(SEQLIST *list);               // 删除顺序表最后一个元素

  17. void list_printf(SEQLIST *list);            // 遍历顺序表
  18. void destroy_list(SEQLIST *list);           // 摧毁顺序表
  19. void list_cleardata(SEQLIST *list);         // 清空顺序表的数据


  20. int main(void)
  21. {
  22.     SEQLIST list;
  23.    
  24.     seqlist_init(&list);
  25.     push_front(&list, 3);
  26.     push_front(&list, 2);
  27.     push_front(&list, 1);
  28. //    list_printf(&list);
  29.    
  30.     push_back(&list, 3);
  31.     push_back(&list, 2);
  32.     push_back(&list, 1);
  33.     pop_back(&list);
  34.     pop_front(&list);
  35.     insert_list(&list, 3, 4);
  36.     list_printf(&list);
  37.    
  38.     return 0;
  39. }


  40. // 初始化顺序表

  41. void seqlist_init(SEQLIST *list)
  42. {
  43.     assert(list);   // 断言:判断list是否为空
  44.    
  45.     list->array = (int *)malloc(INIT_CAPACITY * sizeof(int)); // 分配一断内存,容量为 INIT_CAPACITY个int的大小
  46.     assert(list->array);               // 断言:判断aray是否为空
  47.     list->size = 0;                    // 没有赋值之前有效元素个数为0
  48.     list->capacity = INIT_CAPACITY;    // 容量
  49. }


  50. // 判断顺序表是否以满,如果满了,增加顺序表的容量

  51. void check_capacity(SEQLIST *list)
  52. {
  53.     assert(list);    // 判断传入的指针是否为空
  54.    
  55.     if (list->size == list->capacity)
  56.     {
  57.          // 重新分配一块内存,但是之前的数据不变
  58.         list->array = (int *)realloc(list->array, (2 * list->capacity * sizeof(int)));
  59.         assert(list);            // 判断是否分配成功
  60.         list->capacity *= 2;     // 容量翻倍
  61.     }
  62. }


  63. // 在顺序表前插入数据   
  64. void push_front(SEQLIST *list, int value)
  65. {
  66.     int i;
  67.    
  68.     assert(list);            // 判断传入的是否为空
  69.     check_capacity(list);    // 在执行插入之前先判断顺序表是否以满,如果满了,增加顺序表的容量
  70.    
  71.     // 将所有元素向后移动一位
  72.     for(i=list->size; i>=0; i--)
  73.     {
  74.         list->array[i] = list->array[i-1];
  75.     }
  76.     list->array[0] = value;  // 将插入的值赋给第一个元素
  77.     list->size++;            // 有效元素个数 +1   
  78. }


  79. // 在顺序表尾部插入数据   
  80. void push_back(SEQLIST *list, int value)
  81. {
  82.     assert(list);    // 判断传入的是否为空
  83.    
  84.     check_capacity(list);  // 在执行插入之前先判断顺序表是否以满,如果满了,增加顺序表的容量
  85.    
  86.     list->array[list->size] = value; // size记录了当前有效元素的个数,而数组从0开始,array[list->size]处在顺序表最后一个元素的位置+1
  87.     list->size++;           // 插入后,有效元素个数+1
  88. }


  89. // 在顺序表第pos个位置插入数据   
  90. void insert_list(SEQLIST *list, int pos, int value)
  91. {
  92.     int i;
  93.    
  94.     assert(list);  // 判断传入的指针是否为空
  95.     if (pos > list->size+1 && pos < 0)
  96.     {
  97.         printf("非法操作!!\n");
  98.         return;   
  99.     }
  100.    

  101.     // 将第pos个位置之后的数据往后移动一位
  102.     for (i=list->size; i>=pos; i--)
  103.     {
  104.         list->array[i] = list->array[i-1];   
  105.     }
  106.    
  107.     list->array[pos-1] = value;
  108.     list->size++;
  109. }


  110. // 删除头部数据
  111. void pop_front(SEQLIST *list)
  112. {
  113.     int i = 0;
  114.    
  115.     assert(list);    // 判断传入的是否为空
  116.    
  117.     if (list->size == 1)
  118.     {
  119.         list->size = 0;
  120.     }
  121.     else
  122.     {
  123.         for (i=0; i<list->size; i++)
  124.         {
  125.             // 将后面的赋值给前面的,第0个元素的数据就不管了(第0个数据被覆盖)
  126.             // 第1个元素的数据给第0个数据  第2个元素的数据给第1个数据 ...
  127.             list->array[i] = list->array[i+1];  
  128.         }
  129.     }
  130.     list->size--; // 有效数据的个数减1
  131. }

  132. // 尾部插入
  133. void pop_back(SEQLIST *list)
  134. {
  135.     assert(list);
  136.    
  137.     if (list->size == 0)
  138.     {
  139.         return;   
  140.     }
  141.     else
  142.     {
  143.         list->size--;
  144.     }
  145. }

  146. // 遍历数组,并打印

  147. void list_printf(SEQLIST *list)
  148. {
  149.     int i;
  150.    
  151.     for (i=0; i<list->size; i++)
  152.     {
  153.         printf("%d  \n", list->array[i]);
  154.     }
  155. }
  156.   // 摧毁顺序表
  157. void destroy_list(SEQLIST *list)
  158. {
  159.     free(list->array);
  160.    
  161.     list->size = 0;
  162.     list->capacity = 0;
  163. }

  164. // 清空顺序表的数据
  165. void list_cleardata(SEQLIST *list)
  166. {
  167.     list->size = 0;
  168. }
复制代码




评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:430197 发表于 2019-4-2 12:13 | 只看该作者
厉害厉害,有点意思
回复

使用道具 举报

板凳
ID:33544 发表于 2019-8-29 11:04 | 只看该作者
厉害厉害,有点意思
回复

使用道具 举报

地板
ID:68189 发表于 2019-8-30 16:24 | 只看该作者
提高一下自己 ,学习前辈的经验.谢谢.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表