線性鏈表之將單調遞增的la和lb中的數據元素按值遞增,將la和lb合併爲新的線性表lc,使lc中的元素仍然單調遞增

#ifndef _SIMPLELINKLIST_H_
#define _SIMPLELINKLIST_H_
#include "Node.h"
using namespace std;

enum StatusCode{SUCCESS,RANGE_ERROR};
template<class ElemType>
class SimpleLinkList
{
	protected:
		Node<ElemType> *head;
		
		void Init();
		Node<ElemType>* GetElemPtr(int position)const;

	public:
		SimpleLinkList();
		virtual ~SimpleLinkList();

		void Clear();
		bool Empty()const;
		int Length()const;
		void Traverse(void(* visit)(ElemType &e))const;

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

		SimpleLinkList(const SimpleLinkList<ElemType> &copy);
		SimpleLinkList<ElemType>& operator = (const SimpleLinkList<ElemType> &copy);
};

template<class ElemType>
SimpleLinkList<ElemType>::SimpleLinkList()
{
	head=new Node<ElemType>;//head必須要再構造函數中賦值
	Init();
}

template<class ElemType>
void SimpleLinkList<ElemType>::Init()
{
	
	if((head->next)!=NULL)
	{
		Clear();
	//	delete head;
	}
		
	//cout<<"init()"<<endl;
}

template<class ElemType>
void SimpleLinkList<ElemType>::Clear()
{
	
	ElemType Elem;
	while(Length()>0)
	{
		//cout<<"length()"<<Length()<<endl;
			Delete(1,Elem);
	}
	
	//cout<<"Clear"<<endl;
}

template<class ElemType>
int SimpleLinkList<ElemType>::Length()const
{
	int count=0;
	Node<ElemType> *ptr=head->next;
	while(ptr!=NULL)
	{
		count++;
		ptr=ptr->next;
	}
	return count;
}

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

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

template<class ElemType>
SimpleLinkList<ElemType>::~SimpleLinkList()
{
	Clear();
	delete head;
}

template<class ElemType>
StatusCode SimpleLinkList<ElemType>::Insert(int position,const ElemType &e)
{
	//cout<<"insert"<<endl;
	if(position<1||position>Length()+1)
		return RANGE_ERROR;
	else
	{
		//cout<<"insert"<<endl;
		Node<ElemType> *ptr=GetElemPtr(position-1);
	//	cout<<"insert"<<endl;
		Node<ElemType> *ptrNext=new Node<ElemType>(e,ptr->next);
		ptr->next=ptrNext;
		return SUCCESS;
	}
}

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

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

template<class ElemType>
void SimpleLinkList<ElemType>::Traverse(void(* visit)(ElemType &e))const
{
	for(Node<ElemType> *ptr=head->next;ptr!=NULL;ptr=ptr->next)
		(* visit)(ptr->data);
}

template<class ElemType>
Node<ElemType>* SimpleLinkList<ElemType>::GetElemPtr(int position)const
{
	//cout<<"GetElemPtr"<<endl;
	if(position<0||position>Length())
		return NULL;		
	else
	{
		for(Node<ElemType> *ptr=head;position>0;--position,ptr=ptr->next)//注意ptr指向head爲第一個位置,ptr—>next爲第二個位置
		{;}
		return ptr;
	}
}

template<class ElemType>
SimpleLinkList<ElemType>::SimpleLinkList(const SimpleLinkList<ElemType> &copy)
{
	Init();
	for(Node<ElemType> *ptrCopy=copy.head->next,Node<ElemType> *ptr=head->next;ptrCopy!=NULL;ptrCopy=ptrCopy->next)
	{
		ptr=new Node(ptrCopy->data,NULL);
		ptr=ptr->next;
	}
	
}

template<class ElemType>
SimpleLinkList<ElemType>& SimpleLinkList<ElemType>::operator = (const SimpleLinkList<ElemType> &copy)
{
	if(&copy!=this)
	{
		Init();
		for(Node<ElemType> *ptrCopy=copy.head->next,Node<ElemType> *ptr=head->next;ptrCopy!=NULL;ptrCopy=ptrCopy->next)
		{
			ptr=new Node(ptrCopy->data,NULL);
			ptr=ptr->next;
		}
	}
	return *this;
}

#endif


#ifndef _NODE_H_
#define _NODE_H_
using namespace std;

template<class ElemType>
class Node
{
	public:
		ElemType data;
		Node<ElemType> *next;

		Node();
		Node(ElemType e,Node<ElemType> *link=NULL);
		
};

template<class ElemType>
Node<ElemType>::Node()
{
	next=NULL;
}

template<class ElemType>
Node<ElemType>::Node(ElemType e,Node<ElemType> *link)
{
	data=e;
	next=link;
}
#endif

#include<iostream>
#include "SimpleLinkList.h"
#include "Node.h"
using namespace std;

template<class ElemType>
void Merg(SimpleLinkList<ElemType> &la,SimpleLinkList<ElemType> &lb,SimpleLinkList<ElemType> &lc)
{
	int aPosition=1,bPosition=1;
	ElemType aElem,bElem;
	while(aPosition<=la.Length()&&bPosition<=lb.Length())
	{
		la.GetElem(aPosition,aElem);
		lb.GetElem(bPosition,bElem);
		if(aElem>bElem)
		{
			bPosition++;
			lc.Insert(lc.Length()+1,bElem);
		}else if(aElem==bElem)
		{
			aPosition++;
			bPosition++;
			lc.Insert(lc.Length()+1,bElem);
			lc.Insert(lc.Length()+1,aElem);
		}else
		{
			aPosition++;
			lc.Insert(lc.Length()+1,aElem);
		}
	}
	while(aPosition<=la.Length())
	{
		la.GetElem(aPosition,aElem);
		aPosition++;
		lc.Insert(lc.Length()+1,aElem);
	}
	while(bPosition<=lb.Length())
	{
		lb.GetElem(bPosition,bElem);
		bPosition++;
		lc.Insert(lc.Length()+1,bElem);
	}
}

void main()
{
	SimpleLinkList<int> la,lb,lc;
	la.Insert(1,5);
	la.Insert(2,6);
	la.Insert(3,8);
	la.Insert(4,9);
	lb.Insert(1,7);
	lb.Insert(2,8);
	lb.Insert(3,9);
	lb.Insert(4,10);
	lb.Insert(5,11);
	Merg(la,lb,lc);
	int a;
	for(int i=1;i<=lc.Length();i++)
	{
		lc.GetElem(i,a);
		cout<<a<<" ";
	}
	cout<<endl;
}


發佈了445 篇原創文章 · 獲贊 27 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章