数据结构之单链表实现栈(C++)

#ifndef STL_LINK_SEQUENCELIST_H
#define STL_LINK_SEQUENCELIST_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;
}

/************************************************************************/
/*  链表类实现
/************************************************************************/

enum Status
{
	EMPTY,
	HAVE,
	FULL
};
template <class Elemplent>
class SquenceList
{
protected:
	int CountTotal;
	CNode<Elemplent> *topPtr;
public:
	SquenceList();
	~SquenceList();
	void InitSquenceList();
	SquenceList(const SquenceList<Elemplent>& tempptr);
	SquenceList<Elemplent>& operator = (const SquenceList<Elemplent>& tempptr);
public:
	int GetLength();
	bool IsEmpty();
	bool Clear();
	bool GetTop(Elemplent & temp);
	bool Push(const Elemplent& temp);
	bool Pop();
};
template <class Elemplent>
SquenceList<Elemplent>::SquenceList()
{
	InitSquenceList();
}

template<class Elemplent>
void SquenceList<Elemplent>::InitSquenceList()
{
	CountTotal = 0;
	topPtr = new CNode<Elemplent>;
}

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

template <class Elemplent>
bool SquenceList<Elemplent>::IsEmpty()
{
	return topPtr ==NULL;
}

template <class Elemplent>
bool SquenceList<Elemplent>::GetTop(Elemplent & temp)
{
	if (IsEmpty())
	{
		return false;
	}
	temp = topPtr->data;
	return true;
}

template <class Elemplent>
bool SquenceList<Elemplent>::Push(const Elemplent& temp)
{
	//it's fault.
	//CNode<Elemplent> * newptr =new CNode<Elemplent>(temp,NULL);
	CNode<Elemplent>* newptr = new CNode<Elemplent> (temp,topPtr);
	topPtr= newptr;
	CountTotal ++;
	return true;
}

template<class Elemplent>
bool SquenceList<Elemplent>::Pop()
{
	if (IsEmpty())
	{
		return false;
	}
	CNode<Elemplent> * deleteptr =topPtr;
	topPtr = topPtr->next;
	delete deleteptr;
	CountTotal--;
	return true;
}

template<class Elemplent>
bool SquenceList<Elemplent>::Clear()
{
	while(IsEmpty()!)
	{
		Pop();
	}
	return false
}
template<class Elemplent>
SquenceList<Elemplent>::SquenceList(const SquenceList<Elemplent>& tempptr)
{
	int length =tempptr.GetLength;
	Clear();
	InitSquenceList();
	CNode<Elemplent> *curptr = tempptr.topPtr;
	//因为是FILO所以,为了复制,必要倒叙一下,
	for (int i = 1;i<=length;i++)
	{
		for (int j = 1;j<=length-i;j++)
		{
			curptr =curptr->next;
		}
		topPtr->data = curptr->next;
		curptr = tempptr.topPtr;
	}
	return *this;
}

template <class Elemplent>
SquenceList<Elemplent>& SquenceList<Elemplent>::operator=(const SquenceList<Elemplent>& tempptr)
{
	if (this == &tempptr)
	{
		return *this;
	}
	int length =tempptr.GetLength;
	Clear();
	InitSquenceList();
	CNode<Elemplent> *curptr = tempptr.topPtr;
	//因为是FILO所以,为了复制,必要倒叙一下,
	for(int i = 1;i<=length;i++)
	{
		for (int j = 1;j<=length-i;j++)
		{
			curptr =curptr->next;
		}
		topPtr->data = curptr->next;
		curptr = tempptr.topPtr;
	}
	return this;
}



#endif

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值