首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

堆栈兑现

2012-09-03 
堆栈实现#include stdio.h#include stdlib.h#include memory.h#include stack.h#ifdef __cplusplu

堆栈实现

#include <stdio.h>#include <stdlib.h>#include <memory.h>#include "stack.h"#ifdef __cplusplus extern "C" { #endif #ifdef STACK_LINKtypedef struct stack{int num;struct stack *next;}STACK;STACK *g_pStack = NULL;void push(int i){STACK *pEle = (STACK*)malloc(sizeof(STACK));if(pEle == NULL){return;}memset(pEle,0,sizeof(STACK));pEle->num = i;if(g_pStack == NULL){g_pStack = pEle;}else{pEle->next = g_pStack;g_pStack = pEle;}return;}int pop(){int tempRe = 0;STACK *tempFree = 0;if(g_pStack == NULL){return 0;}tempRe = g_pStack->num;tempFree = g_pStack;g_pStack = g_pStack->next;free(tempFree);return tempRe;}int empty(){return (g_pStack)?0:1;}#endif#ifdef STACK_ARRAYint g_stack[MAX_STACK] = {0};unsigned int g_pos = 0;void push(int i){if(g_pos == MAX_STACK){return;}g_stack[g_pos++] = i;return;}int pop(){if(g_pos == 0){return 0;}return g_stack[--g_pos];}int empty(){return (g_pos == 0)?1:0;}#endif#ifdef __cplusplus}#endif
?

热点排行