队列的基本操作-创新互联
structComm.h //定义各种结构的头文件
创新互联服务项目包括鄂城网站建设、鄂城网站制作、鄂城网页制作以及鄂城网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,鄂城网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到鄂城省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!#ifndef STRUCTCOMM_H_INCLUDED #define STRUCTCOMM_H_INCLUDED #undef NULL #ifdef __cplusplus #define NULL 0 #else #define NULL ((void *)0) #endif struct list_head { struct list_head *next; struct list_head *prev; }; typedef struct tag_myTree { int data; struct tag_myTree* pLeft; struct tag_myTree* pRight; }myTree; typedef struct tag_myStack { int data; myTree *pTree; struct list_head stStack; }myStack; typedef struct tag_myQue { int data; myTree *pTree; struct list_head stQue; }myQue; #endif // STRUCTCOMM_H_INCLUDED
myQue.h
#ifndef MYQUE_H_INCLUDED #define MYQUE_H_INCLUDED #include "structComm.h" myQue* getNewNode(); void initQue(myQue *pRoot); void destoryQue(myQue *pRoot); int getQueLen(myQue *pRoot); int isQueEmpty(myQue *pRoot); void enQue(myQue *pRoot, myQue *pNew); myQue* deQue(myQue *pRoot); #endif // MYQUE_H_INCLUDED
myQue.c
#include "myQue.h" #include "myList.h" myQue* getNewQueNode() { myQue *pTmp = NULL; pTmp = (myQue *)malloc(sizeof(myQue)); if (NULL == pTmp) { return NULL; } pTmp->data = 0; pTmp->pTree = NULL; INIT_LIST_HEAD(&(pTmp->stQue)); return pTmp; } void initQue(myQue *pRoot) { pRoot->data = 0; pRoot->pTree = NULL; INIT_LIST_HEAD(&(pRoot->stQue)); return; } void destoryQue(myQue *pRoot) { struct list_head *pos = NULL; struct list_head *n = NULL; myQue *pstQue = NULL; list_for_each_safe(pos, n, &(pRoot->stQue)) { list_del(pos); pstQue = list_entry(pos, myQue, stQue); free(pstQue); pos = n; } return; } int getQueLen(myQue *pRoot) { int len = 0; struct list_head *pos = NULL; list_for_each(pos, &(pRoot->stQue)) { len++; } return len; } int isQueEmpty(myQue *pRoot) { return list_empty(&(pRoot->stQue)); } void enQue(myQue *pRoot, myQue *pNew) { list_add(&(pNew->stQue), &(pRoot->stQue)); } myQue* deQue(myQue *pRoot) { myQue *pstQue = NULL; struct list_head *pTmp = NULL; if (!isQueEmpty(pRoot)) { pTmp = (pRoot->stQue).prev; list_del((pRoot->stQue).prev); pstQue = list_entry(pTmp, myQue, stQue); return pstQue; } return NULL; }
main.c
#include#include "myQue.h" myQue g_stQueRoot; //栈的头节点,不存储数据 int main() { int i = 0; myQue *pNewNode = NULL; initQue(&g_stQueRoot); //初始化之前要保证头队列为空 printf("is empty:\t%d\n", isQueEmpty(&g_stQueRoot)); for (i = 0; i < 5; i++) { pNewNode = getNewNode(); if (NULL == pNewNode) { exit(0); } pNewNode->data = i; enQue(&g_stQueRoot, pNewNode); } printf("is empty:\t%d\n", isQueEmpty(&g_stQueRoot)); printf("que len:\t%d\n", getQueLen(&g_stQueRoot)); printf("de:\t"); for (i = 0; i < 5; i++) { pNewNode = deQue(&g_stQueRoot); printf("%d\t", pNewNode->data); } pNewNode = deQue(&g_stQueRoot); if (NULL == pNewNode) { printf("\ncan not de\n"); } return 0; }
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章标题:队列的基本操作-创新互联
网页链接:http://ybzwz.com/article/ceedjp.html