栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

具体实现代码@数据结构探险——栈篇 3-2

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

具体实现代码@数据结构探险——栈篇 3-2

file:MyStack.hpp
#ifndef MyStack_hpp
#define MyStack_hpp

#include 

using namespace std;

template 
class MyStack{
public:
    MyStack(int size);
    ~MyStack();
    bool stackFull();
    bool stackEmpty();
    void clearStack();
    int stackLength();
    bool push(T elem);
    bool pop(T &elem);
    void stackTraverse(bool isFromBottom);
private:
    T *m_pBuffer;
    int m_iSize;
    int m_iTop;

};

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

template 
MyStack::~MyStack(){
    delete []m_pBuffer;
    m_pBuffer = NULL;
}

template 
bool MyStack::stackFull(){
    return m_iTop==m_iSize?true:false;
}

template 
bool MyStack::stackEmpty(){
    return m_iTop==0?true:false;
}

template 
void MyStack::clearStack(){
    m_iTop = 0;
}

template 
int MyStack::stackLength(){
    return m_iTop;
}

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

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

}

template 
void MyStack::stackTraverse(bool isFromBottom){
    if(isFromBottom){
 for(int i =0;i < m_iTop;i++) {
     cout << m_pBuffer[i] << endl;
 }
    }
    else{
 for(int i = m_iTop-1;i >=0;i--){
     cout << m_pBuffer[i] << endl;
 }
    }
}

#endif 
file:MyStack.cpp
file:Coordinate.hpp

#ifndef Coordinate_hpp
#define Coordinate_hpp

#include 
using namespace std;

class Coordinate{
public:
    friend ostream &operator<<(ostream &out,Coordinate &coor);

    Coordinate(int x = 0 ,int y = 0);
    void printCoordinate();

private:
    int m_iX;
    int m_iY;
};

#endif 
file:Coordinate.cpp

#ifndef Coordinate_hpp
#define Coordinate_hpp

#include 
using namespace std;

class Coordinate{
public:
    friend ostream &operator<<(ostream &out,Coordinate &coor);

    Coordinate(int x = 0 ,int y = 0);
    void printCoordinate();

private:
    int m_iX;
    int m_iY;
};

#endif 
file:demo.cpp

#include 
#include 
#include "MyStack.hpp"
#include "Coordinate.hpp"

using namespace std;

int main(void){
    MyStack *pStack = new MyStack(5);

    Coordinate coor1(1,2);
    Coordinate coor2(2,3);
    Coordinate coor3(3,4);
    Coordinate coor4(4,5);
    Coordinate coor5(5,6);

    pStack->push(coor1);
    pStack->push(coor2);
    pStack->push(coor3);
    pStack->push(coor4);
    pStack->push(coor5);

    pStack->stackTraverse(true);

    Coordinate elem(0,0);
    pStack->pop(elem);
    elem.printCoordinate();

    cout << pStack->stackLength() << endl;

    pStack->stackTraverse(true);

    pStack->clearStack();

    if(pStack->stackEmpty()){
 cout << "栈为空" << endl;
    }

    if(pStack->stackFull()){
 cout << "栈为满" << endl;
    }

    delete pStack;
    pStack = NULL;

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/233031.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号