最小栈的两种实现方法

方法一:顺序栈

最小栈入栈思路:

    以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(){
	TestHeader;
	Min_Struct stack;
	MinstructInit(&stack);
	MinStackPush(&stack, 9);
	MinStackPush(&stack, 5);
	MinStackPush(&stack, 7);
	MinStackPush(&stack, 2);
	MinstackPrint(&stack, "最小栈的入栈");

}
void TestMinTop(){
	TestHeader;
	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>
typedef int LinkType;
#define MaxSize 1000
typedef struct LinkNode{
	LinkType data;
	struct LinkNode* next;
}LinkNode;
typedef struct Min_Stack{
	LinkNode head;
}Min_Stack;

LinkNode* MinStackCreat(LinkType value);//开辟结点空间
void MinStackInit(Min_Stack* stack);//最小栈的初始化
void MinStackPush(Min_Stack* stack, LinkType value);//最小栈入栈
void MinStackPop(Min_Stack* stack);//最小栈出栈
int  MinStackTop(Min_Stack* stack, LinkType *value);//取最小栈栈顶元素


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

}
void MinStackPop(Min_Stack* stack)
{
	//出栈两次
	if (stack == NULL){
		return;
	}
	if (stack->head.next == NULL){
		return;
	}
	LinkNode* to_delete1 = stack->head.next;
	stack->head.next = to_delete1->next;
	free(to_delete1);
	LinkNode* to_delete2 = stack->head.next;
	stack->head.next = to_delete2->next;
	free(to_delete2);
	return;
}
int  MinStackTop(Min_Stack* stack, LinkType *value)
{
	if (stack == NULL){
		return 0;
	}
	if (stack->head.next == NULL){
		return 0;
	}
	*value = stack->head.next->data;
	return 1;
}

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

void TestMinstackPush()
  {
	  TestHeader;
	  Min_Stack stack;
	  MinStackInit(&stack);
	  MinStackPush(&stack, 9);
	  MinStackPush(&stack, 5);
	  MinStackPush(&stack, 7);
	  MinStackPush(&stack, 2);
	  MinStackPrint(&stack, "最小栈元素入栈");
  }
  void TestMinstackTop(){
	  TestHeader;
	  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;
  }


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