#include
#include
#include
#define INITSIZE 10
typedef int STD;
typedef struct Stack
{
STD*base;
int top;
int size;
}ST;
void StackInit(ST*s)
{
s->base=(STD*)malloc(sizeof(STD)*INITSIZE);
s->top=0;
s->size=INITSIZE;
}
void checkcapacity(ST*s)
{
if(s->size==s->top)
{
s->size*=2;
s->base=(STD*)realloc(s->base,sizeof(STD)*s->size);
}
}
void push(ST*s,STD n)
{
checkcapacity(s);
s->base[s->top++]=n;
}
int StackEmpty(ST*s)
{
return s->top>0?1:0;
}
void pop(ST*s)
{
if(StackEmpty(s))
printf("%d\n",s->base[--s->top]);
else
printf("error\n");
}
void top(ST*s)
{
if(StackEmpty(s))
printf("%d\n",s->base[s->top-1]);
else
printf("error\n");
}
int main()
{
ST s;
StackInit(&s);
int n;
scanf("%d",&n);
int num;
getchar();
while(n--)
{
char fun[5]={0};
scanf("%s %d",fun,&num);
if(!strcmp(fun,"push"))
{
scanf("%d",&num);
push(&s,num);
}
if(!strcmp(fun,"pop"))
pop(&s);
if(!strcmp(fun,"top"))
top(&s);
}
return 0;
}
使用vector容器作为栈的存储空间
#include
#include
using namespace std;
class stack
{
vector<int>_data;
public:
void push(int x)
{
_data.push_back(x);
}
void pop()
{
if(_data.size())
{
cout<<_data.back()<<endl;
_data.pop_back();
}
else
cout<<"error"<<endl;
}
void top()
{
if(_data.size())
cout<<_data.back()<<endl;
else
cout<<"error"<<endl;
}
};
int main()
{
stack s;
int n;
cin>>n;
while(n--)
{
string tmp;
int num;
cin>>tmp;
if(tmp=="push")
{
cin>>num;
s.push(num);
}
if(tmp=="pop")
s.pop();
if(tmp=="top")
s.top();
}
return 0;
}
时间复杂度:O(n),进行n次对栈的操作
空间复杂度:O(1),除必要栈存储空间
题目要我们判断两个序列是否符合入栈出栈的次序,我们就可以用一个栈来模拟。对于入栈序列,只要栈为空,序列肯定要依次入栈。那什么时候出来呢?自然是遇到一个元素等于当前的出栈序列的元素,那我们就放弃入栈,让它先出来。
//入栈:栈为空或者栈顶不等于出栈数组
while(j < n && (s.isEmpty() || s.peek() != popA[i])){
s.push(pushA[j]);
j++;
}
如果能按照这个次序将两个序列都访问完,那说明是可以匹配入栈出栈次序的。

class Solution {
public:
bool IsPopOrder(vector<int> pushV,vector<int> popV) {
int n = pushV.size();
//辅助栈
stack<int> s;
//遍历入栈的下标
int j = 0;
//遍历出栈的数组
for(int i = 0; i < n; i++){
//入栈:栈为空或者栈顶不等于出栈数组
while(j < n && (s.empty() || s.top() != popV[i])){
s.push(pushV[j]);
j++;
}
//栈顶等于出栈数组
if(s.top() == popV[i])
s.pop();
//不匹配序列
else
return false;
}
return true;
}
};
时间复杂度:O(n),其中n为数组长度,最坏情况下需要遍历两个数组各一次
空间复杂度:O(n),辅助栈空间最大为一个数组的长度
方法一我们使用了一个辅助栈来模拟,但是数组本来就很类似栈啊,用下标表示栈顶。在方法一种push数组前半部分入栈了,就没用了,这部分空间我们就可以用来当成栈。原理还是同方法一一样,只是这时我们遍历push数组的时候,用下标n表示栈空间,n的位置就是栈顶元素。
class Solution {
public:
bool IsPopOrder(vector<int> pushV,vector<int> popV) {
//表示栈空间的大小,初始化为0
int n = 0;
//出栈序列的下标
int j = 0;
//对于每个待入栈的元素
for(int num : pushV){
//加入栈顶
pushV[n] = num;
//当栈不为空且栈顶等于当前出栈序列
while(n >= 0 && pushV[n] == popV[j]){
//出栈,缩小栈空间
j++;
n--;
}
n++;
}
//最后的栈是否为空
return n == 0;
}
};
时间复杂度:O(n),其中n为数组长度,最坏还是遍历两个数组
空间复杂度:O(1),常数级变量,无额外辅助空间