stack link

来源:互联网 发布:mac装好windows没wifi 编辑:程序博客网 时间:2024/06/08 19:42

CODE

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>typedef struct tagStackNode{int iValue;struct tagStackNode *pstNext;}STACK_NODE_S;static STACK_NODE_S *pstStack;#define STACK_TOP() \pstStack;#define STACK_SET_TOP(pstTop) \{ \pstStack = pstTop; \}int STACK_Push(int iNum){int iRet = -1;STACK_NODE_S *pstTop;pstTop = (STACK_NODE_S *)malloc(sizeof(STACK_NODE_S));if (pstTop != NULL){memset(pstTop, 0, sizeof(STACK_NODE_S));pstTop->iValue = iNum;pstTop->pstNext = STACK_TOP();STACK_SET_TOP(pstTop);iRet = 0;}return iRet;}void STACK_Pop(){STACK_NODE_S *pstCurTop = NULL;pstCurTop = STACK_TOP();assert(pstCurTop != NULL);STACK_SET_TOP(pstCurTop->pstNext);free(pstCurTop);return;}int STACK_Top(){STACK_NODE_S *pstTop;pstTop = STACK_TOP();assert(pstTop != NULL);return pstTop->iValue;}int STACK_IsEmpty(){STACK_NODE_S *pstTop;pstTop = STACK_TOP();return pstTop == NULL;}int main(){int iRet = -1;int i;STACK_NODE_S *pstTop;printf("Push:\r\n");for (i = 0; i < 5; i++){iRet = rand();printf("%d ", iRet);STACK_Push(iRet);}printf("\r\n");printf("Display and Pop.\r\n");while (STACK_IsEmpty() == 0){printf("%d ", STACK_Top());STACK_Pop();}return iRet;} 

Result

Push:
1804289383 846930886 1681692777 1714636915 1957747793
Display and Pop.
1957747793 1714636915 1681692777 846930886 1804289383

0 0