码迷,mamicode.com
首页 > 其他好文 > 详细

Min Stack

时间:2014-12-15 17:25:36      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:des   style   io   ar   color   on   ef   amp   as   

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.
思路:同时维护两个栈,正常的进出都往datastack里进行,push的时候,如果minstack是空的,或者最上面的元素大小比push的元素大,则同时push minstack,这样minstack的top元素永远维持当前栈中最小的元素;pop的时候,先pop datastack,如果datastack的栈顶元素和minstack的栈顶元素一样大,则同时pop minstack。

C:
#include<stdlib.h>
#include<stdio.h>


struct node
{
    int data;
    struct node* down;
};
typedef struct node node;


struct stack
{
    node* top;
    int size;
};
typedef struct stack Stack;


Stack* EmptyStack()
{
    Stack *top = (Stack*)malloc(sizeof(Stack*));
    if(top)
    {
        top->top = NULL;
        top->size = 0;
    }
    return top;
}


void push(Stack* stack,Stack* minstack,int x) {
    node* newtop=(node *)malloc(sizeof(node));
    newtop->data=x;
    newtop->down=stack->top;
    stack->top=newtop;
    stack->size++;
    if(minstack->top==NULL || x <= minstack->top->data){
        node* newmin=(node *)malloc(sizeof(node));
        newmin->data=x;
        newmin->down=minstack->top;
        minstack->top=newmin;
        minstack->size++;
    }
    return;
}


void pop(Stack* stack,Stack* minstack){
    node* p;
    int d;
    if(stack->top==NULL && stack->size==0) return;
    else{
        p=stack->top;
        d=stack->top->data;
        stack->top=stack->top->down;
        stack->size--;
        free(p);
        if(d == minstack->top->data){
            if(minstack->top == NULL && minstack->size == 0) return;
            else {
               p=minstack->top;
               minstack->top=minstack->top->down;
               stack->size--;
               free(p);
            }
        }
    }
    return;
}


int top(Stack* stack) {
    return stack->top->data;
}


int getMin(Stack* minstack){
    return minstack->top->data;
}


void main(){
    Stack* stack=EmptyStack();
    Stack* minstack=EmptyStack();
    int data;
    scanf("%d",&data);
    while(data>=0) {
        push(stack,minstack,data);
        scanf("%d",&data);
    }
    pop(stack,minstack);
    printf("%d\n",getMin(minstack));
}


C++
class MinStack {
private:
    stack<int> datastack;
    stack<int> minstack;
public:
    void push(int x) {
        datastack.push(x);
        if(minstack.empty() || x<=minstack.top())
            minstack.push(x);
    }

    void pop() {
        int data=datastack.top();
        datastack.pop();
        if(data==minstack.top()) minstack.pop();
    }

    int top() {
        return datastack.top();
    }

    int getMin() {
        return minstack.top();
    }
};




Min Stack

标签:des   style   io   ar   color   on   ef   amp   as   

原文地址:http://blog.csdn.net/uj_mosquito/article/details/41944337

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!