# 最小栈的两种实现方法

以9 5 7 2为例：

Min_stack.h
#include<stdio.h>
#pragma once
#define StackMAX 1000
typedef int SeqListType;

typedef struct Min_Struct{
SeqListType data[StackMAX];
size_t size;
}Min_Struct;

void MinstructInit(Min_Struct* stack);//最小栈的初始化
void MinStackPush(Min_Struct* stack, SeqListType value);//最小栈的入栈
void MinStackPop(Min_Struct* stack);//最小栈的出栈（出栈两次）
int MinStackTop(Min_Struct* stack, SeqListType* value);//取最小栈的栈顶元素
Min_stack.c
//代码部分

void MinstructInit(Min_Struct* stack)
{
if (stack == NULL){
return;
}
stack->size = 0;
return;
}
int Max_judge(int* flag, int* value)
{
return *flag > *value ? *value : *flag;
}
void MinStackPush(Min_Struct* stack, SeqListType value)
{
if (stack == NULL){
return;
}
if (stack->size >= StackMAX){
return NULL;
}
if (stack->size == 0){
stack->data[stack->size++] = value;
stack->data[stack->size++] = value;
return;
}
else{
stack->data[stack->size++] = value;
int flag = stack->data[stack->size - 2];
int ret=Max_judge(&flag, &value);
stack->data[stack->size++] = ret;
return;
}
}
void MinStackPop(Min_Struct* stack)
{
if (stack == NULL){
return;
}
if (stack->size == 0){
return;
}
stack->size--;
stack->size--;
return;
}
int MinStackTop(Min_Struct* stack, SeqListType* value)
{
if (stack == NULL){
return 0;
}
if (stack->size == 0){
return 0;
}
*value = stack->data[stack->size-1];
return 1;
}
测试部分

void TestMinPush(){
Min_Struct stack;
MinstructInit(&stack);
MinStackPush(&stack, 9);
MinStackPush(&stack, 5);
MinStackPush(&stack, 7);
MinStackPush(&stack, 2);
MinstackPrint(&stack, "最小栈的入栈");

}
void TestMinTop(){
Min_Struct stack;
MinstructInit(&stack);
MinStackPush(&stack, 9);
MinStackPush(&stack, 5);
MinStackPush(&stack, 7);
MinStackPush(&stack, 2);
MinstackPrint(&stack, "最小栈的入栈");
int tmp;
int ret=MinStackTop(&stack, &tmp);
printf("expect 1,actual %d\n", ret);
printf("expect 2,actual %d\n", tmp);
MinStackPop(&stack);
int ret1 = MinStackTop(&stack, &tmp);
printf("expect 1,actual %d\n", ret);
printf("expect 5,actual %d\n", tmp);
}
主函数调用
int main(){
TestMinPush();
TestMinTop();}

LinkMin_stack.h


#include<stddef.h>
#include<stdlib.h>
#define MaxSize 1000
typedef struct Min_Stack{
}Min_Stack;

void MinStackInit(Min_Stack* stack);//最小栈的初始化
void MinStackPop(Min_Stack* stack);//最小栈出栈
int  MinStackTop(Min_Stack* stack, LinkType *value);//取最小栈栈顶元素

LinkMin_stack.c
#include<stdio.h>
{
cur->data = value;
cur->next = NULL;
return cur;
}
void MinStackInit(Min_Stack* stack)
{
if (stack == NULL){
return;
}
}
int Max_judge(int* value, int* flag){
if (flag == NULL || value == NULL){
return 0;
}
return *value > *flag ? *flag : *value;
}
{
if (stack == NULL){
return;
}
return;
}
else{
int ret = Max_judge(&value, &flag);
return;
}

}
void MinStackPop(Min_Stack* stack)
{
//出栈两次
if (stack == NULL){
return;
}
return;
}
free(to_delete1);
free(to_delete2);
return;
}
{
if (stack == NULL){
return 0;
}
return 0;
}
return 1;
}

/////////////////////////////////////////////////测试部分///////////////////////////////////////////
void MinStackPrint(Min_Stack* stack, const char* msg){
if (stack == NULL){
printf("stack==NULL\n");
return;
}
printf("[%s]\n", msg);
printf("[栈顶]");
for (; cur != NULL; cur = cur->next){
printf("[%d]", cur->data);
}
printf("[栈底]\n");
}

void TestMinstackPush()
{
Min_Stack stack;
MinStackInit(&stack);
MinStackPush(&stack, 9);
MinStackPush(&stack, 5);
MinStackPush(&stack, 7);
MinStackPush(&stack, 2);
MinStackPrint(&stack, "最小栈元素入栈");
}
void TestMinstackTop(){
Min_Stack stack;
MinStackInit(&stack);
MinStackPush(&stack, 9);
MinStackPush(&stack, 5);
MinStackPush(&stack, 7);
MinStackPush(&stack, 2);
MinStackPrint(&stack, "最小栈元素入栈");
int tmp;
int ret=MinStackTop(&stack, &tmp);
printf("expect 1,actual %d\n", ret);
printf("expect 2,actual %d\n", tmp);
MinStackPop(&stack);
int ret1 = MinStackTop(&stack, &tmp);
printf("expect 1,actual %d\n", ret1);
printf("expect 5,actual %d\n", tmp);
}
////////////////////////////////////////主函数调用/////////////////////////////////
int main(){
TestMinstackPush();
TestMinstackTop();
return 0;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120