數據結構學習筆記

數據結構學習筆記(一)

假期以來我都堅持每天看一點郝斌的數據結構視頻。講的很透徹,也很風趣。

前幾天都是為講數據結構而做準備,講了一些結構體和指針,今天終于開始正式將數據結構。說實話,我今天才知道函數的用處。。

照著郝斌講連續存儲數組的算法演示,又自己寫了一遍,發現有一個錯誤,左看右看都看不出哪錯了,索性貼出了,,,有興趣的朋友可以看看

百度求助,一位牛人看出錯誤來,謝謝了!重新貼出正確的代碼

[cpp] view plaincopy
  1. #include?<stdio.h>??
  2. #include?<malloc.h>??
  3. #include?<stdlib.h>???//??包含exit??
  4. int?val,i,t;??
  5. struct?Arr??
  6. {??
  7. ????int?*?pBase;????//儲存的是數組第一個元素的地址??
  8. ????int?len;????//數組所能容納的最大元素個數??
  9. ????int?cnt;????//當前數組有效個數??
  10. };??
  11. void?init_arr(struct?Arr?*?pArr,int?length);????//初始化??
  12. bool?append_arr(struct?Arr?*?pArr,int?val);??
  13. bool?insert_arr(struct?Arr?*?pArr,int?pos,int?val);?????//pos的值從1開始??
  14. bool?delete_arr(struct?Arr?*?pArr,int?pos,int?*pVal);??
  15. int?get();??
  16. bool?is_empty(struct?Arr?*?pArr);??
  17. bool?is_full(struct?Arr?*?pArr);??
  18. void?sort_arr(struct?Arr?*?pArr);??
  19. void?show_arr(struct?Arr?*?pArr);??
  20. void?inversion_arr(struct?Arr?*?pArr);??//倒置??
  21. int?main()??
  22. {??
  23. ????struct?Arr?arr;??
  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. ????return?0;??
  32. }??
  33. void?init_arr(struct?Arr?*?pArr,int?length)??
  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. }??
  49. bool?is_empty(struct?Arr?*?pArr)??
  50. {??
  51. ????if(0?==?pArr->cnt)??
  52. ????????return?true;??
  53. ????else??
  54. ????????return?false;??
  55. }??
  56. bool?is_full(struct?Arr?*?pArr)??
  57. {??
  58. ????if(pArr->cnt?==?pArr->len)??
  59. ????????return?true;??
  60. ????else??
  61. ????????return?false;??
  62. }??
  63. void?show_arr(struct?Arr?*?pArr)??
  64. {??
  65. ????if(?is_empty(pArr)?)????//pArr本來就是地址??
  66. ????{??
  67. ????????printf("數組為空\n");??
  68. ????}??
  69. ????else??
  70. ????{??
  71. ????????for(int?i=0;i<pArr->cnt;++i)??
  72. ????????????printf("%d???",pArr->pBase[i]);??
  73. ????????printf("\n");??
  74. ????}??
  75. }??
  76. bool?append_arr(struct?Arr?*?pArr,int?val)??
  77. {??
  78. ????if(?is_full(pArr)?)??
  79. ????????return?false;??
  80. ????else??
  81. ????????pArr->pBase[pArr->cnt]?=?val;??
  82. ????????(pArr->cnt)++;??
  83. ????return?true;??
  84. }??
  85. bool?insert_arr(struct?Arr?*?pArr,int?pos,int?val)??
  86. {??
  87. ????int?i;??
  88. ????if(pos<1||pos>pArr->len)??
  89. ????for?(i=pArr->cnt-1;i>=pos-1;--i)??
  90. ????{??
  91. ????????pArr->pBase[i+1]?=?pArr->pBase[i];??
  92. ????}??
  93. ????pArr->pBase[pos-1]?=?val;??
  94. ????return?true;??
  95. }??
  96. bool?delete_arr(struct?Arr?*?pArr,int?pos,int?*pVal)??
  97. {??
  98. ????if?(?is_empty(pArr)?)??
  99. ????????return?false;??
  100. ????if?(pos<1||?pos>pArr->cnt)??
  101. ????????return?false;??
  102. ????*pVal?=?pArr->pBase[pos-1];??
  103. ????for(i=pos;?i<pArr->cnt;i++)??
  104. ????{??
  105. ????????pArr->pBase[i-1]?=?pArr->pBase[i];??
  106. ????}??
  107. ????pArr->cnt--;??
  108. ????return?true;??
  109. }??
  110. void?inversion_arr(struct?Arr?*?pArr)??
  111. {??
  112. ????int?i?=?0;??
  113. ????int?j?=?pArr->cnt-1;??
  114. ????int?t;??
  115. ????while?(i<j)??
  116. ????{??
  117. ????????t?=?pArr->pBase[i];??
  118. ????????pArr->pBase[i]?=?pArr->pBase[j];??
  119. ????????pArr->pBase[j]?=?t;??
  120. ????????i++;??
  121. ????????j--;??
  122. ????}??
  123. ????return;??
  124. }??
  125. void?sort_arr(struct?Arr?*?pArr)??
  126. {??
  127. ????int?i,j;????//冒泡法??
  128. ????for(i=0;i<pArr->cnt;i++)??
  129. ????{??
  130. ????????for(j=i+1;j<pArr->cnt;i++)??
  131. ????????{??
  132. ????????????if(pArr->pBase[i]?>?pArr->pBase[j])??
  133. ????????????{??
  134. ????????????????t?=?pArr->pBase[i];??
  135. ????????????????pArr->pBase[i]?=?pArr->pBase[j];??
  136. ????????????????pArr->pBase[j]?=?t;??
  137. ????????????}??
  138. ????????}??
  139. ????}??
  140. }??

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/452986.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/452986.shtml
英文地址,請注明出處:http://en.pswp.cn/news/452986.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

