myStack.h
#ifndef MYSTACK_H_INCLUDED #define MYSTACK_H_INCLUDED #include "myList.h" #include "myTree.h" typedef struct tag_myStack { int data; myTree *pTree; struct list_head stStack; }myStack; myStack* getNewNode(); void initStack(myStack *pRoot); void destoryStack(myStack *pRoot); int getStackLen(myStack *pRoot); int isStackEmpty(myStack *pRoot); void stackPush(myStack *pRoot, myStack *pNew); myStack* stackPop(myStack *pRoot); #endif // MYSTACK_H_INCLUDED
myStack.c
#include "myStack.h" #include "malloc.h" myStack* getNewNode() { myStack *pTmp = NULL; pTmp = (myStack *)malloc(sizeof(myStack)); if (NULL == pTmp) { return NULL; } pTmp->data = 0; pTmp->pTree = NULL; INIT_LIST_HEAD(&(pTmp->stStack)); return pTmp; } void initStack(myStack *pRoot) { pRoot->data = 0; pRoot->pTree = NULL; INIT_LIST_HEAD(&(pRoot->stStack)); return; } void destoryStack(myStack *pRoot) { struct list_head *pos = NULL; struct list_head *n = NULL; myStack *pstStack = NULL; list_for_each_safe(pos, n, &(pRoot->stStack)) { list_del(pos); pstStack = list_entry(pos, myStack, stStack); free(pstStack); pos = n; } return; } int getStackLen(myStack *pRoot) { int len = 0; struct list_head *pos = NULL; list_for_each(pos, &(pRoot->stStack)) { len++; } return len; } int isStackEmpty(myStack *pRoot) { return list_empty(&(pRoot->stStack)); } void stackPush(myStack *pRoot, myStack *pNew) { list_add_tail(&(pNew->stStack), &(pRoot->stStack)); } myStack* stackPop(myStack *pRoot) { myStack *pstStack = NULL; struct list_head *pTmp = NULL; if (!isStackEmpty(pRoot)) { pTmp = (pRoot->stStack).prev; list_del((pRoot->stStack).prev); pstStack = list_entry(pTmp, myStack, stStack); return pstStack; } return NULL; }
main.c
#include "myStack.h" #include "malloc.h" myStack* getNewNode() { myStack *pTmp = NULL; pTmp = (myStack *)malloc(sizeof(myStack)); if (NULL == pTmp) { return NULL; } pTmp->data = 0; pTmp->pTree = NULL; INIT_LIST_HEAD(&(pTmp->stStack)); return pTmp; } void initStack(myStack *pRoot) { pRoot->data = 0; pRoot->pTree = NULL; INIT_LIST_HEAD(&(pRoot->stStack)); return; } void destoryStack(myStack *pRoot) { struct list_head *pos = NULL; struct list_head *n = NULL; myStack *pstStack = NULL; list_for_each_safe(pos, n, &(pRoot->stStack)) { list_del(pos); pstStack = list_entry(pos, myStack, stStack); free(pstStack); pos = n; } return; } int getStackLen(myStack *pRoot) { int len = 0; struct list_head *pos = NULL; list_for_each(pos, &(pRoot->stStack)) { len++; } return len; } int isStackEmpty(myStack *pRoot) { return list_empty(&(pRoot->stStack)); } void stackPush(myStack *pRoot, myStack *pNew) { list_add_tail(&(pNew->stStack), &(pRoot->stStack)); } myStack* stackPop(myStack *pRoot) { myStack *pstStack = NULL; struct list_head *pTmp = NULL; if (!isStackEmpty(pRoot)) { pTmp = (pRoot->stStack).prev; list_del((pRoot->stStack).prev); pstStack = list_entry(pTmp, myStack, stStack); return pstStack; } return NULL; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。