#include<iostream>
#include"MyStack.h"
using namespace std;
int main(){
Coordinate c1(1, 2);
Coordinate c2(1, 3);
Coordinate c3(1, 4);
MyStack *pStack = new MyStack(4);
pStack->push(c1);
pStack->push(c2);
pStack->push(c3);
pStack->stackTraverse();
delete pStack;
pStack = NULL;
return 0;
}
#ifndef COORDINATE_H
#define COORDINATE_H
class Coordinate
{
public:
Coordinate(){}
Coordinate(int x, int y);
void printCoordinate();
private:
int m_iX;
int m_iY;
};
#endif
#include"Coordinate.h"
#include<iostream>
using namespace std;
Coordinate::Coordinate(int x, int y) :m_iX(x), m_iY(y){
}
void Coordinate::printCoordinate(){
cout << "(" << m_iX << "," << m_iY << ")" << endl;
}
#ifndef MYSTACK_H
#define MYSTACK_H
#include"Coordinate.h"
class MyStack
{
public:
MyStack(){}
MyStack(int size);
~MyStack();
bool stackEmpty();
bool stackFull();
void clearStack();
int stackLength();
bool push(Coordinate elem);
bool pop(Coordinate &elem);
void stackTraverse();
private:
Coordinate *m_pBuffer;
int m_iSize;
int m_iTop;
};
#endif
#include "MyStack.h"
#include<iostream>
#include"Coordinate.h"
using namespace std;
MyStack::MyStack(int size){
m_iSize = size;
m_iTop = 0;
m_pBuffer = new Coordinate[size];
}
MyStack::~MyStack(){
delete[] m_pBuffer;
m_pBuffer = nullptr;
}
bool MyStack::stackEmpty(){
if (0==m_iTop)
return true;
return false;
}
bool MyStack::stackFull(){
if (m_iTop == m_iSize)
return true;
return false;
}
void MyStack::clearStack(){
m_iTop = 0;
}
int MyStack::stackLength(){
return m_iTop;
}
bool MyStack::push(Coordinate elem){
if (stackFull())
return false;
m_pBuffer[m_iTop++] = elem;
return true;
}
bool MyStack::pop(Coordinate &elem){
if (stackEmpty())
return false;
m_iTop--;
elem = m_pBuffer[m_iTop];
return true;
}
void MyStack::stackTraverse(){
for (int i = 0; i < m_iTop; i++)
m_pBuffer[i].printCoordinate();
}