2018-1-27:內存的劃分

內存的劃分 1.寄存器 2.本地方法區 3.方法區 4.棧內存 存儲的都是局部變量&#xff0c;變量的作用域一結束&#xff0c;該變量就立即釋放 5.堆內存 存儲的是對象&#xff0c;即凡是new的都在堆中 特點&#xff1a;   1.每一個實體都有首地址 2.堆內存中每一個變量都有默認初始…

mysql 1130 localhost_解決1130 Host 'localhost' is not allowed to connect to this MySQL server

WAMP 64bit安裝好后&#xff0c;連接數據庫&#xff0c;提示#1130 Host ‘localhost’ is not allowed to connect to this MySQL server1.首先停止mysql服務&#xff0c;如果不會可以直接停止所有wampserver。2.然后打開一個命令行窗口&#xff0c;切換到mysql的安裝目錄&…

Ubuntu 14.04下安裝Redis報錯:“You need tcl 8.5 or newer in order to run the Redis test”問題解決

解決方案參考文獻&#xff1a;安裝tcl https://blog.csdn.net/zhangshu123321/article/details/51440106 感謝樓主分享&#xff01;

InstallSheild 獲取系統環境變量,如Desktop路徑等

使用FOLDER_DESKTOP變量獲取的桌面路徑可能為&#xff1a;C:\Users\Public\Desktop 而不是C:\Users\用戶\Desktop Copy and paste the following define statements at the beginning of your setup.rul file. Also, make sure you copy and paste the prototype for the SHGe…

線程2---異步1

在Java中什么是同步&#xff1f;什么是異步&#xff1f;對于這兩個概念我們必須要明確。只有明確這兩個概念&#xff0c;才會在明確在什么場景下使用同步以及異步。 在這里我可以形象的舉個例子來辨明這兩個概念&#xff1a; 1.同步與異步同步和異步關注的是消息通信機制 (sync…

mysql通常使用語句_Mysql 常用SQL語句集錦

