.[題目分析] 題目要求按遞增次序輸出單鏈表中各結點的數據元素並釋放結點所占存儲空間應對鏈表進行遍歷在每趟遍歷中查找出整個鏈表的最小值元素輸出並釋放結點所占空間;再查次最小值元素輸出並釋放空間如此下去直至鏈表為空最後釋放頭結點所占存儲空間當然刪除結點一定要記住該結點的前驅結點的指針
void MiniDelete(LinkedList head)∥head是帶頭結點的單鏈表的頭指針本算法按遞增順序輸出單鏈表中各結點的數據元素並釋放結點所占的存儲空間
{while(head>next!=null)∥循環到僅剩頭結點
{pre=head;∥pre為元素最小值結點的前驅結點的指針
p=pre>next;∥p為工作指針
while(p>next!=null)
{if(p>next>data<pre>next>data)pre=p;∥記住當前最小值結點的前驅
p=p>next;
}
printf(pre>next>data);∥輸出元素最小值結點的數據
u=pre>next;pre>next=u>next; free(u);∥刪除元素值最小的結點釋放結點空間
}∥ while(head>next!=null)
free(head);}∥釋放頭結點
[算法討論] 算法中使用的指針變量只有prep和u三個請讀者細心體會要注意沒特別記最小值結點而是記其前驅
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/23316.html