ITPub博客

首页 > IT职业 > IT职场 > C++栈类模板 = =# = =#

C++栈类模板 = =# = =#

IT职场 作者:ofan 时间:2021-09-09 09:00:15 0 删除 编辑

栈类模板实现如下,

  • 栈类模板
#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 
  • 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  stackChar = Stack(5);

        //入栈
        cout  *stackCoor = new Stack(3);
        if (stackCoor == NULL) {
            cout push(c1);
        stackCoor->push(c2);
        stackCoor->push(c3);
        stackCoor->push(c4);

        cout printStack(&printCoor);
        cout printStack(&printCoor, false);
        cout length() pop(c5);
        cout printStack(&printCoor);
        cout length() 
  • 运行结果
    图片描述

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4571/viewspace-2807631/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-01

  • 博文量
    159
  • 访问量
    1163