首页 > IT职业 > IT职场 > 具体实现代码@数据结构探险——栈篇 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 =0;i--){
cout file:MyStack.cpp
file:Coordinate.hpp
#ifndef Coordinate_hpp
#define Coordinate_hpp
#include
using namespace std;
class Coordinate{
public:
friend ostream &operator file:Coordinate.cpp
#ifndef Coordinate_hpp
#define Coordinate_hpp
#include
using namespace std;
class Coordinate{
public:
friend ostream &operator 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 stackLength() stackTraverse(true);
pStack->clearStack();
if(pStack->stackEmpty()){
cout stackFull()){
cout 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2056/viewspace-2799324/ ,如需转载,请注明出处,否则将追究法律责任。