1,先讲栈的操作,特性,再讲如何存储栈的元素,代码如何实现。提及双端栈,注意寻找条件何时栈空,栈满。
const int MAX_SIZE=100;
template
class seqStack
{
public:
seqStack ( ) ;
~seqStack ( );
void Push ( T x );
T Pop ( );
T GetTop ( );
bool Empty ( );
private:
T data[MAX_SIZE];
int top;
}
//俩栈共享空间的声明
const int Stack_Size=100;
template
class BothStack
{
public:
BothStack( );
~BothStack( );
void Push(int i, T x);
T Pop(int i);
T GetTop(int i);
bool Empty(int i);
private:
T data[Stack_Size];
int top1, top2;
};
注意:在用顺序表建立栈的时候检查栈空和栈满
2,链栈,头部做栈顶
3,后缀表达式,扫描字符串,遇到数字进栈,遇到操作符出栈做操作,再入栈
stack a;
string s;char b[99],f=0;
int x=0,q=0;
getline(cin,s);
for(int i = 0;i <s.size();i++)
{ if(s[i]>=‘0’&&s[i]<=‘9’)
x = x*10+s[i]-‘0’;
else if(s[i]==’ '){a.push(x);
x = 0; }
else {b[f]=s[i];
f++;}
}
while(1){
if(a.top()==0)
a.pop();
else break;
}
4,队列,顺序队列首指针指向第一个元素之前的位置,循环队列可避免假溢出的问题
循环队列:将存储队列的数组头尾相接。 队空:frontrear 队满的条件:(rear+1) mod QueueSizefront
rear=(rear+1)% MAXSIZE
front=(front+1) % MAZSIZE