继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

黄亚彬
关注TA
已关注
手记 3
粉丝 16
获赞 56

栈类模板实现如下,

  • 栈类模板
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
#include <iostream>
#include <string.h>

using namespace std;

template <class T>
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 <class T>
Stack<T>::Stack(int capacity){
    clearStack();
    m_iCapacity = capacity;
    if ((m_pStack = new T[m_iCapacity]) == NULL) {
        throw string("Stack Initialization Failed!");
    }
}

template <class T>
Stack<T>::~Stack(){
    delete []m_pStack;
    m_pStack = NULL;
}

template <class T>
void Stack<T>::clearStack(){
    m_iTop = 0;
    m_iLength = 0;
}

template <class T>
bool Stack<T>::push(T element){
    if (isFull()) {
        return false;
    }
    m_pStack[m_iTop] = element;
    m_iLength++;
    m_iTop++;
    return true;
}

template <class T>
bool Stack<T>::pop(T &element){
    if (isEmpty()) {
        return false;
    }
    m_iLength--;
    m_iTop--;
    element = m_pStack[m_iTop];
    return true;
}

template <class T>
const bool Stack<T>::isEmpty(){
    return m_iTop == 0 ? true : false;
}

template <class T>
const bool Stack<T>::isFull(){
    return m_iTop == m_iCapacity ? true : false;
}

template <class T>
const int Stack<T>::length(){
    return m_iLength;
}

template <class T>
void Stack<T>::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 <iostream>
#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<char> stackChar = Stack<char>(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<Coordinate> *stackCoor = new Stack<Coordinate>(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;
}
  • 运行结果
    图片描述
打开App,阅读手记
3人推荐
发表评论
随时随地看视频慕课网APP