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

chu xue zhe a
ge wei bang bang mang ha

编写一个类,实现简单的栈操作:元素入栈,读出栈顶元素值,退栈,判断栈顶元素是否为空。如果栈溢出,程序终止。栈的数据成员有10个整形数组构成。先后作如下操作:
将10入栈;
将12入栈;
将14入栈;
读出栈顶元素;
退栈;
读出并输出栈顶元素。

#include<iostream>
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<<"栈溢出"<<endl;
return 0;
}
if(!s.push(12))//将12入栈;
{
cout<<"栈溢出"<<endl;
return 0;
}
if(!s.push(14))//将14入栈;
{
cout<<"栈溢出"<<endl;
return 0;
}
cout<<s.gettop()<<endl;//读出并输出栈顶元素;
s.pop();//退栈
cout<<s.gettop()<<endl;//读出并输出栈顶元素;
}
如上类,这是可以存储10个整型数空间的栈。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-23
#include<iostream>
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<<"栈溢出"<<endl;
return 0;
}
if(!s.push(12))//将12入栈;
{
cout<<"栈溢出"<<endl;
return 0;
}
if(!s.push(14))//将14入栈;
{
cout<<"栈溢出"<<endl;
return 0;
}
cout<<s.gettop()<<endl;//读出并输出栈顶元素;
s.pop();//退栈
cout<<s.gettop()<<endl;//读出并输出栈顶元素;
}
如上类,这是可以存储10个整型数空间的栈。
第2个回答  2009-12-14
#include <iostream>
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个回答  2020-04-29
#include<iostream>
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<<"栈溢出"<<endl;
return
0;
}
if(!s.push(12))//将12入栈;
{
cout<<"栈溢出"<<endl;
return
0;
}
if(!s.push(14))//将14入栈;
{
cout<<"栈溢出"<<endl;
return
0;
}
cout<<s.gettop()<<endl;//读出并输出栈顶元素;
s.pop();//退栈
cout<<s.gettop()<<endl;//读出并输出栈顶元素;
}
如上类,这是可以存储10个整型数空间的栈。
相似回答