與本題類似的其它題解答如下
(1)[問題分析]與上題類似不同之處在於一是鏈表無頭結點為處理方便給加上頭結點處理結束再刪除之;二是數據相同的結點不合並到結果鏈表中;三是hb鏈表不能被破壞即將hb的結點合並到結果鏈表時要生成新結點
LinkedList Union(LinkedList ha hb)∥ha和hb是兩個無頭結點的數據域值遞增有序的單鏈表本算法將hb中並不出現在ha中的數據合並到ha中合並中不能破壞hb鏈表
{LinkedList la;
la=(LinkedList)malloc(sizeof(LNode));
la>next=ha;∥申請頭結點以便操作
pa=ha;∥pa是ha鏈表的工作指針
pb=hb;∥pb是hb鏈表的工作指針
pre=la;∥pre指向當前待合並結點的前驅
while(pa&&pb)
if(pa>data<pb>data)∥處理ha中數據
{pre>next=pa;pre=pa;pa=pa>next;}
else if(pa>data>pb>data)∥處理hb中數據
{r=(LinkedList)malloc(sizeof(LNode));∥申請空間
r>data=pb>data; pre>next=r;
pre=r;∥將新結點鏈入結果鏈表
pb=pb>next;∥hb鏈表中工作指針後移
}
else∥處理pa>data=pb>data;
{pre>next=pa; pre=pa;
pa=pa>next;∥兩結點數據相等時只將ha的數據鏈入
pb=pb>next;∥不要hb的相等數據
}
if(pa!=null)pre>next=pa;∥將兩鏈表中剩余部分鏈入結果鏈表
else pre>next=pb;
free(la);∥釋放頭結點hahb指針未被破壞
}∥算法nion結束
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/23358.html