數據結構學習筆記(一)
假期以來我都堅持每天看一點郝斌的數據結構視頻。講的很透徹,也很風趣。
前幾天都是為講數據結構而做準備,講了一些結構體和指針,今天終于開始正式將數據結構。說實話,我今天才知道函數的用處。。
照著郝斌講連續存儲數組的算法演示,又自己寫了一遍,發現有一個錯誤,左看右看都看不出哪錯了,索性貼出了,,,有興趣的朋友可以看看
百度求助,一位牛人看出錯誤來,謝謝了!重新貼出正確的代碼
- #include?<stdio.h>??
- #include?<malloc.h>??
- #include?<stdlib.h>???//??包含exit??
- int?val,i,t;??
- struct?Arr??
- {??
- ????int?*?pBase;????//儲存的是數組第一個元素的地址??
- ????int?len;????//數組所能容納的最大元素個數??
- ????int?cnt;????//當前數組有效個數??
- };??
- void?init_arr(struct?Arr?*?pArr,int?length);????//初始化??
- bool?append_arr(struct?Arr?*?pArr,int?val);??
- bool?insert_arr(struct?Arr?*?pArr,int?pos,int?val);?????//pos的值從1開始??
- bool?delete_arr(struct?Arr?*?pArr,int?pos,int?*pVal);??
- int?get();??
- bool?is_empty(struct?Arr?*?pArr);??
- bool?is_full(struct?Arr?*?pArr);??
- void?sort_arr(struct?Arr?*?pArr);??
- void?show_arr(struct?Arr?*?pArr);??
- void?inversion_arr(struct?Arr?*?pArr);??//倒置??
- int?main()??
- {??
- ????struct?Arr?arr;??
- ????init_arr(&arr,6);??
- ????show_arr(&arr);??
- ????append_arr(&arr,1);??
- ????append_arr(&arr,2);??
- ????append_arr(&arr,3);??
- ????append_arr(&arr,4);??
- ????delete_arr(&arr,1,&val);??
- ????return?0;??
- }??
- void?init_arr(struct?Arr?*?pArr,int?length)??
- {??
- ????pArr->pBase?=?(int?*)malloc(sizeof(int)?*?length);??
- ????if?(NULL?==?pArr->pBase)??
- ????{??
- ????????printf("動態內存分配失敗!\n");??
- ????????exit(-1);???//終止整個程序??
- ????}??
- ????else??
- ????{??
- ????????pArr->len?=?length;??
- ????????pArr->cnt?=?0;??
- ????}??
- ????return;??
- ??
- }??
- bool?is_empty(struct?Arr?*?pArr)??
- {??
- ????if(0?==?pArr->cnt)??
- ????????return?true;??
- ????else??
- ????????return?false;??
- }??
- bool?is_full(struct?Arr?*?pArr)??
- {??
- ????if(pArr->cnt?==?pArr->len)??
- ????????return?true;??
- ????else??
- ????????return?false;??
- }??
- void?show_arr(struct?Arr?*?pArr)??
- {??
- ????if(?is_empty(pArr)?)????//pArr本來就是地址??
- ????{??
- ????????printf("數組為空\n");??
- ????}??
- ????else??
- ????{??
- ????????for(int?i=0;i<pArr->cnt;++i)??
- ????????????printf("%d???",pArr->pBase[i]);??
- ????????printf("\n");??
- ????}??
- }??
- bool?append_arr(struct?Arr?*?pArr,int?val)??
- {??
- ????if(?is_full(pArr)?)??
- ????????return?false;??
- ????else??
- ????????pArr->pBase[pArr->cnt]?=?val;??
- ????????(pArr->cnt)++;??
- ????return?true;??
- }??
- bool?insert_arr(struct?Arr?*?pArr,int?pos,int?val)??
- {??
- ????int?i;??
- ????if(pos<1||pos>pArr->len)??
- ????for?(i=pArr->cnt-1;i>=pos-1;--i)??
- ????{??
- ????????pArr->pBase[i+1]?=?pArr->pBase[i];??
- ????}??
- ????pArr->pBase[pos-1]?=?val;??
- ????return?true;??
- }??
- bool?delete_arr(struct?Arr?*?pArr,int?pos,int?*pVal)??
- {??
- ????if?(?is_empty(pArr)?)??
- ????????return?false;??
- ????if?(pos<1||?pos>pArr->cnt)??
- ????????return?false;??
- ????*pVal?=?pArr->pBase[pos-1];??
- ????for(i=pos;?i<pArr->cnt;i++)??
- ????{??
- ????????pArr->pBase[i-1]?=?pArr->pBase[i];??
- ????}??
- ????pArr->cnt--;??
- ????return?true;??
- }??
- void?inversion_arr(struct?Arr?*?pArr)??
- {??
- ????int?i?=?0;??
- ????int?j?=?pArr->cnt-1;??
- ????int?t;??
- ????while?(i<j)??
- ????{??
- ????????t?=?pArr->pBase[i];??
- ????????pArr->pBase[i]?=?pArr->pBase[j];??
- ????????pArr->pBase[j]?=?t;??
- ????????i++;??
- ????????j--;??
- ????}??
- ????return;??
- }??
- void?sort_arr(struct?Arr?*?pArr)??
- {??
- ????int?i,j;????//冒泡法??
- ????for(i=0;i<pArr->cnt;i++)??
- ????{??
- ????????for(j=i+1;j<pArr->cnt;i++)??
- ????????{??
- ????????????if(pArr->pBase[i]?>?pArr->pBase[j])??
- ????????????{??
- ????????????????t?=?pArr->pBase[i];??
- ????????????????pArr->pBase[i]?=?pArr->pBase[j];??
- ????????????????pArr->pBase[j]?=?t;??
- ????????????}??
- ????????}??
- ????}??
- }??