数据结构开发(7):典型问题分析(Bugfix)
生活随笔
收集整理的這篇文章主要介紹了
数据结构开发(7):典型问题分析(Bugfix)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
0.目錄
1.創建異常對象時的空指針問題
2.LinkList 中的數據元素刪除
3.LinkList 中遍歷操作與刪除操作的混合使用
4.StaticLinkList 中數據元素刪除時的效率問題
5.StaticLinkList 是否需要提供析構函數?
6.StLib 是否有必要增加多維數組類?
1.創建異常對象時的空指針問題
ISSUE 1——創建異常對象時的空指針問題:
改進Exception.cpp:
在init函數中改進strdup的賦值方法
2.LinkList 中的數據元素刪除
ISSUE 2——LinkList 中的數據元素刪除:
單鏈表的實現沒有考慮異常安全性!
改進LinkList.h中的remove函數和clear函數:
3.LinkList 中遍歷操作與刪除操作的混合使用
ISSUE 3——LinkList 中遍歷操作與刪除操作的混合使用:
混合使用導致了m_current指向了已經被刪除的結點,于是程序出錯。
改進LinkList.h中的remove函數:
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; }運行結果為:
4 0 1 2 44.StaticLinkList 中數據元素刪除時的效率問題
ISSUE 4——StaticLinkList 中數據元素刪除時的效率問題:
改進LinkList.h中的destroy函數:
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; }運行結果為:
0 1 2 45.StaticLinkList 是否需要提供析構函數?
ISSUE 5——StaticLinkList 是否需要提供析構函數?:
構造函數與析構函數不會發生多態,于是調用的是父類的析構函數!
改進StaticLinkList.h:
在子類StaticLinkList中實現析構函數:
6.StLib 是否有必要增加多維數組類?
ISSUE 6——StLib 是否有必要增加多維數組類?:
多維數組的本質:數組的數組!
不需要定義多維數組!
使用DynamicArray創建多維數組:
#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; }運行結果為:
0 0 1 0 2 4實踐經驗:
- 是軟件就有bug,因此需要不停的迭代升級,解決問題。庫是一種特殊的軟件產品,也會存在各種bug,也需要迭代升級,解決問題。
轉載于:https://www.cnblogs.com/PyLearn/p/10123468.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的数据结构开发(7):典型问题分析(Bugfix)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 借记卡是什么意思?借记卡功能盘点
- 下一篇: 搭建集群架构