C++栈实践
来源:互联网 发布:淘宝会员名有什么用处 编辑:程序博客网 时间:2024/06/04 19:59
#include<iostream>
class SqStack
{
private:
enum{ MaxSize = 100 };
int data[MaxSize];
int top;
public:
SqStack();
~SqStack();
bool isEmpty();
void pushInt(int x);
int popInt();
int getTop();
int getMax();
void display();
};
SqStack::SqStack()
{
top = -1;
}
SqStack::~SqStack(){}
bool SqStack::isEmpty()//判断栈为空
{
return(top == -1);
}
void SqStack::pushInt(int x)//元素进栈
{
if (top == MaxSize - 1)
{
std::cout << "栈上溢出!" << std::endl;
}
else
{
++top;
data[top] = x;
}
}
int SqStack::popInt()//退栈
{
int tmp = 0;
if (top == -1)
{
std::cout << "栈已空!" << std::endl;
}
else
{
tmp = data[top--];
}
return tmp;
}
int SqStack::getTop()//获得栈顶元素
{
int tmp = 0;
if (top == -1)
{
std::cout << "栈空!" << std::endl;
}
else
{
tmp = data[top];
}
return tmp;
}
void SqStack::display()//打印栈里元素
{
std::cout << "栈中元素:" << std::endl;
for (int index = top; index >= 0; --index)
{
std::cout << data[index] << std::endl;
}
}
int SqStack::getMax()//获取最大值
{
int tmp = data[0];
for (int index = top; index > 0; --index)
{
if (data[index] > tmp)
{
tmp = data[index];
}
}
return tmp;
}
int main()
{
SqStack st;
std::cout << "栈空:" << st.isEmpty() << std::endl;
for (int i = 1; i<10; i++)
{
st.pushInt(i);
}
st.display();
std::cout<<"最大值:"<<st.getMax()<<std::endl;
std::cout << "退一次栈" << std::endl;
st.popInt();
std::cout << "栈顶元素:" << st.getTop() << std::endl;
st.popInt();
st.display();
system("pause");
return 0;
}
class SqStack
{
private:
enum{ MaxSize = 100 };
int data[MaxSize];
int top;
public:
SqStack();
~SqStack();
bool isEmpty();
void pushInt(int x);
int popInt();
int getTop();
int getMax();
void display();
};
SqStack::SqStack()
{
top = -1;
}
SqStack::~SqStack(){}
bool SqStack::isEmpty()//判断栈为空
{
return(top == -1);
}
void SqStack::pushInt(int x)//元素进栈
{
if (top == MaxSize - 1)
{
std::cout << "栈上溢出!" << std::endl;
}
else
{
++top;
data[top] = x;
}
}
int SqStack::popInt()//退栈
{
int tmp = 0;
if (top == -1)
{
std::cout << "栈已空!" << std::endl;
}
else
{
tmp = data[top--];
}
return tmp;
}
int SqStack::getTop()//获得栈顶元素
{
int tmp = 0;
if (top == -1)
{
std::cout << "栈空!" << std::endl;
}
else
{
tmp = data[top];
}
return tmp;
}
void SqStack::display()//打印栈里元素
{
std::cout << "栈中元素:" << std::endl;
for (int index = top; index >= 0; --index)
{
std::cout << data[index] << std::endl;
}
}
int SqStack::getMax()//获取最大值
{
int tmp = data[0];
for (int index = top; index > 0; --index)
{
if (data[index] > tmp)
{
tmp = data[index];
}
}
return tmp;
}
int main()
{
SqStack st;
std::cout << "栈空:" << st.isEmpty() << std::endl;
for (int i = 1; i<10; i++)
{
st.pushInt(i);
}
st.display();
std::cout<<"最大值:"<<st.getMax()<<std::endl;
std::cout << "退一次栈" << std::endl;
st.popInt();
std::cout << "栈顶元素:" << st.getTop() << std::endl;
st.popInt();
st.display();
system("pause");
return 0;
}
0 0
- C语言程序设计实践-C语言应用实践
- C 编程最佳实践
- C 编程最佳实践
- C 编程最佳实践
- C#编码实践[转]
- C 编程最佳实践
- C 编程最佳实践
- C 编程最佳实践
- C编程最佳实践
- C 编程最佳实践
- C 编程最佳实践
- C 编程最佳实践
- C语言实践总结
- C 编程最佳实践
- C 编程最佳实践
- C++Test单元测试实践
- c程序设计实践-1
- C语言实践总结
- AOP Spring
- android-percent-support-lib-sample
- SQL表复制
- Bentley STAAD.Pro V8i SS6 v20.07.11.33 1CD(土木結構分析)
- [转载] 尺度不变特征变换匹配算法详解
- C++栈实践
- 希尔排序堆排序和基数排序
- COPRA.RF.v2005.SR1-ISO 1CD(设计检验轧辊的虚拟现实的集成软件)
- 影像去条带噪声
- Android System Property
- Engineering Equation Solver(EES) v7.171 1CD(工程方程求解器)
- GIT初学文章推荐
- 黑马程序员——Java基础---io流
- 切换imageview当中的图片