c++ 模板类实现堆栈实验报告_C++栈类模板 = =# = =#

栈类模板实现如下,

栈类模板

#ifndef STACK_H_INCLUDED

#define STACK_H_INCLUDED

#include

#include

using namespace std;

template

class Stack{

public:

Stack(int capacity);

virtual ~Stack();

//清栈

void clearStack();

//入栈

bool push(T element);

//出栈

bool pop(T &element);

//判空

const bool isEmpty();

//判满

const bool isFull();

//栈长

const int length();

//列栈

void printStack(void(*pFunc)(T), bool fromTop = true);

private:

//栈数组指针

T *m_pStack;

//栈容量

int m_iCapacity;

//栈顶

int m_iTop;

//栈长

int m_iLength;

};

template

Stack::Stack(int capacity){

clearStack();

m_iCapacity = capacity;

if ((m_pStack = new T[m_iCapacity]) == NULL) {

throw string("Stack Initialization Failed!");

}

}

template

Stack::~Stack(){

delete []m_pStack;

m_pStack = NULL;

}

template

void Stack::clearStack(){

m_iTop = 0;

m_iLength = 0;

}

template

bool Stack::push(T element){

if (isFull()) {

return false;

}

m_pStack[m_iTop] = element;

m_iLength++;

m_iTop++;

return true;

}

template

bool Stack::pop(T &element){

if (isEmpty()) {

return false;

}

m_iLength--;

m_iTop--;

element = m_pStack[m_iTop];

return true;

}

template

const bool Stack::isEmpty(){

return m_iTop == 0 ? true : false;

}

template

const bool Stack::isFull(){

return m_iTop == m_iCapacity ? true : false;

}

template

const int Stack::length(){

return m_iLength;

}

template

void Stack::printStack(void (*pFunc)(T), bool fromTop){

if (fromTop) { //从顶

for (int i = (m_iLength - 1); i >= 0; i--) {

pFunc(m_pStack[i]);

}

} else { //从底

for (int i = 0; i < m_iLength; i++) {

pFunc(m_pStack[i]);

}

}

}

#endif // STACK_H_INCLUDED

Coordinate测试类

#ifndef COORDINATE_H_INCLUDED

#define COORDINATE_H_INCLUDED

class Coordinate{

public:

Coordinate();

Coordinate(double x, double y);

virtual ~Coordinate();

double getX();

double getY();

private:

double m_iX;

double m_iY;

};

Coordinate::Coordinate(){

}

Coordinate::Coordinate(double x, double y){

m_iX = x;

m_iY = y;

}

Coordinate::~Coordinate(){

}

double Coordinate::getX(){

return m_iX;

}

double Coordinate::getY(){

return m_iY;

}

#endif // COORDINATE_H_INCLUDED

测试代码

#include

#include "Stack.h"

#include "Coordinate.h"

using namespace std;

void printChar(char o){

cout << o;

}

void printCoor(Coordinate o){

cout << "(" << o.getX() << "," << o.getY() << ")" << endl;

}

int main()

{

try{

cout << "*************char*************" << endl;

Stack stackChar = Stack(5);

//入栈

cout << stackChar.length() << endl;

stackChar.push('h');

stackChar.push('e');

stackChar.push('l');

stackChar.push('l');

stackChar.push('o');

stackChar.push('!'); //栈满插入失败

cout << stackChar.length() << endl;

cout << "--------------------------" << endl;

//打印

stackChar.printStack(&printChar);

cout << endl;

stackChar.printStack(&printChar, false);

cout << endl;

cout << stackChar.length() << endl;

cout << "--------------------------" << endl;

//出栈测试

char e1,e2;

stackChar.pop(e1);

stackChar.pop(e2);

cout << "i'm out : " << e1 << endl;

cout << "i'm out : " << e2 << endl;

stackChar.printStack(&printChar);

cout << endl;

cout << stackChar.length() << endl;

cout << "*************Coordinate*************" << endl;

Stack *stackCoor = new Stack(3);

if (stackCoor == NULL) {

cout << "Point init failed!" << endl;

return 3;

}

//入栈

Coordinate c1 = Coordinate(1,2);

Coordinate c2 = Coordinate(3,4);

Coordinate c3 = Coordinate(5,6);

Coordinate c4 = Coordinate(7,8); //栈满插入失败

stackCoor->push(c1);

stackCoor->push(c2);

stackCoor->push(c3);

stackCoor->push(c4);

cout << "--------------------------" << endl;

//打印

stackCoor->printStack(&printCoor);

cout << endl;

stackCoor->printStack(&printCoor, false);

cout << endl;

cout << stackCoor->length() << endl;

cout << "--------------------------" << endl;

//出栈

Coordinate c5;

stackCoor->pop(c5);

cout << "i'm out : ";

printCoor(c5);

stackCoor->printStack(&printCoor);

cout << stackCoor->length() << endl;

delete stackCoor;

stackCoor = NULL;

} catch (string &e) { //捕获可预知异常

cout << e << endl;

return 2;

} catch (...) { //捕获其他所有未知异常

return 1;

}

return 0;

}

运行结果

2c1abc9594789d8d16733ad6024ae6c6.png

打开App,阅读手记

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值