pop push min O(1)

#include 
#include 
using namespace std;

#include
#define STACK_LEN 7

typedef struct
{
	int data;
	int min;
}stackitem;

typedef struct
{
	stackitem data[STACK_LEN];
	int top;
}stack;

void push(stack *s,int val)
{
	s->data[s->top].data = val;
	if(s->top > 0) 			// 保证栈顶元素中的min始终为当前栈中最小值
	{
		if(val < s->data[s->top-1].min)   // 如果当前push进的元素小于栈中最小元素值
			s->data[s->top].min = val;	  // 把当前元素置为栈中最小元素值
		else
			s->data[s->top].min = s->data[s->top-1].min;
	}
	else		// 否则,不更新
		s->data[s->top].min = val;
	s->top++;
}
int pop(stack *s)
{
	if(s->top-1 >= 0)
		return s->data[--s->top].data;
	else
		return -1;
}
int min(stack*s)
{
	if(s->top-1 >= 0)
		return s->data[s->top-1].min;
	else
		return -1;
}
int main(int argc,char *argv[])
{
	stack s;
	memset(&s,0,sizeof(s));
	push(&s,5);
	push(&s,2);
	push(&s,1);
	push(&s,3);
	push(&s,7);
	push(&s,9);

	printf("pop: %d\n",pop(&s));
	printf("min: %d\n\n",min(&s));

	printf("pop: %d\n",pop(&s));
	printf("min: %d\n\n",min(&s));

	printf("pop: %d\n",pop(&s));
	printf("min: %d\n\n",min(&s));

	printf("pop: %d\n",pop(&s));
	printf("min: %d\n\n",min(&s));

    printf("pop: %d\n",pop(&s));
	printf("min: %d\n\n",min(&s));


	return 0;
}



/*

class C_Overflow {};


template
class C_MinStack
{
public:
    C_MinStack();
    bool IsEmpty() const { return nCurLen == 0; }
    bool IsFull() const  { return nCurLen == MAX_SIZE; }
	int GetSize() const  { return nCurLen; }

	const T& Top() const;
    const T& GetMin() const;
    int Push(const T& val);
    T Pop();

private:
    T arrAllItem[MAX_SIZE];
    int arrMinIndex[MAX_SIZE];
    int nCurLen;
	int nCurMinPos;
};

template
C_MinStack::C_MinStack():nCurLen(0),nCurMinPos(0) {}

template
const T& C_MinStack::Top() const
{
    if (IsEmpty())
    {
        throw C_Overflow();
    }

    return arrAllItem[nCurLen - 1];
}

template
const T& C_MinStack::GetMin() const
{
    if (IsEmpty())
    {
        throw C_Overflow();
    }
    return arrAllItem[arrMinIndex[nCurMinPos - 1]];
}


template
int C_MinStack::Push(const T& val)
{
    if (IsFull())
    {
        throw C_Overflow();
    }
    if (IsEmpty() || val < GetMin())
    {
        arrMinIndex[nCurMinPos++] = nCurLen;
    }

    arrAllItem[nCurLen++] = val;
	return nCurLen;
}


template
T C_MinStack::Pop()
{
    if (IsEmpty())
    {
        throw C_Overflow();
    }

    T retVal = arrAllItem[--nCurLen];

    if (nCurLen == arrMinIndex[nCurMinPos-1])
    {
        nCurMinPos--;
    }

    return retVal;
}



int main(int argc, char * argv[])
{
	C_MinStack objStack;

	objStack.Push(2);
	objStack.Push(4);
	objStack.Push(8);
	objStack.Push(6);
	objStack.Push(12);
	objStack.Push(10);
	objStack.Push(15);
	objStack.Push(1);

	cout << "Stack size is: " << objStack.GetSize() << ",Min value is: " << objStack.GetMin() <<  endl;
	objStack.Pop();
	cout << "Stack size is: " << objStack.GetSize() << ",Min value is: " << objStack.GetMin() <<  endl;
	objStack.Pop();
	cout << "Stack size is: " << objStack.GetSize() << ",Min value is: " << objStack.GetMin() <<  endl;
	objStack.Pop();
	cout << "Stack size is: " << objStack.GetSize() << ",Min value is: " << objStack.GetMin() <<  endl;

	return 0;
}
*/


 

你可能感兴趣的:(Algorithm)