基礎篇//查詢時間&#xff0c;友好提示$sql "select date_format(create_time, %Y-%m-%d) as day from table_name";//int 時間戳類型$sql "select from_unixtime(create_time, %Y-%m-%d) as day from table_name";//一個sql返回多個總數$sql "sel…

為什么你需要設計和維護一套自我移動標準?

在一個很長的調研日的休息時分&#xff0c;我在同一個客戶談一個項目&#xff0c;在這個項目中&#xff0c;我正為一個全球經濟公司開發一個iOS標準。他們的第一反應是這樣的&#xff1a;“什么&#xff1f;你在開發Apple iOS以外的標準&#xff1f;那還要開發什么&#xff1f;…

JS 小知識點匯總

1.offsetWidth & width A:因為. 1.offsetWidth是只讀屬性&#xff0c;而style.width是可讀寫得。2.offsetWidth返回的是一個整數&#xff0c;style.width返回的是一個字符串,并且帶有單位&#xff1b;**3.style.width只能返回以style方式定義的內部樣式的width屬性值。4.of…

django-redis中redis.conf配置詳細說明

參考文獻&#xff1a;https://blog.csdn.net/ljphilp/article/details/52934933 感謝樓主分享&#xff01;

初識Mysql(一)

1 搭建數據庫服務器2 MYSQL數據類型3 修改表結構1 搭建數據庫服務器1.1 存在mariadb時&#xff1a;#systemctl status mariadb#systemctl stop mariadb#rpm -e --nodeps mariadb-libs #不依賴關系卸載#rm -rf /etc/my.cnf#rm -rf /var/lib/mysql/*1.2 新安裝時[rootser51 ~]# …

數據挖掘資料

https://blog.csdn.net/baimafujinji/article/details/53269040 在2006年12月召開的 IEEE 數據挖掘國際會議上&#xff08;ICDM&#xff0c; International Conference on Data Mining&#xff09;&#xff0c;與會的各位專家選出了當時的十大數據挖掘算法&#xff08; top 10 …

如何進行個人知識管理和提高自己能力?

21世紀是一個知識爆炸的世紀&#xff0c;知識爆炸是指人類創造的知識,主要是自然科學知識,在短時期內以極高的速度增長起來。是人們對當前大量出現并飛速發展的各種知識現象所進行的夸張和描述。有人綜合計算,全世界的知識總量,七到十年翻一番。這就是風行全球的摩登名詞的意思…

redis服務器端和客戶端啟動

服務器端 sudo redis-server /etc/redis/redis.conf 指定加載的配置文件 ps -ef|grep redis 查看redis服務器進程 sudo kill -9 pid 殺死redis服務器 連接特定IP的redis&#xff1a;

hive表移到mysql_如何將Hive數據表移動到MySql?

我想知道如何將日期從Hive轉移到MySQL&#xff1f;我已經看到了如何將hive數據移動到Amazon DynamoDB而不是像MySQL這樣的RDBMS的示例.以下是我在DynamoDB中看到的示例&#xff1a;CREATE EXTERNAL TABLE tbl1 ( name string, location string )STORED BY org.apache.hadoop.hi…

賬簿與平衡段關聯表

gl_ledger_norm_seg_vals轉載于:https://www.cnblogs.com/lizicheng/p/8817982.html

超級丑數

題目&#xff1a; 寫一個程序來找第 n 個超級丑數。超級丑數的定義是正整數并且所有的質數因子都在所給定的一個大小為 k 的質數集合內。比如給你 4 個質數的集合 [2, 7, 13, 19], 那么 [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] 是前 12 個超級丑數。注意事項&#xff1a;…

為什么要進行個人知識管理(PKM)

21世紀是一個知識爆炸的世紀&#xff0c;知識爆炸是指人類創造的知識,主要是自然科學知識,在短時期內以極高的速度增長起來。是人們對當前大量出現并飛速發展的各種知識現象所進行的夸張和描述。有人綜合計算,全世界的知識總量,七到十年翻一番。這就是風行全球的摩登名詞的意思…