C++实现的栈--非STL

栈的实现
包括:动态扩容、删除、遍历、插入元素
模板类 整就完事了~就没用STL了

//栈
//include包括的头文件就是常见的头文件 就不再添加了
template<typename T>
class Stack
{
public:
	Stack()	
	{
		this->Maxsize = 2;
		this->base =  new T[Maxsize];//分配内存
		top = base;
	};
	~Stack() 
	{
		this->Maxsize = 0;
		if (this->base!=NULL)
		{
			this->base = NULL;
			delete [] base;
		}

	};
	void PushElem( T a);
	T PopElem();
	void ShowElem();
	bool Isempty();
	void ClearStack();
	void DestroyStack();
	T GetTopElem();
	T GetLength();
private:
	T * top;
	T * base;
	int Maxsize;

};
template<typename T>
void Stack<T>::PushElem( T a)
{
	if (this->top-this->base==Maxsize)//栈满 动态扩容
	{
		this->Maxsize += Maxsize;
		T *SparePoint = new T[this->Maxsize];
		SparePoint = this->base;
		this->base = NULL;
		delete[] this->base;
		this->base = SparePoint;
		T *CurrPoint= SparePoint;
		while (CurrPoint !=this->top)
		{
			SparePoint = CurrPoint;
			CurrPoint++;
		}
		this->top = CurrPoint;
	}
	*this->top =a;//必须解引用
	++this->top;
}
template<typename T>
T Stack<T>::PopElem()
{
	if (this->base==this->top)
	{
		cout << "无元素可删除" << endl;
		return 0;
	}
	--top;
	return *(top);
}
template<typename T>
void Stack<T>::ShowElem()
{
	if (this->top== this->base)
	{
		cout << "栈内元素为空" << endl;
		return;
	}
	T *CurrPoint=NULL;
	CurrPoint = this->top;
	while (CurrPoint!=this->base)
	{
		CurrPoint--;
		cout << *CurrPoint << " ";
	}
	cout << endl;
}
template<typename T>
bool Stack<T>::Isempty()
{
	return this->base == this->top;
}
template<typename T>
void Stack<T>::ClearStack()
{
	this->base = this->top;
}
template<typename T>
void Stack<T>::DestroyStack()
{
	T *CurrPoint;
	CurrPoint = this->top;
	while (CurrPoint!=this->base)
	{
		CurrPoint--;
		CurrPoint = NULL;
	}
	delete[] this->base;
}
template<typename T>
T Stack<T>::GetTopElem()
{
	T *CurrPoint;
	CurrPoint = this->top;
	return *(--CurrPoint);
}
T Stack<T>::GetLength()
{
	return this->top - this->base;
}
void test09()
{
	Stack<int>S;
	S.PushElem(1);
	S.PushElem(2);
	S.PushElem(3);
	S.PushElem(4);
	cout<<"栈顶元素为"<<S.GetTopElem()<<endl;
	cout << "栈内元素为:" << endl;
	S.ShowElem();
	S.PopElem();
	cout << "删除栈顶元素之后的栈为:" << endl;
	cout << "栈的长度为" << S.GetLength()<< endl;
	S.ShowElem();
	S.ClearStack();
	S.ShowElem();
	

}

int main()
{
	cout << "正在测试我的代码。。" << endl;
	srand((unsigned int)time(NULL));
	test09();
	system("pause");
}
-------------------------------------------
//以栈来完成2进制到十进制的转换函数:
int Getnumber(int a)
{
	return a-48;//将字符的ASCII码转换为数字
}
int Binary2Ten()
{
	cout << "您想输入的待转换的值,以#结束" << endl;
	string a;//输入字符串
	cin >> a;
	int index = 0;
	Stack<int>N;

	while (a[index]!='#')
	{	
		N.PushElem(Getnumber(a[index]));
		index++;
	}
	int PopTimes = 0;
	int len = N.GetLength();
	int Number = 0;
	while (PopTimes != len)
	{
		Number += (N.PopElem()) *(pow(2, PopTimes));
		PopTimes++;
	}
	cout << "转换后的结果为:" << Number << endl;
	return Number;
}
int main()
{
	cout << "正在测试我的代码。。" << endl;
	srand((unsigned int)time(NULL));
	Binary2Ten();
	system("pause");
}

你可能感兴趣的:(翔改的数据结构之路~,数据结构,c++,栈)