翻轉線性表中的元素

#ifndef _NODE_H_
#define _NODE_H_
#include <iostream>
using namespace std;

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

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

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

template<class ElemType>
Node<ElemType>::Node(ElemType e,Node<ElemType>* link)//這裏e前面不用加&
{
	data=e;
	next=link;
}

#endif



#ifndef _SIMPLELINKLIST_H_
#define _SIMPLELINKLIST_H_
#include "Node.h"
#include <iostream>
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();
		~SimpleLinkList();

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

		StatusCode GetElem(int position,ElemType &e)const;
		StatusCode SetElem(int position,const ElemType &e);
		StatusCode Insert(int position,const ElemType &e);
		StatusCode Delete(int position,ElemType &e);
		
		SimpleLinkList(const SimpleLinkList<ElemType> &copy);
		SimpleLinkList<ElemType>& operator = (const SimpleLinkList<ElemType> &copy);

};

template<class ElemType>
SimpleLinkList<ElemType>::SimpleLinkList()
{
	head=new Node<ElemType>;
	Init();
}

template<class ElemType>
void SimpleLinkList<ElemType>::Init()
{
	if(head!=NULL)
		Clear();
}

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

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

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>
StatusCode SimpleLinkList<ElemType>::Insert(int position,const ElemType &e)
{
	if(position<1||position>Length()+1)
		return RANGE_ERROR;
	else
	{
		Node<ElemType> *ptrPrior=GetElemPtr(position-1);
		Node<ElemType> *ptr;
		ptr=new Node<ElemType>(e,ptrPrior->next);
		ptrPrior->next=ptr;
		return SUCCESS;
	}
}

template<class ElemType>
Node<ElemType>* SimpleLinkList<ElemType>::GetElemPtr(int position)const
{
	if(position<0||position>Length())
		return NULL;
	else
	{
		for(Node<ElemType> *ptr=head;position>0;--position,ptr=ptr->next)
			;
		return ptr;
	}
}

template<class ElemType>
StatusCode SimpleLinkList<ElemType>::GetElem(int position,ElemType &e)const
{
	if(position<1||position>Length())
		return RANGE_ERROR;
	else
	{
		e=(GetElemPtr(position))->data;
		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>
SimpleLinkList<ElemType>::SimpleLinkList(const SimpleLinkList<ElemType> &copy)
{
	head=new Node<ElemType>;
	Init();
	for(Node<ElemType> *copyPtr=copy.head->next,*ptr=head->next;copyPtr!=NULL;copyPtr=copyPtr->next,ptr=ptr->next)
	{
		ptr=Node(ptr->data,NULL);
	}
}

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

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

template<class ElemType>
void Reserve(SimpleLinkList<ElemType> &la)
{
	ElemType temp1,temp2;
	for(int i=1;i<=(int)(la.Length()/2);i++)
	{
		la.GetElem(i,temp1);
		la.GetElem(la.Length()-i+1,temp2);
		la.SetElem(i,temp2);
		la.SetElem(la.Length()-i+1,temp1);
	}
}
void main()
{
	int j=1,aElem;
	SimpleLinkList<int> la;
	la.Insert(1,j);
	la.Insert(2,8);
	la.Insert(3,2);
	Reserve(la);
	for(int i=1;i<=la.Length();i++)
	{
		la.GetElem(i,aElem);
		cout<<aElem<<" ";
	}
	cout<<endl;
}


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