泛型编程C++实现栈

泛型编程C++实现栈

Coordinate.h 头文件

#include <iostream>
#include <ostream>
using namespace std;

class Coordinate
{
    friend ostream &operator<<(ostream &out, Coordinate &coor);
public:
    Coordinate(int x = 0, int y = 0);
    ~Coordinate();
    void printCoordinate();

private:
    int m_iX;
    int m_iY;
};

Coordinate::Coordinate(int x, int y)
{
    m_iX = x;
    m_iY = y;
}

Coordinate::~Coordinate()
{

}

void Coordinate::printCoordinate()
{
    cout << "(" << m_iX << "," << m_iY << ")" << endl;
}

ostream &operator<<(ostream &out, Coordinate &coor)
{
    out << "(" << coor.m_iX << "," << coor.m_iY << ")" << endl;
    return out;
}

MyStack.h 头文件

#ifndef MYSTACK_H
#define MYSTACK_H
#include <iostream>
using namespace std;

template <typename T>
class MyStack
{
public:
    MyStack(int size);          //分配内存初始化空间,设定栈容量,栈顶
    ~MyStack();                    //回收栈空间内存
    bool stackEmpty();          //判定栈是否为空,为空返回true,非空返回false
    bool stackFull();           //判定栈是否为满,为满返回true,不满返回false
    void clearStack();          //清空栈
    int stackLength();          //已有元素的个数
    bool push(T elem);          //元素入栈,栈顶上升
    bool pop(T &elem);          //元素出栈,栈顶下降
    void stackTraverse(bool isFromButtom);      //遍历栈中所有元素

private:
    T *m_pBuffer;               //栈空间指针
    int m_iSize;                //栈容量
    int m_iTop;                 //栈顶,栈中元素个数

};

template <typename T>
MyStack<T>::MyStack(int size)
{
    m_iSize = size;
    m_pBuffer = new T[size];
    m_iTop = 0;
}

template <typename T>
MyStack<T>::~MyStack()
{
    delete[]m_pBuffer;
    m_pBuffer = NULL;
}

template <typename T>
bool MyStack<T>::stackEmpty()
{
    if (0 == m_iTop)
    {
        return true;
    }
    else
    {
        return false;
    }
}

template <typename T>
bool MyStack<T>::stackFull()
{
    if (m_iTop == m_iSize)
    {
        return true;
    }
    else
    {
        return false;
    }
}

template <typename T>
void MyStack<T>::clearStack()
{
    m_iTop = 0;
}

template <typename T>
int MyStack<T>::stackLength()
{
    return m_iTop;
}

template <typename T>
bool MyStack<T>::push(T elem)
{
    if(!stackFull())
    {
        m_pBuffer[m_iTop] = elem;
        m_iTop++;
        return true;
    }
    else
    {
        return false;
    }
}

template <typename T>
bool MyStack<T>::pop(T &elem)
{
    if (!stackEmpty())
    {   
        m_iTop--;
        elem = m_pBuffer[m_iTop];
        return true;
    }
    else
    {
        return false;
    }
}

template <typename T>
void MyStack<T>::stackTraverse(bool isFromButtom)
{
    if (isFromButtom)
    {
        for (size_t i = 0; i < m_iTop; i++)
        {
            cout << m_pBuffer[i];
        }
    }
    else 
        for (size_t i = m_iTop -1; i > 0; i--)
        {
            cout << m_pBuffer[i];
        }
}

#endif MYSTACK_H

main.cpp 主函数

#include "MyStack.h"
#include "Coordinate.h"

int main()
{
    MyStack<char> *pStack = new MyStack<char>(5);

    pStack->push('h');
    pStack->push('e');
    pStack->push('l');
    pStack->push('l');
    pStack->push('o');

    pStack->stackTraverse(true);

    cout << endl;

    MyStack<Coordinate> *p = new MyStack<Coordinate>(5);

    p->push(Coordinate(1, 2));
    p->push(Coordinate(3, 4));
    p->push(Coordinate(5, 6));

    p->stackTraverse(true);

    delete p;
    p = NULL;
    delete pStack;
    pStack = NULL;

    system("pause");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值