数据结构之单链表实现队列C++

#ifndef STL_SDFLSJ_H
#define STL_SDFLSJ_H

/************************************************************************/
/* 单链表实现队列C++
/************************************************************************/

/************************************************************************/
/* 节点类
/************************************************************************/
//结点定义
template <class Elemplent>
class CNode
{
public:
	Elemplent data;
	CNode<Elemplent> *next;
public:
	CNode<Elemplent> ();
	CNode(Elemplent tempElemplent,CNode<Elemplent>* tempNext = NULL); 
	~CNode();
};

//结点构造函数
template<class Elemplent>
CNode<Elemplent>::CNode()
{
	next = NULL;
}

//结点析构函数
template<class Elemplent>
CNode<Elemplent>::~CNode()
{

}

//创建新节点函数
template<class Elemplent>
CNode<Elemplent>::CNode(Elemplent tempElemplent,CNode<Elemplent>* tempNext )
{
	data = tempElemplent;
	next = tempNext;
}

/************************************************************************/
/* 链表队列类定义
/************************************************************************/

template<class Elemplent>
class ListSequence
{
protected:
	CNode<Elemplent> *front,*rear;
	int CountTotal;
public:
	ListSequence();
	~ListSequence();
	ListSequence(const ListSequence& tempptr);
	ListSequence<Elemplent>& operator = (const ListSequence& tempptr);
	void InitListSequence();
public:
	bool IsEmpty();
	void Clear();
	int GetLength()const;
	bool Insert(const Elemplent & tempElemplent);
	bool Delete();
}

template<class Elemplent>
ListSequence<Elemplent>::ListSequence()
{
	InitListSequence();
}
template<class Elemplent>
ListSequence<Elemplent>::~ListSequence()
{
	delete front;
};
template <class Elemplent>
void ListSequence<Elemplent>::Clear()
{
	while(IsEmpty()!)
	{
		Delete();
	}
};

template <class Elemplent>
void ListSequence<Elemplent>::InitListSequence()
{
	front = new CNode<Elemplent>;
	rear = front;
	CountTotal = 0;
}

template <class Elemplent>
int ListSequence<Elemplent>::GetLength()
{
	return CountTotal;
}

template <class Elemplent>
bool ListSequence<Elemplent>::IsEmpty()
{
	return front == rear;
}
template <class Elemplent>
bool ListSequence<Elemplent>::Delete()
{
	if (IsEmpty())
	{
		return false;
	}
	CNode<Elemplent> *temptr = front->next;
	front->next = temptr->next;
	delete temptr;
	CountTotal--;
	return true;
}
template <class Elemplent>

template <class Elemplent>
bool ListSequence<Elemplent>::Insert(const Elemplent & tempElemplent)
{
	CNode<Elemplent> *newptr = new CNode<Elemplent>(tempElemplent,NULL);
	rear->next = newptr;
	CountTotal++;
	return true;
}

template<class Elemplent>
ListSequence<Elemplent>::ListSequence(const ListSequence& tempptr)
{
	if (tempptr.IsEmpty())
	{
		return *this;
	}
	InitListSequence();
	CNode<Elemplent> * tempcopy = tempptr.front;
	CNode<Elemplent> * tempGet;
	for (tempcopy->next;tempcopy!=NULL;tempcopy = tempcopy->next )
	{
		tempGet =new CNode<Elemplent>;
		tempGet->data = tempcopy->data;
		Insert(tempGet);
	}
	return *this;
}
template<class Elemplent>
ListSequence<Elemplent>& ListSequence<Elemplent>::operator=(const ListSequence& tempptr)
{
	if (tempptr.IsEmpty())
	{
		return *this;
	}
	if (this == tempptr)
	{
		return *this;
	}
	Clear();
	InitListSequence();
	CNode<Elemplent> * tempcopy = tempptr.front;
	CNode<Elemplent> * tempGet;
	for (tempcopy->next;tempcopy!=NULL;tempcopy = tempcopy->next )
	{
		tempGet =new CNode<Elemplent>;
		tempGet->data = tempcopy->data;
		Insert(tempGet);
	}
	return *this;
}


#endif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值