c++栈 编写一个类实现简单的栈

2024年11月22日 18:08
有3个网友回答
网友(1):

#include
using namespace std;

class Stack
{
public:
Stack()
{
top=-1;
}
bool push(int n)//压栈
{
if(!isfull())
data[++top]=n;
else
return false;
return true;
}
bool pop()//退栈
{
if(!isempty())
top--;
else
return false;
return true;
}
int gettop()//得到栈顶元素
{
return data[top];
}
bool isempty()//判断是否为空
{
return top==-1?true:false;
}
bool isfull()//判断是否已满
{
return top==9?true:false;
}
private:
int data[10];
int top;
};

int main()
{
Stack s;//建立一个栈
if(!s.push(10))//将10入栈;
{
cout<<"栈溢出"< return 0;
}
if(!s.push(12))//将12入栈;
{
cout<<"栈溢出"< return 0;
}
if(!s.push(14))//将14入栈;
{
cout<<"栈溢出"< return 0;
}
cout< s.pop();//退栈
cout<}
如上类,这是可以存储10个整型数空间的栈。

网友(2):

#include
using namespace std;

#define STACKSIZE 100
typedef char DataType;

void Error(char* _pchMessage);

typedef class CStack
{
private:
DataType m_Data[STACKSIZE];
int m_nTop;
public:
CStack();
~CStack();
void InitStack();
int StackEmpty();
int StackFull();
void PushStack(DataType _Temp);
DataType PopStack();
DataType StackTop();
void ShowStack();

}SeqStack;

CStack::CStack()
{
for(int i = 0; i < STACKSIZE; ++i)
{
m_Data[i] = 0;
}
m_nTop = -1;
}

CStack::~CStack()
{
}

void CStack::InitStack()
{
//将顺序栈置空
for(int i = 0; i < STACKSIZE; ++i)
{
m_Data[i] = 0;
}
m_nTop = -1;
}

int CStack::StackEmpty()
{
return m_nTop == -1;
}

int CStack::StackFull()
{
return m_nTop == STACKSIZE - 1;
}

void CStack::PushStack(DataType _Temp)
{
if(STACKSIZE - 1 == m_nTop)
{
Error("Stack overflow.");
return ;
}

m_Data[++m_nTop] = _Temp;
}

DataType CStack::PopStack()
{
if( -1 == m_nTop)
{
Error("Stack underflow.");
return -1;
}

return m_Data[m_nTop--];
}

DataType CStack::StackTop()
{
if( -1 == m_nTop)
{
Error("Stack underflow.");
return -1;
}

return m_Data[m_nTop];
}

void CStack::ShowStack()
{
if(-1 == m_nTop)
{
cout << "此栈为空!" << endl;
}
else
{
for(int i = m_nTop; i > -1; --i)
{
cout << m_Data[i] << '\t' << endl;
}
}
}

int main()
{
SeqStack* pStack= new SeqStack;
pStack->InitStack();

if(pStack->StackEmpty())
{
cout << "此栈已空!" << endl;
}

if(pStack->StackFull())
{
cout << "此栈已满!" << endl;
}

pStack->PushStack('a');
pStack->PushStack('b');
pStack->PushStack('c');
pStack->PushStack('d');
pStack->PushStack('e');

cout << "PopStack = " << pStack->PopStack() << endl;
cout << "StackTop = " << pStack->StackTop() << endl;

pStack->ShowStack();

delete pStack;
pStack = NULL;

return 0;
}

void Error(char* _pchMessage)
{
fprintf(stderr, "Error: %s\n", _pchMessage);
cout << _pchMessage << endl;
}

网友(3):

#include
using
namespace
std;
class
Stack
{
public:
Stack()
{
top=-1;
}
bool
push(int
n)//压栈
{
if(!isfull())
data[++top]=n;
else
return
false;
return
true;
}
bool
pop()//退栈
{
if(!isempty())
top--;
else
return
false;
return
true;
}
int
gettop()//得到栈顶元素
{
return
data[top];
}
bool
isempty()//判断是否为空
{
return
top==-1?true:false;
}
bool
isfull()//判断是否已满
{
return
top==9?true:false;
}
private:
int
data[10];
int
top;
};
int
main()
{
Stack
s;//建立一个栈
if(!s.push(10))//将10入栈;
{
cout<<"栈溢出"<return
0;
}
if(!s.push(12))//将12入栈;
{
cout<<"栈溢出"<return
0;
}
if(!s.push(14))//将14入栈;
{
cout<<"栈溢出"<return
0;
}
cout<s.pop();//退栈
cout<}
如上类,这是可以存储10个整型数空间的栈。