#include "stdafx.h"
#include <stdlib.h>
#define Max 50
typedef struct Data{
int value;
int min;
}*DataType;
typedef struct Stack
{
DataType link;
int top;
}Stack;
void InitStack(Stack *s)
{
s->top=0;
s->link=(DataType)malloc(Max*sizeof(struct Data));
}
void Push(Stack *s, int d)
{
if(s->top==Max)
{
printf("栈已满,无法进栈\n");
return;
}
DataType element=(DataType)malloc(sizeof(struct Data));
element->value=d;
if(s->top==0)
element->min=d;
else
{
element->min=s->link[(s->top)-1].min;
if(s->link[(s->top)-1].min>d)
element->min=d;
}
s->link[s->top]=*element;
s->top++;
}
void Pop(Stack *s)
{
if(s->top==0)
{
printf("栈为空\n");
return;
}
s->top--;
}
int MinStack(Stack *s)
{
return s->link[s->top-1].min;
}
int _tmain(int argc, _TCHAR* argv[])
{
int i;
int value;
Stack s;
InitStack(&s);
for(i=0;i<5;i++)
{
scanf("%d",&value);
Push(&s,value);
}
Pop(&s);
printf("min is :%d\n",MinStack(&s));
system("PAUSE");
return 0;
}
网友评论