#include"Mystack.h"
#include<iostream>
#include<string>
using namespace std;
Mystack::Mystack(int size)
{
m_isize=size;
m_pbuffer=new char[m_isize];
m_itop=0;
}
Mystack::~Mystack()
{
delete []m_pbuffer;
}
bool Mystack::stackempty()
{
if (0==m_itop)
{
cout<<"栈空"<<endl;
return true;
}
return false;
}
bool Mystack::stackfull()
{
if (m_itop==m_isize)
{
cout<<"栈满"<<endl;
return true;
}
return false;
}
void Mystack::clearstack()
{
m_itop=0;
}
int Mystack::stacklenght()
{
return m_itop;
}
bool Mystack:: push(char &elem)
{
if (stackfull())
{
cout<<"zhan man le !"<<endl;
return false;
}
m_pbuffer[m_itop]=elem;
m_itop++;
return true;
}
// char Mystack::pop()
// {
// if (stackempty())
// {
// throw 1;
// }
// else
// {
// m_itop--;
// return m_pbuffer[m_itop];
// }
// }
bool Mystack:: pop(char&elem)
{
if (stackempty())
{
return false;
}
m_itop--;
elem=m_pbuffer[m_itop];
return true;
}
void Mystack::stacktraverse(bool isfrombuttom/*visit()*/)
{
if (isfrombuttom)
{
for (int i=0;i<m_itop;i++)
cout<<m_pbuffer[i]<<",";
}
else
{
for (int i=m_itop-1;i>=0;i--)
cout<<m_pbuffer[i]<<",";
}
}
#ifndef MYSTACK_H
#define MYSTACK_H
class Mystack
{
public:
Mystack(int size);
~Mystack();
bool stackempty();
bool stackfull();
void clearstack();
int stacklenght();
bool push(char &elem);
bool pop(char&elem);
void stacktraverse(bool isfrombuttom);
private:
char*m_pbuffer;
int m_isize;
int m_itop;
};
#endif
#include "Mystack.h"
#include <iostream>
using namespace std;
int main()
{
Mystack *p=new Mystack(5);
p->push("h");
p->push("e");
p->push("l");
char elem=0;
if(p->stackempty()){}
if(p->stackfull()){}
cout<<p->stacklenght()<<endl;
delete p;
p=NULL;
system("pause");
return 0;
}
相关分类