数据结构开发(7):典型问题分析(Bugfix)
0.目錄
1.創(chuàng)建異常對象時的空指針問題
2.LinkList 中的數(shù)據(jù)元素刪除
3.LinkList 中遍歷操作與刪除操作的混合使用
4.StaticLinkList 中數(shù)據(jù)元素刪除時的效率問題
5.StaticLinkList 是否需要提供析構(gòu)函數(shù)?
6.StLib 是否有必要增加多維數(shù)組類?
1.創(chuàng)建異常對象時的空指針問題
ISSUE 1——創(chuàng)建異常對象時的空指針問題:
改進(jìn)Exception.cpp:
在init函數(shù)中改進(jìn)strdup的賦值方法
2.LinkList 中的數(shù)據(jù)元素刪除
ISSUE 2——LinkList 中的數(shù)據(jù)元素刪除:
單鏈表的實(shí)現(xiàn)沒有考慮異常安全性!
改進(jìn)LinkList.h中的remove函數(shù)和clear函數(shù):
3.LinkList 中遍歷操作與刪除操作的混合使用
ISSUE 3——LinkList 中遍歷操作與刪除操作的混合使用:
混合使用導(dǎo)致了m_current指向了已經(jīng)被刪除的結(jié)點(diǎn),于是程序出錯。
改進(jìn)LinkList.h中的remove函數(shù):
main.cpp測試
#include <iostream> #include "LinkList.h"using namespace std; using namespace StLib;int main() {LinkList<int> list;for(int i=0; i<5; i++){list.insert(i);}for(list.move(0); !list.end(); list.next()){if( list.current() == 3 ){list.remove(list.find(list.current()));cout << list.current() << endl;}}for(int i=0; i<list.length(); i++){cout << list.get(i) << endl;}return 0; }運(yùn)行結(jié)果為:
4 0 1 2 44.StaticLinkList 中數(shù)據(jù)元素刪除時的效率問題
ISSUE 4——StaticLinkList 中數(shù)據(jù)元素刪除時的效率問題:
改進(jìn)LinkList.h中的destroy函數(shù):
void destroy(Node* pn){SNode* space = reinterpret_cast<SNode*>(m_space);SNode* psn = dynamic_cast<SNode*>(pn);for(int i=0; i<N; i++){if( psn == (space + i) ){m_used[i] = 0;psn->~SNode();break;}}}main.cpp測試
#include <iostream> #include "StaticLinkList.h"using namespace std; using namespace StLib;int main() {StaticLinkList<int, 10> list;for(int i=0; i<5; i++){list.insert(i);}list.remove(3);for(int i=0; i<list.length(); i++){cout << list.get(i) << endl;}return 0; }運(yùn)行結(jié)果為:
0 1 2 45.StaticLinkList 是否需要提供析構(gòu)函數(shù)?
ISSUE 5——StaticLinkList 是否需要提供析構(gòu)函數(shù)?:
構(gòu)造函數(shù)與析構(gòu)函數(shù)不會發(fā)生多態(tài),于是調(diào)用的是父類的析構(gòu)函數(shù)!
改進(jìn)StaticLinkList.h:
在子類StaticLinkList中實(shí)現(xiàn)析構(gòu)函數(shù):
6.StLib 是否有必要增加多維數(shù)組類?
ISSUE 6——StLib 是否有必要增加多維數(shù)組類?:
多維數(shù)組的本質(zhì):數(shù)組的數(shù)組!
不需要定義多維數(shù)組!
使用DynamicArray創(chuàng)建多維數(shù)組:
#include <iostream> #include "DynamicArray.h"using namespace std; using namespace StLib;int main() {DynamicArray< DynamicArray<int> > d;d.resize(3);for(int i=0; i<d.length(); i++){d[i].resize(i + 1);}for(int i=0; i<d.length(); i++){for(int j=0; j<d[i].length(); j++){d[i][j] = i * j;}}for(int i=0; i<d.length(); i++){for(int j=0; j<d[i].length(); j++){cout << d[i][j] << " ";}cout << endl;}return 0; }運(yùn)行結(jié)果為:
0 0 1 0 2 4實(shí)踐經(jīng)驗(yàn):
- 是軟件就有bug,因此需要不停的迭代升級,解決問題。庫是一種特殊的軟件產(chǎn)品,也會存在各種bug,也需要迭代升級,解決問題。
轉(zhuǎn)載于:https://www.cnblogs.com/PyLearn/p/10123468.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的数据结构开发(7):典型问题分析(Bugfix)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 借记卡是什么意思?借记卡功能盘点
- 下一篇: 搭建集群架构