ÀàËÆÓÚ˳Ðò±í£¬ÓÃһάÊý×éÃèÊö˳ÐòÕ»ÖеÄÊý¾ÝÔªËصĴ洢ÇøÓò£¬²¢Ô¤ÉèÒ»¸öÊý×éµÄ×î´ó¿Õ¼ä¡£ÃèÊö˳ÐòÕ»µÄͨ³£µÄÏ°¹ß×ö·¨ÊÇÒÔtop=0±íʾ¿ÕÕ»£¬¼øÓÚCÓïÑÔÖÐÊý×éµÄϱêÔ¼¶¨ÊÇ´Ó0¿ªÊ¼£¬Ôòµ±ÒÔC×÷ÃèÊöÓïÑÔʱ£¬Èç´ËÉ趨»á´øÀ´ºÜ´ó²»±ã£»ÁíÒ»·½Ã棬ÓÉÓÚÕ»ÔÚʹÓùý³ÌÖÐËùÐè×î´ó¿Õ¼äµÄ´óСºÜÄѹÀ¼Æ£¬Òò´Ë£¬Ò»°ãÀ´Ëµ£¬ÔÚ³õʼ»¯Éè¿Õջʱ²»Ó¦ÏÞ¶¨Õ»µÄ×î´óÈÝÁ¿¡£Ò»¸ö½ÏºÏÀíµÄ×ö·¨ÊÇ£ºÏÈΪջ·ÖÅäÒ»¸ö»ù±¾ÈÝÁ¿£¬È»ºóÔÚÓ¦Óùý³ÌÖУ¬µ±Õ»µÄ¿Õ¼ä²»¹»Ê¹ÓÃʱÔÙÖð¶ÎÀ©´ó
Õ»£ºÖ»ÔÊÐíÒ»¶Ë½øÐвÙ×÷µÄÏßÐÔ±í¡£ ÔÊÐí²Ù×÷µÄÒ»¶Ë½Ð×öÕ»¶¥£¬²»ÄܲÙ×÷µÄÒ»¶Ë½Ð×öÕ»µ×¡£
µ±Õ»ÖÐûÓÐÔªËصÄʱºò£¬Ëü¾ÍÊÇ¿ÕÕ»¡£
¶¨ÒåÒ»¸öÕ»Óï·¨½á¹¹£º
struct STACK
{
//´æ·ÅÊý¾Ý
data_t data[STACK_SIZE];
//Õ»¶¥
data_t iTop;
};
//stack.h #ifndef _STACK_H_ #define _STACK_H_
#define data_t int #define STACK_SIZE 5
//˳ÐòÕ»µÄ¶¨Òå typedef struct STACK { data_t data[STACK_SIZE]; data_t iTop; }Stack;
//´æ·Å´íÎóÂë typedef enum OP { STACK_EMPTY = -2, STACK_ERROR, STACK_OK }STACK_ENUM_OP;
//º¯ÊýµÄÉùÃ÷ Stack * CreateStack(); void DestroyStack(Stack *pstack); int PushStack(Stack *pstack,data_t pdata); int PopStack(Stack *pstack,data_t* pdata); int EmptyStack(Stack *pstack);
#endif // _STACK_H_ |
#include <stdio.h> #include "stack.h" #include <stdlib.h> #include <string.h>
/******************************************** º¯ÊýÃû£ºCreateStack ¹¦ÄÜ£º´´½¨Ò»¸ö¿ÕÕ» ×÷Õߣº ÈÕÆÚ£º ********************************************/ Stack * CreateStack() { Stack * pstack = (Stack *)malloc(sizeof(Stack)); if(NULL != pstack) { pstack->iTop = -1; } return pstack; }
/******************************************** º¯ÊýÃû£ºDestroyStack ¹¦ÄÜ£º Ïú»ÙÒ»¸öÕ» ×÷Õߣº ÈÕÆÚ£º ********************************************/ void DestroyStack(Stack *pstack) { if(NULL == pstack) { printf("Destroy error\n"); return ; } free(pstack); pstack = NULL; }
/******************************************** º¯ÊýÃû£ºPushStack ¹¦ÄÜ£ºÔªËØѹջ ´ÓÕ»¶¥Ôö¼ÓÒ»¸öÐÂÔªËØ ×÷Õߣº ÈÕÆÚ£º ********************************************/ int PushStack(Stack *pstack,data_t pdata) // ѹջ { if((NULL == pstack)||(STACK_SIZE-1 == pstack->iTop)) { printf("Push is error\n"); return STACK_ERROR; } pstack->iTop++; pstack->data[pstack->iTop] = pdata; return STACK_OK;
}
/******************************************** º¯ÊýÃû£ºPopStack ¹¦ÄÜ£º ³öջɾ³ýÕ»¶¥ÔªËØ ×÷Õߣº ÈÕÆÚ£º ********************************************/ int PopStack(Stack *pstack,data_t* pdata) { if((NULL == pstack)||(NULL == pdata)||(-1 ==pstack->iTop)) { printf("Popstack is error\n"); return STACK_ERROR; } *pdata = pstack->data[pstack->iTop]; pstack->iTop--; return STACK_OK; }
/******************************************** º¯ÊýÃû£ºEmptyStack ¹¦ÄÜ£ºÅжÏÕ»ÊÇ·ñΪ¿Õ ×÷Õߣº ÈÕÆÚ£º ********************************************/ int EmptyStack(Stack *pstack) { if(NULL == pstack) { printf("Empty stack errror\n"); return STACK_ERROR; } if(-1 ==pstack->iTop) { return STACK_EMPTY; } return STACK_OK; }
|
#include <stdio.h> #include "stack.h"
int main () { Stack *pstack = CreateStack(); if(NULL == pstack) { printf("create is failed\n"); return -1; } int i; for(i = 0;i<5;i++) { PushStack(pstack,i); } printf("stack is %d\n",EmptyStack(pstack)); int num = -1; for(i = 0;i<5;i++) { PopStack(pstack,&num); printf("%d\n",num); } printf("stack is %d\n",EmptyStack(pstack));
DestroyStack(pstack); pstack = NULL; return 0; } |