数据结构——栈篇(利用模板生成栈的模板类)

本文主要利用栈技术实现了栈的模板类,利用模板类实例化各种数据类型作为栈元素包括:类、字符型等。

主要代码如下,有不足之处请各位指正,谢谢!

首先建立一个Coordinate类:

Coordinate.h:

#ifndef COORDINATE_H_
#define COORDINATE_H_
#include<iostream>
#include<string>
using namespace std;
class Coordinate
{
public:
	Coordinate(int x=0,int y=0);
	~Coordinate();
	friend ostream & operator<<(ostream &os, const Coordinate  & temp);
private:
	int m_ix;
	int m_iy;

};
#endif

Coordinate.cpp:

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

Coordinate::Coordinate(int x, int y)
{
	m_ix = x;
	m_iy = y;
}
Coordinate:: ~Coordinate()
{
}
ostream & operator<<(ostream &os, const Coordinate  & temp)
{
	os << '('<<temp.m_ix <<','<< temp.m_iy<<')' << endl;
	return os;
}

建立栈模板,Mystack.h文件:

#ifndef MYSTACK_H_
#define MYSTACK_H_
#include<iostream>
#include<string>
#include"Mystack.h"
//#include"Customer2.h"
template<typename T>

class Mystack
{
public:
	Mystack(int size);
	virtual ~Mystack();
	bool stackFull();
	bool stackEmpty();
	void clearStack();
	void pushStack(T const elem);
	void popStack(T &elem);
	int stackLength();
	void stackTraverse();
private:
	int m_isize;
	int top_stack;
	T *m_p;
};
template<typename T>
Mystack<T>::Mystack(int size)
{
	m_isize = size;
	m_p = new T [m_isize];
	top_stack=0;
}
template<typename T>
Mystack<T>::~Mystack()
{
	delete []m_p;
	m_p = NULL;
}
template<typename T>
bool Mystack<T>::stackFull()
{
	if (m_isize == top_stack)
	{
		return true;
	}
	else
	{
		return false;
	}
}
template<typename T>
bool Mystack<T>::stackEmpty()
{
	if (top_stack == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
template<typename T>
void Mystack<T>::clearStack()
{
	top_stack = 0;

}
template<typename T>
void Mystack<T>::pushStack(T const elem)
{
	if (!stackFull())
	{
		m_p[top_stack++] = elem;
	}
}
template<typename T>
void Mystack<T>::popStack(T &elem)
{
	if (stackEmpty())
	{
		cout << "stack is empty!" << endl;
	}
	else
	{
		elem = m_p[--top_stack];
	}
}
template<typename T>
int Mystack<T>::stackLength()
{
	if (stackEmpty())
	{
		return 0;
	}
	else
	{
		return top_stack - 1;
	}
}
template<typename T>
void Mystack<T>::stackTraverse()
{
	for (int i = top_stack-1; i >= 0; i--)
	{
		cout << m_p[i] << endl;
	}
}
#endif

此时模板建立完毕,建立demo来进行测试:

#include<iostream>
#include<string>
#include"Mystack.h"
#include"Coordinate.h"
using namespace std;
int main()
{
	
	cout << "使用类实例化模板:" << endl;
	Mystack<Coordinate> *p = new Mystack<Coordinate>(5);
	Coordinate customer1(1, 21);//实例化对象
	Coordinate customer2(2, 21);
	Coordinate customer3(3, 21);
	Coordinate customer4(4, 21);
	Coordinate customer5(5, 21);
	p->pushStack(customer1);
	p->pushStack(customer2);
	p->pushStack(customer3);
	p->pushStack(customer4);
	p->pushStack(customer5);
	cout << "遍历" << endl;
	p->stackTraverse();
	cout << endl;
	cout << "出栈" << endl;
	Coordinate e;
	p->popStack(e);
	cout << e << endl;
	cout << endl;
	cout << "再次遍历" << endl;
	p->stackTraverse();
	cout << "使用字符类型实例化" << endl;
	p->clearStack();



	Mystack<char> *pp = new Mystack<char>(5);

	pp->pushStack('f');
	pp->pushStack('y');
	pp->pushStack('j');
	pp->pushStack('g');
	pp->pushStack('b');
	cout << "遍历" << endl;
	pp->stackTraverse();
	cout << endl;
	cout << "出栈" << endl;
	char ee;
	pp->popStack(ee);
	cout << ee << endl;
	cout << endl;
	cout << "再次遍历" << endl;
	pp->stackTraverse();
	system("pause");
	return 0;
}

输出如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值