摘要:本文主要向大家介绍了C/C++知识点之不定长链表队列C语言实现,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
本文主要向大家介绍了C/C++知识点之不定长链表队列C语言实现,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
#ifndef _CONST_H_#define _CONST_H_#include
typedef enum { False = 0, True,}Bool;
typedef int ElemType;
#define QUEUE_MAX_SIZE 10
#define STACK_INIT_SIZE 10#define STACK_INCREMENT_SIZE 2
#define Null ((void *)0)
typedef enum{ NORMAL = 0, ERROR, UNDERFLOW, OVERFLOW, STATUSCOUNT,}Status;
#endif
#ifndef _LINKED_QUEUE_H_#define _LINKED_QUEUE_H_
#include "Const.h"
typedef struct node{ ElemType data; struct node *pNext;}Node, *pNode;
typedef struct linkedqueue{ pNode pfront; pNode prear;}LinkedQueue, *pLinkedQueue;
Status InitLinkedQueue(pLinkedQueue pQ);
Bool IsLinkedQueueEmpty(pLinkedQueue pQ);
Bool EnLinkedQueue(pLinkedQueue pQ, ElemType elem);
Bool DeLinkedQueue(pLinkedQueue pQ, ElemType *e);
void DestoryLinkedQueue(pLinkedQueue pQ);
void ClearLinkedQueue(pLinkedQueue pQ);
Status GetLinkedQueueHead(pLinkedQueue pQ, ElemType *e);
int GetLinkedQueueLength(pLinkedQueue pQ);
Bool InvertLinkedQueue_Static(pLinkedQueue pQ);
Bool InvertLinkedQueue_Dynamic(pLinkedQueue pQ);
#endif
#include "LinkedQueue.h"#include "Const.h"#include "StaticStack.h"#include "DynamicStack.h"
Status InitLinkedQueue(pLinkedQueue pQ){ pNode pN = (pNode)malloc(sizeof(Node)); if (pN == Null) { printf("No accessable free memory.\n"); return ERROR; } pN->pNext = Null; pQ->pfront = pN; pQ->prear = pN;}
Bool IsLinkedQueueEmpty(pLinkedQueue pQ){ if (pQ->pfront == pQ->prear) { return True; } else { return False; }}
Bool EnLinkedQueue(pLinkedQueue pQ, ElemType elem){ pNode pTempNode = (pNode)malloc(sizeof(Node)); if (pTempNode == Null) { printf("No accessable free memory.\n"); return False; }
pTempNode->data = elem; pTempNode->pNext = Null;
pQ->prear->pNext = pTempNode; pQ->prear = pTempNode;}
Bool DeLinkedQueue(pLinkedQueue pQ, ElemType *e){ if (IsLinkedQueueEmpty(pQ)) { printf("The Queue Is Empty.\n"); return False; } else { pNode pTempNode = pQ->pfront->pNext; *e = pTempNode->data; pQ->pfront->pNext = pTempNode->pNext; //Only has one node in this linked queue. if (pQ->prear == pTempNode) { pQ->prear = pQ->pfront; } free(pTempNode); return True; }}
void DestoryLinkedQueue(pLinkedQueue pQ){ pNode p = pQ->pfront; pNode q = Null; while(p != Null) { q = p; p = p->pNext; free(q); } pQ->pfront = Null; pQ->prear = Null;}
void ClearLinkedQueue(pLinkedQueue pQ){ pNode p = pQ->pfront->pNext; pNode q = Null; while(p != Null) { q = p; p = p->pNext; free(q); } pQ->pfront->pNext = Null; pQ->prear = pQ->pfront;}
Status GetLinkedQueueHead(pLinkedQueue pQ, ElemType *e){ if (IsLinkedQueueEmpty(pQ)) { printf("The linked queue is empty.\n"); return ERROR; } *e = pQ->pfront->pNext->data; return NORMAL;}
int GetLinkedQueueLength(pLinkedQueue pQ){ if (IsLinkedQueueEmpty(pQ)) { return 0; } int Count = 0; pNode pTemp = pQ->pfront;
while(pTemp->pNext != Null) { Count++; pTemp = pTemp->pNext; } return Count;}
Status QueueTraverse(pLinkedQueue pQ, void(*func)(ElemType*)){ pNode pTemp = pQ->pfront->pNext; while(pTemp != Null) { func(&pTemp->data); pTemp = pTemp->pNext; } return NORMAL;}
Bool InvertLinkedQueue_Static(pLinkedQueue pQ){ pStaticStack pSS = (pStaticStack)malloc(sizeof(StaticStack)); if (NORMAL != InitStaticStack(pSS)) { return False; }
ElemType V; while(!IsLinkedQueueEmpty(pQ)) { DeLinkedQueue(pQ, &V); PushStaticStack(pSS, V); }
while(!IsStaticStackEmpty(pSS)) { PopStaticStack(pSS, &V); EnLinkedQueue(pQ, V); } DestoryStaticStack(pSS); return True;}
Bool InvertLinkedQueue_Dynamic(pLinkedQueue pQ){ pDynamicStack pDS = (pDynamicStack)malloc(sizeof(DynamicStack)); if (NORMAL != InitDynamicStack(pDS)) { return False; }
ElemType V; while(!IsLinkedQueueEmpty(pQ)) { DeLinkedQueue(pQ, &V); PushDynamicStack(pDS, V); }
while(!IsDynamicStackEmpty(pDS)) { PopDynamicStack(pDS, &V); EnLinkedQueue(pQ, V); } DestoryDynamicStack(pDS); return True;}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号