栈的基本操作
myStack.h
创新互联主要从事成都网站设计、做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务田家庵,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792
#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; }
网站栏目:栈的基本操作
标题来源:http://hbruida.cn/article/ippcch.html