.[題目分析] 本題要求在單鏈表中刪除最小值結點單鏈表中刪除結點為使結點刪除後不出現斷鏈應知道被刪結點的前驅而最小值結點是在遍歷整個鏈表後才能知道所以算法應首先遍歷鏈表求得最小值結點及其前驅遍歷結束後再執行刪除操作
LinkedList Delete(LinkedList L)∥L是帶頭結點的單鏈表本算法刪除其最小值結點
{p=L>next;∥p為工作指針指向待處理的結點假定鏈表非空
pre=L;∥pre指向最小值結點的前驅
q=p;∥q指向最小值結點初始假定第一元素結點是最小值結點
while(p>next!=null)
{if(p>next>data<q>data){pre=p;q=p>next;}∥查最小值結點
p=p>next;∥指針後移
}
pre>next=q>next;∥從鏈表上刪除最小值結點
free(q);∥釋放最小值結點空間
}∥結束算法delete
[算法討論] 算法中函數頭是按本教材類C描述語言書寫的原題中void delete(linklist &L)是按C++的引用來寫的目的是實現變量的傳址克服了C語言函數傳遞只是值傳遞的缺點
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/23342.html