【申明:本文仅限于自我归纳总结和相互交流,有纰漏还望各位指出。 联系邮箱:Mr_chenping@163.com】
题目:定义栈的数据结构,要求添加一个min
函数,能够得到栈的最小元素。
要求函数min、push 以及pop 的时间复杂度都是O(1)。
题目分析:
一、插入的时候多插入一个数值min,这个数值被初始化为第一次插入节点的数值
二、后续插入的时候,先与上一次min比较,保证每次插入的时候都是最小值。
算法实现:
#include <stdio.h>
#include <stdlib.h>
typedef struct _data
{
int value;
int min;
}data;
typedef struct _stack
{
int top;
int calloc_size;
data *data;
}stack_t;
stack_t * stack_init(int size)
{
stack_t *re = calloc(1, sizeof(stack_t));
re->top = 0;
re->calloc_size = size;
re->data = calloc(size, sizeof(data));
}
void stack_free(stack_t *s)
{
if(s)
{
if(s->data)
free(s->data);
free(s);
}
}
void stack_push(stack_t *s, int value)
{
data data;
if(s->top == 0)
{
data.min = value;
}
else
{
if(s->data[s->top - 1].min > value)
data.min = value;
}
data.value = value;
s->data[s->top++] = data;
}
int stack_pop(stack_t *s)
{
if(s->top == 0)
return 0;
return s->data[--(s->top)].value;
}
int stack_min(stack_t *s)
{
if(s->top == 0)
return 0;
return s->data[s->top - 1].min;
}
int main()
{
stack_t *s = stack_init(8);
stack_push(s, -1);
stack_push(s, 1);
stack_push(s, -10);
stack_push(s, 15);
stack_push(s, 17);
stack_push(s, -11);
stack_push(s, -100);
stack_push(s, 100);
printf("min---->%d\n", stack_min(s));
while(s->top)
{
printf("pop---->%d\n", stack_pop(s));
}
stack_free(s);
return 0;
}