已知两个链表 la和 lb,其元素值递增排序。编程将la和lb合并成一个递减有序(相同值元素只保留一个)的链表lc。(北方名校经典试题) 本题选做

该程序实现将两个递增排序的链表la和lb合并为一个递减有序且无重复元素的链表lc。通过遍历并比较链表元素,将元素插入lc,并确保lc中元素唯一。
摘要由CSDN通过智能技术生成
#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+
  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值