Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
typedef struct { } MinStack; void minStackCreate(MinStack *stack, int maxSize) { } void minStackPush(MinStack *stack, int element) { } void minStackPop(MinStack *stack) { } int minStackTop(MinStack *stack) { } int minStackGetMin(MinStack *stack) { } void minStackDestroy(MinStack *stack) { }1、复习一下堆栈知识点
//链式存储 //Written by ZP1015 //2015.10.18 #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #define OK 0 #define ERROR -1 /*链式结构*/ struct STACK_NODE { int pData; struct STACK_NODE *next; }; struct LinkStack { struct STACK_NODE *top;//栈顶指针的位置 int StackLen; //统计当前堆栈中包含多少数据 }; int init_stack(struct LinkStack *pStackNode) { pStackNode->top= NULL;//空栈,头指针指向空 pStackNode->StackLen = 0; return OK; } int empty_stack(struct LinkStack *pStackNode) { /*1.输入的参数有误*/ if(NULL == pStackNode) { printf("[%d] pStackNode is illegal! \n",__LINE__); return ERROR; } /*2.输入的链式堆栈为空*/ if(NULL == pStackNode->top) { printf("[%d] pStackNode is empty!\n",__LINE__); return ERROR; } struct STACK_NODE *pStackNodeTopNext = NULL; /*3.清空链表*/ while(pStackNode->StackLen){ pStackNodeTopNext = pStackNode->top->next;//保存 free(pStackNode->top); pStackNode->top = NULL;/*set to NULL*/ pStackNode->top = pStackNodeTopNext;//重新赋值栈顶指针 pStackNode->StackLen--;/*Stack len */ } return OK; } int push_stack(struct LinkStack *pStackNode, int value) { /*1.输入的参数有误*/ if(NULL == pStackNode) { printf("[%d] pStackNode is illegal! \n",__LINE__); return ERROR; } struct STACK_NODE *pStackNodeTemp = NULL; pStackNodeTemp = (struct STACK_NODE*)malloc(sizeof(struct STACK_NODE)); if(NULL == pStackNodeTemp) { printf("[%d] malloc failed!\n",__LINE__); return ERROR; } pStackNodeTemp->pData = value;//赋值 pStackNodeTemp->next = pStackNode->top;//当前的栈顶元素赋值给新结点如图① pStackNode->top = pStackNodeTemp;//新结点赋值给栈顶元素,如图② pStackNode->StackLen ++; return OK; } int pop_stack(struct LinkStack *pStackNode, int* value) { /*1.输入的参数有误*/ if(NULL == pStackNode || NULL == value) { printf("[%d] pStackNode is illegal! \n",__LINE__); return ERROR; } /*2.输入的链式堆栈为空*/ if(NULL == pStackNode->top) { printf("[%d] pStackNode is empty!\n",__LINE__); return ERROR; } struct STACK_NODE *pStackNodeTemp = NULL; *value = pStackNode->top->pData; pStackNodeTemp = pStackNode->top; pStackNode->top = pStackNode->top->next; free(pStackNodeTemp); pStackNodeTemp = NULL; pStackNode->StackLen--; return OK; } int count_stack_length(struct LinkStack pStackNode) { return pStackNode.StackLen; } void print_stack_node(struct LinkStack *pStackNode) { /*1.输入的参数有误*/ if(NULL == pStackNode) { printf("[%d] pStackNode is illegal! \n",__LINE__); return; } /*2.输入的链式堆栈为空*/ if(NULL == pStackNode->top) { printf("[%d] pStackNode is empty!\n",__LINE__); return ; } struct STACK_NODE *pStackNodeTemp = pStackNode->top; while(pStackNodeTemp!= NULL) { printf("%d ",pStackNodeTemp->pData); pStackNodeTemp = pStackNodeTemp->next; } printf("\n"); } int main() { struct LinkStack pStackNode; init_stack(&pStackNode); for (int i = 0;i<10;i++) { push_stack(&pStackNode,i); } print_stack_node(&pStackNode); /*test pop*/ #if 0 int a = 0; pop_stack(pStackNode,&a); print_stack_node(pStackNode); #endif #if 0 printf("%d\n",count_stack_length(pStackNode)); #endif empty_stack(&pStackNode); print_stack_node(&pStackNode); getchar(); getchar(); return 0; }
题目描述:
定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。
要求函数min,push,pop的时间复杂度都是O(1).
题目难点在于:获得堆栈中最小元素,由于堆栈结构是先入后出,故将堆栈设计成一个链表格式,堆栈变成了一个链表,不用将元素弹出来也可以实现查询
struct STACK_NODE { int pData; struct STACK_NODE *next; }; typedef struct { struct STACK_NODE* top;/*栈顶指针*/ int StackLen;/*链式堆栈当前长度*/ int StackLenMax;/*链式堆栈最大长度*/ } MinStack; void minStackCreate(MinStack *pStackNode, int maxSize) { if(!pStackNode) { return; } pStackNode->top= NULL;//空栈,头指针指向空 pStackNode->StackLen = 0; pStackNode->StackLenMax = maxSize; } void minStackPush(MinStack *pStackNode, int element) { /*1.输入的参数有误*/ if(NULL == pStackNode) { return; } /*2.栈已经满了*/ if(pStackNode->StackLen==pStackNode->StackLenMax) { return; } struct STACK_NODE* pStackNodeTemp = NULL; pStackNodeTemp = (struct STACK_NODE*)malloc(sizeof(struct STACK_NODE)); if(NULL == pStackNodeTemp) { return; } pStackNodeTemp->pData = element;//赋值 pStackNodeTemp->next = pStackNode->top;//当前的栈顶元素赋值给新结点如图① pStackNode->top = pStackNodeTemp;//新结点赋值给栈顶元素,如图② pStackNode->StackLen ++; } void minStackPop(MinStack *pStackNode) { /*1.输入的参数有误*/ if(NULL == pStackNode) { return; } /*2.输入的链式堆栈为空*/ if(NULL == pStackNode->top) { return; } struct STACK_NODE *pStackNodeTemp = NULL; pStackNodeTemp = pStackNode->top; pStackNode->top = pStackNode->top->next; free(pStackNodeTemp); pStackNodeTemp = NULL; pStackNode->StackLen--; } int minStackTop(MinStack *pStackNode) { int pStackNodeTopData = pStackNode->top->pData; return pStackNodeTopData; } int minStackGetMin(MinStack *pStackNode) { /*1.输入的参数有误*/ if(NULL == pStackNode) { return 0; } /*2.输入的链式堆栈为空*/ if(NULL == pStackNode->top) { return 0; } int min = 0x7fffffff; struct STACK_NODE *pStackNodeTemp = pStackNode->top; while(pStackNodeTemp!= NULL) { if(min>pStackNodeTemp->pData) { min = pStackNodeTemp->pData; } pStackNodeTemp = pStackNodeTemp->next; } return min; } void minStackDestroy(MinStack *pStackNode) { /*1.输入的参数有误*/ if(NULL == pStackNode) { return ; } /*2.输入的链式堆栈为空*/ if(NULL == pStackNode->top) { return ; } struct STACK_NODE *pStackNodeTopNext = NULL; /*3.清空链表*/ while(pStackNode->StackLen){ pStackNodeTopNext = pStackNode->top->next;//保存 free(pStackNode->top); pStackNode->top = NULL;/*set to NULL*/ pStackNode->top = pStackNodeTopNext;//重新赋值栈顶指针 pStackNode->StackLen--;/*Stack len */ } }