您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。