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

#include //compare.cpp
#include "Node.h"
#include "SimpleLinkList.h"
using namespace std;

template
void oppositedSequence(SimpleLinkList &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
void MergeList(SimpleLinkList &la,SimpleLinkList &lb,SimpleLinkList &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++;	
		
			
	}
	while(aPosition<=aLength)
	{
		la.GetElem(aPosition,aItem);
		if(lc.Exist(aItem))
		{
			aPosition++;
			continue;
		}
		lc.Insert(lc.Length()+1,aItem);
		aPosition++;
	}
	while(bPosition<=bLength)
	{
		lb.GetElem(bPosition,bItem);
		if(lc.Exist(bItem))
		{
			bPosition++;
			continue;
		}
		lc.Insert(lc.Length()+1,bItem);
		bPosition++;
	}
	//逆序排列
	oppositedSequence(lc);
}
void main()
{
	SimpleLinkList la,lb,lc;
	for(int i=1,k=2;i<3;i++,k++)//定义la和lb为递增排列的链表
	{
		la.Insert(i,i);
		lb.Insert(i,k);
	}
//	lb.Insert(3,5);
	la.Insert(3,7);
	MergeList(la,lb,lc);
	for(int j=1;j<=lc.Length();j++)
	{
		int cTemp;
		lc.GetElem(j,cTemp);
		cout<
using namespace std;

template
struct Node
{
	ElemType data;
	Node *next;
	Node();
	Node(ElemType item,Node *link=NULL);
};

template
Node::Node()
{
	next=NULL;
}

template
Node::Node(ElemType item,Node *link)
{
	data=item;
	next=link;
}
#endif

#ifndef _SIMPLELINKLIST_H_//SimpleLinkList.h
#define _SIMPLELINKLIST_H_
#include "Node.h"
#include 
using namespace std;
enum StatusCode{SUCCESS,FAIL,UNDER_FLOW,RANGE_ERROR,DUPLICATE_ERROR,NOT_PRESENT,ENTRY_INSERTED,ENTRY_FOUND,VISITED,UNVISITED};
template
class SimpleLinkList
{
protected:
	Node *head;
	Node *GetElemPtr(int position)const;
	void Init();
public:
	
	SimpleLinkList();
	virtual ~SimpleLinkList();
	int Length()const;
	bool Empty()const;
	void Clear();

	StatusCode GetElem(int position,ElemType &e)const;
	StatusCode SetElem(int position,ElemType e);
	StatusCode Delete(int position,ElemType &e);
	StatusCode Insert(int position,ElemType e);

	SimpleLinkList(const SimpleLinkList ©);
	SimpleLinkList operator = (const SimpleLinkList ©);
	bool Exist(ElemType &e);
};

template
bool SimpleLinkList::Exist(ElemType &e)
{
	for(Node* tmpPtr=head;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
	{
		if(tmpPtr->data==e)
			return true;
	}
	return false;
		
}
template
Node *SimpleLinkList::GetElemPtr(int position)const
{
	Node *tmpPtr=head;
	int curPosition=0;
	while(tmpPtr!=NULL&&curPositionnext;
		curPosition++;
	}
	if(tmpPtr!=NULL&&curPosition==position)
		return tmpPtr;
	else
		return NULL;
		
}
//实现
template
SimpleLinkList::SimpleLinkList()
{
	Init();
}

template
void SimpleLinkList::Init()
{
	head=new Node;
}

template
SimpleLinkList::~SimpleLinkList()
{
	Clear();
	delete head;
}

template
int SimpleLinkList::Length()const
{
	int count=0;
	for(Node *tmpPtr=head->next;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
		count++;
	return count;
}

template
void SimpleLinkList::Clear()
{
	ElemType tmpElem;
	while(Length()>0)
		Delete(1,tmpElem);
}

template
bool SimpleLinkList::Empty()const
{
	return head->next==NULL;
}

template
StatusCode SimpleLinkList::GetElem(int position,ElemType &e)const
{
	if(position<1||position>Length())
		return RANGE_ERROR;
	else
	{
		e=((GetElemPtr(position))->data);
		return ENTRY_FOUND;
	}
}

template
StatusCode SimpleLinkList::SetElem(int position,ElemType e)
{
	if(position<1||position>Length())
		return RANGE_ERROR;
	else
	{
		(GetElemPtr(position))->data=e;
		return SUCCESS;
	}
}

template
StatusCode SimpleLinkList::Insert(int position,ElemType e)
{
	if(position<1||position>Length()+1)
		return RANGE_ERROR;
	else
	{
		Node *tmpPtr=GetElemPtr(position-1);
		Node *s=new Node(e,tmpPtr->next);
		tmpPtr->next=s;
		return SUCCESS;
	}

}

template
StatusCode SimpleLinkList::Delete(int position,ElemType &e)
{
	if(position<1||position>Length())
		return RANGE_ERROR;
	else
	{
		Node *tmpPtr=GetElemPtr(position-1);
		Node *deletePtr=tmpPtr->next;
		tmpPtr->next=deletePtr->next;
		e=deletePtr->data;
		delete deletePtr;
		return SUCCESS;
	}
}

template
SimpleLinkList::SimpleLinkList(const SimpleLinkList &e)
{
	for(int i=1;i<=e.Length();i++)
	{
		ElemType eElem;
		e.GetElem(i,eElem);
		Insert(i,eElem);
	}
}

template
SimpleLinkList SimpleLinkList::operator = (const SimpleLinkList &e)
{
	SimpleLinkList *a=new SimpleLinkList(e);
	return a;
}
#endif

你可能感兴趣的:(数据结构作业)