NO2包含min函数的栈

来源:互联网 发布:淘宝装修全屏轮播代码 编辑:程序博客网 时间:2024/04/29 21:21
#include<iostream>
#include<stdio.h>
using namespace std;
//typedef int Elemtype;
struct Elemtype
{
    int data;
    int min;
};
#define MaxSize 1005
class Stack
{
    public:
        int size;
        int top1;
        Elemtype myStack[MaxSize];
        Stack()
        {
            size=0;
            top1=-1;
        }
        void push(int data)
        {
            top1++;
            myStack[top1].min=(top1==0?data:min(data,myStack[top1-1].min));
            myStack[top1].data=data;
            size++;
        }
        void pop()
        {
            top1--;
            size--;
        }
        Elemtype top()
        {
            return myStack[top1];
        }
        bool empty()
        {
            if(size<=0)
                return true;
            return false;
        }
};
int main(int argc,char *argv[])
{
    freopen("input.txt","r",stdin);
    Stack ts;
    int num,i,data;
    scanf("%d",&num);
    for(i=0;i<num;i++)
    {
        scanf("%d",&data);
        ts.push(data);
    }
    printf("size=%d\n",ts.size);
    while(!ts.empty())
    {
        Elemtype tmp;
        tmp=ts.top();
        ts.pop();
        printf("栈顶元素为:%d,当前最小值为:%d\n",tmp.data,tmp.min);
    }
    printf("\n");
//    puts("\n");
    return 0;
}

0 0
原创粉丝点击