数据结构学习笔记

数据结构学习笔记(一)

假期以来我都坚持每天看一点郝斌的数据结构视频。讲的很透彻,也很风趣。

前几天都是为讲数据结构而做准备,讲了一些结构体和指针,今天终于开始正式将数据结构。说实话,我今天才知道函数的用处。。

照着郝斌讲连续存储数组的算法演示,又自己写了一遍,发现有一个错误,左看右看都看不出哪错了,索性贴出了,,,有兴趣的朋友可以看看

百度求助,一位牛人看出错误来,谢谢了!重新贴出正确的代码

  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>//包含exit
  4. intval,i,t;
  5. structArr
  6. {
  7. int*pBase;//储存的是数组第一个元素的地址
  8. intlen;//数组所能容纳的最大元素个数
  9. intcnt;//当前数组有效个数
  10. };
  11. voidinit_arr(structArr*pArr,intlength);//初始化
  12. boolappend_arr(structArr*pArr,intval);
  13. boolinsert_arr(structArr*pArr,intpos,intval);//pos的值从1开始
  14. booldelete_arr(structArr*pArr,intpos,int*pVal);
  15. intget();
  16. boolis_empty(structArr*pArr);
  17. boolis_full(structArr*pArr);
  18. voidsort_arr(structArr*pArr);
  19. voidshow_arr(structArr*pArr);
  20. voidinversion_arr(structArr*pArr);//倒置
  21. intmain()
  22. {
  23. structArrarr;
  24. init_arr(&arr,6);
  25. show_arr(&arr);
  26. append_arr(&arr,1);
  27. append_arr(&arr,2);
  28. append_arr(&arr,3);
  29. append_arr(&arr,4);
  30. delete_arr(&arr,1,&val);
  31. return0;
  32. }
  33. voidinit_arr(structArr*pArr,intlength)
  34. {
  35. pArr->pBase=(int*)malloc(sizeof(int)*length);
  36. if(NULL==pArr->pBase)
  37. {
  38. printf("动态内存分配失败!\n");
  39. exit(-1);//终止整个程序
  40. }
  41. else
  42. {
  43. pArr->len=length;
  44. pArr->cnt=0;
  45. }
  46. return;
  47. }
  48. boolis_empty(structArr*pArr)
  49. {
  50. if(0==pArr->cnt)
  51. returntrue;
  52. else
  53. returnfalse;
  54. }
  55. boolis_full(structArr*pArr)
  56. {
  57. if(pArr->cnt==pArr->len)
  58. returntrue;
  59. else
  60. returnfalse;
  61. }
  62. voidshow_arr(structArr*pArr)
  63. {
  64. if(is_empty(pArr))//pArr本来就是地址
  65. {
  66. printf("数组为空\n");
  67. }
  68. else
  69. {
  70. for(inti=0;i<pArr->cnt;++i)
  71. printf("%d",pArr->pBase[i]);
  72. printf("\n");
  73. }
  74. }
  75. boolappend_arr(structArr*pArr,intval)
  76. {
  77. if(is_full(pArr))
  78. returnfalse;
  79. else
  80. pArr->pBase[pArr->cnt]=val;
  81. (pArr->cnt)++;
  82. returntrue;
  83. }
  84. boolinsert_arr(structArr*pArr,intpos,intval)
  85. {
  86. inti;
  87. if(pos<1||pos>pArr->len)
  88. for(i=pArr->cnt-1;i>=pos-1;--i)
  89. {
  90. pArr->pBase[i+1]=pArr->pBase[i];
  91. }
  92. pArr->pBase[pos-1]=val;
  93. returntrue;
  94. }
  95. booldelete_arr(structArr*pArr,intpos,int*pVal)
  96. {
  97. if(is_empty(pArr))
  98. returnfalse;
  99. if(pos<1||pos>pArr->cnt)
  100. returnfalse;
  101. *pVal=pArr->pBase[pos-1];
  102. for(i=pos;i<pArr->cnt;i++)
  103. {
  104. pArr->pBase[i-1]=pArr->pBase[i];
  105. }
  106. pArr->cnt--;
  107. returntrue;
  108. }
  109. voidinversion_arr(structArr*pArr)
  110. {
  111. inti=0;
  112. intj=pArr->cnt-1;
  113. intt;
  114. while(i<j)
  115. {
  116. t=pArr->pBase[i];
  117. pArr->pBase[i]=pArr->pBase[j];
  118. pArr->pBase[j]=t;
  119. i++;
  120. j--;
  121. }
  122. return;
  123. }
  124. voidsort_arr(structArr*pArr)
  125. {
  126. inti,j;//冒泡法
  127. for(i=0;i<pArr->cnt;i++)
  128. {
  129. for(j=i+1;j<pArr->cnt;i++)
  130. {
  131. if(pArr->pBase[i]>pArr->pBase[j])
  132. {
  133. t=pArr->pBase[i];
  134. pArr->pBase[i]=pArr->pBase[j];
  135. pArr->pBase[j]=t;
  136. }
  137. }
  138. }
  139. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值