请问这是什么意思呐?没调用push()的时候是没问题的?

http://img2.mukewang.com/5a3d37960001777706090377.jpg


http://img3.mukewang.com/5a3d37980001b83b07830751.jpg


#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;

}


慕斯5158549
浏览 1244回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP