赞
踩
栈的原则是先进后出,操作类似于链表。写程序本身就是规则制定的过程,我们是规则的制定者,而栈、队列这些东西都是规则下的产物。
#include <iostream> using namespace std; typedef int DataType; const int MaxSize = 50;//定义顺序栈的存储容量 class SeqStack{ int top; DataType data[MaxSize]; public: SeqStack();//构造函数 ~SeqStack();//析构函数 bool IsEmpty();//判空 bool IsFull();//判满 void Push(DataType e);//入栈 DataType Pop();//出栈 DataType GetTop();//栈顶元素 void Clear();//清空 }; SeqStack::SeqStack(){ top = -1; } SeqStack::~SeqStack(){ } bool SeqStack::IsFull(){ return (top + 1) == MaxSize; } bool SeqStack::IsEmpty(){ return top == -1; } void SeqStack::Clear(){ top = -1; } void SeqStack::Push(DataType e){ if(!IsFull()){ data[++top] = e; }else{ cout << "栈满!!!" << endl; exit(0); } } DataType SeqStack::Pop(){ if(!IsEmpty()){ return data[top--]; }else{ cout << "栈空!!!" << endl; exit(0); } } DataType SeqStack::GetTop(){ if(IsEmpty()){ cout << "栈空!!!" << endl; exit(0); }else{ return data[top]; } } //实现倒序输出 int main(){ SeqStack s; int a[] = { 0,1,2,3,4,5,6,7,8,9}; int len = sizeof(a)/sizeof(a[0]); for(int i &#
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。