.[題目分析]本組題有個本質上都是鏈表的合並操作合並中有各種條件與前組題不同的是敘述上是用線性表代表集合而操作則是求集合的並交差(A∪BA∩BAB)等本題與上面.()基本相同不同之處.()中鏈表是非遞減有序(可能包含相等元素)本題是元素遞增有序(不准有相同元素)因此兩表中合並時如有元素值相等元素則應刪掉一個
LinkedList Union(LinkedList hahb)∥線性表A和B代表兩個集合以鏈式存儲結構存儲元素遞增有序ha和hb分別是其鏈表的頭指針本算法求A和B的並集A∪B仍用線性表表示結果鏈表元素也是遞增有序
{ pa=ha>next;pb=hb>next;∥設工作指針pa和pb
pc=ha;∥pc為結果鏈表當前結點的前驅指針
while(pa&&pb)
if(pa>data<pb>data)
{pc>next=pa;pc=pa;pa=pa>next;}
else if(pa>data>pb>data)
{pc>next=pb;pc=pb;pb=pb>next;}
else∥處理pa>data=pb>data
{pc>next=pa;pc=pa;pa=pa>next;
u=pb;pb=pb>next;free(u);}
if(pa) pc>next=pa;∥ 若ha表未空則鏈入結果表
else pc>next=pb;∥若hb表未空則鏈入結果表
free(hb); ∥釋放hb頭結點
return(ha);
}∥算法Union結束
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/23360.html