最小栈设计并实现一个MinStack类,要求至少含有以下三种方法
一、push方法:将一个整数进行压栈
二、pop方法:弹出栈顶元素
三、getmin方法:输出栈内最小元素
输入格式:
第一行输入一个N,表示接下来要操作的数量
随后N行表示操作:
push方法后跟要压栈的元素
pop方法、getmin方法后不跟任何元素
说明:假设不包含其他无效输入,以及在执行getmin、pop方法时栈不为空
输出样例:
输出栈内最小元素
输入样例:
5
push 2
push 3
push 1
pop
getmin
输出样例:
2
代码
#include <stdio.h>
#include <string.h>
struct cz {
char name[10];
int xh;
};
int main()
{
int n;
int b[n+1];
struct cz a;
scanf("%d",&n);
int i,j;
int count=0;
int min;
for (i=1;i<=n;i++){
scanf("%s",a.name );
if(strcmp(a.name,"push")==0){
scanf("%d",&b[count]);
count++;
}
else if (strcmp(a.name,"pop")==0){
count--;
}
else {
min=0;
for (j=1;j<count;j++){
if(b[min]>b[j]){
min=j;
}
}
printf("%d\n",b[min]);
}
}
return 0;
}