#include <iostream>//compare.cpp
#include "Node.h"
#include "SimpleLinkList.h"
using namespace std;
template<class ElemType>
void oppositedSequence(SimpleLinkList<ElemType> &lc)
{
int atemp;
int btemp;
int length=lc.Length();
for(int i=1;i<=(int)(length/2);i++)
{
lc.GetElem(i,atemp);
lc.GetElem(length-i+1,btemp);
lc.SetElem(i,btemp);
lc.SetElem(length-i+1,atemp);
}
}
template<class ElemType>
void MergeList(SimpleLinkList<ElemType> &la,SimpleLinkList<ElemType> &lb,SimpleLinkList<ElemType> &lc)
{
int aLength=la.Length(),bLength=lb.Length(),aPosition=1,bPosition=1;
ElemType aItem,bItem,cItem;
while(aPosition<=aLength&&bPosition<=bLength)
{
la.GetElem(aPosition,aItem);
lb.GetElem(bPosition,bItem);
if((!lc.Exist(aItem))&&(!lc.Exist(bItem)))
{
if(aItem<=bItem)
{
lc.Insert(lc.Length()+1,aItem);
lc.Insert(lc.Length()+1,bItem);
}
else
{
lc.Insert(lc.Length()+1,bItem);
lc.Insert(lc.Length()+1,aItem);
}
}else if(lc.Exist(aItem)&&lc.Exist(bItem))
{
}else if(lc.Exist(aItem))
{
lc.Insert(lc.Length()+1,bItem);
}
else
{
lc.Insert(lc.Length()+1,aItem);
}
aPosition++;
bPosition++;
已知两个链表 la和 lb,其元素值递增排序。编程将la和lb合并成一个递减有序(相同值元素只保留一个)的链表lc。(北方名校经典试题) 本题选做
最新推荐文章于 2023-06-05 19:59:46 发布
该程序实现将两个递增排序的链表la和lb合并为一个递减有序且无重复元素的链表lc。通过遍历并比较链表元素,将元素插入lc,并确保lc中元素唯一。

4847

被折叠的 条评论
为什么被折叠?



