#include <stdio.h> #include <stdlib.h> #include <string.h> #include "stack.h" #include "stack_public.h" static SEQ_S_STACK s_stack; SEQ_S_STACK *STACK_getDev(void) { return &s_stack; } int STACK_init(SEQ_S_STACK *pStack) { if (NULL == pStack) { return -1; } pStack->bootm = malloc(STACK_SIZE * sizeof(ELEM_TYPE)); if (NULL == pStack) { return -1; } pStack->top = pStack->bootm; pStack->ulStackSize = STACK_SIZE; return 0; } int STACK_isFull(SEQ_S_STACK *pStack) { if (NULL == pStack) { return -1; } return (pStack->top - pStack->bootm == pStack->ulStackSize - 1); } int STACK_isEmpty(SEQ_S_STACK *pStack) { if (NULL == pStack) { return -1; } return (pStack->bootm == pStack->top); } int STACK_pushElem(SEQ_S_STACK *pStack, ELEM_TYPE new) { if (NULL == pStack) { return -1; } if (STACK_isFull(pStack)) { pStack->bootm = (ELEM_TYPE *)realloc(pStack->bootm, sizeof(ELEM_TYPE) * (pStack->ulStackSize + STACK_INCREMENT)); if(NULL == pStack->bootm) { return -1; } pStack->top = pStack->bootm + pStack->ulStackSize; pStack->ulStackSize += STACK_INCREMENT; } *(pStack->top) = new; printf("the push %d\n", *(pStack->top)); pStack->top++; return 0; } /* 出栈 */ int STACK_popElem(SEQ_S_STACK *pStack, ELEM_TYPE *pData) { if (NULL == pStack) { return -1; } if (STACK_isEmpty(pStack)) { return -1; } pStack->top--; *pData = *(pStack->top); return 0; }
#include <stdio.h> #include "stack_public.h" static int valueTranse(SEQ_S_STACK *pStack, unsigned int ulOldValue, unsigned int *pNewValue) { unsigned int ulTmpValue = 0; int lRetValue = 0; if (NULL == pStack) { return -1; } while (ulOldValue != 0) { ulTmpValue = ulOldValue % 8; STACK_pushElem(pStack, ulTmpValue); ulOldValue = ulOldValue / 8; } while (1 != STACK_isEmpty(pStack)) { STACK_popElem(pStack, &lRetValue); printf("%d\t", lRetValue); } printf("\n"); return 0; } int main(int argc, char **argv) { unsigned int ulRetValue = 0; unsigned int ulValue = 320; unsigned int *pRetValue = &ulRetValue; SEQ_S_STACK *pStack = NULL; pStack = STACK_getDev(); STACK_init(pStack); valueTranse(pStack, ulValue, pRetValue); return 0; } ~
#ifndef __STACK_PUBLIC_H__ #define __STACK_PUBLIC_H__ typedef int ELEM_TYPE; typedef struct tagSEQUENCE_S_STACK { unsigned int ulStackSize; ELEM_TYPE *bootm; ELEM_TYPE *top; }SEQ_S_STACK; extern SEQ_S_STACK *STACK_getDev(void); extern int STACK_init(SEQ_S_STACK *pStack); extern int STACK_isFull(SEQ_S_STACK *pStack); extern int STACK_isEmpty(SEQ_S_STACK *pStack); extern int STACK_pushElem(SEQ_S_STACK *pStack, ELEM_TYPE data); extern int STACK_popElem(SEQ_S_STACK *pStack, ELEM_TYPE *pdata); #endif