数据结构——栈篇(利用模板生成栈的模板类)
程序员文章站
2022-06-01 11:28:54
...
本文主要利用栈技术实现了栈的模板类,利用模板类实例化各种数据类型作为栈元素包括:类、字符型等。
主要代码如下,有不足之处请各位指正,谢谢!
首先建立一个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;
}
输出如下: