不定长链表队列C语言实现
#ifndef _CONST_H_
#define _CONST_H_
#include <stdio.h>
#include <stdlib.h>
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语言实现
#ifndef _CONST_H_#define _CONST_H_ #include <stdio.h>#include <stdlib.h> typedef enum { ...
- Stm32使用串口空闲中断,基于队列来接收不定长、不定时数据
串口持续地接收不定长.不定时的数据,把每一帧数据缓存下来且灵活地利用内存空间,下面提供一种方式供参考.原理是利用串口空闲中断和DMA,每当对方发来一帧完整的数据后,串口接收开始空闲,触发中断,在中断处 ...
- C语言格式化输入不定长数组
先随便写写,有空再整理. 直接贴代码 #include <stdio.h> #include <stdlib.h> //从一行标准输入中格式化输入一个不定长数组 void in ...
- C语言 复杂队列(链表队列)
//复杂的队列二 --链表队列 #include<stdio.h> #include<stdlib.h> #define datatype int struct queueli ...
- 数据结构:C_链表队列的实现
数据结构链表形式队列的实现(C语言版) 1.写在前面 队列是一种和栈相反的,遵循先进先出原则的线性表. 本代码是严蔚敏教授的数据结构书上面的伪代码的C语言实现代码. 分解代码没有包含在内的代码如下: ...
- 不定长内存池之apr_pool
内存池可有效降低动态申请内存的次数,减少与内核态的交互,提升系统性能,减少内存碎片,增加内存空间使用率,避免内存泄漏的可能性,这么多的优点,没有理由不在系统中使用该技术. 内存池分类: 1. ...
- iOS实现传递不定长的多个参数
我们在使用苹果官方的文档的时候会发现可传不定数的参数例如: // [[UIAlertView alloc]initWithTitle:<#(nullable NSString *)#> m ...
- 学习javaScript必知必会(1)~js介绍、函数、匿名函数、自调用函数、不定长参数
一.简单了解一下JavaScript(js) 1.什么是js? js:是网景公司开发的,是基于客户端浏览器, 面向(基于)对象.事件驱动式的页面脚本语言. 2.什么场景下使用到js? 表单验证.页面特 ...
- STM32串口接收不定长数据原理与源程序(转)
今天说一下STM32单片机的接收不定长度字节数据的方法.由于STM32单片机带IDLE中断,所以利用这个中断,可以接收不定长字节的数据,由于STM32属于ARM单片机,所以这篇文章的方法也适合其他的A ...
随机推荐
- Issue 4:分布式的基础性问题
最近打算聊一聊分布式的基本思想.定了个大致的技术路线. 分布式难点 分片和元数据管理 多副本策略 多副本一致性方案 并发和事务支持
- iOS进行Basic认证与NTLM认证
一.iOS进行Basic认证 只需要在NSMutableURLRequest的Header中添加认证所需的Username和password. NSMutableURLRequest *webReq ...
- Linux 发行版本及其基于
Independent ---> Debian Debian/Ubuntu(LTS) ---> Linux Mint ---> Linux Lite Debian/Ubuntu -- ...
- 利用zlib库进行zip解压
1:到zlib官网上下载zlib,本文下载的是1.2.8的版本. 2:进行./configure,然后make. 3:进入zlib库中的contrib/minizip/路径下make,生成的miniz ...
- jQuery实现按enter键登录
<script> $(document).keydown(function (event) { if (event.keyCode == 13) { $("#btn_Login& ...
- 移动设备如何打开RMS加密的文档
关键字:RMS. AZure RMS.IPhone.Android.Office365.Sharepoint.Exchange 最近总是碰到要求用苹果手机及安卓手机阅读RMS加密文档的需求,经过查找相 ...
- 如何写出优雅的Python
Looping over a range of numbers Bad: for i in [0,1,2,3,4,5]: print i**2 Good: for i in range(6): pri ...
- Android 短信广播接收相关问题
本人是Android新手,最近做了一个关于监听手机短信功能的应用,我在网上看资料了解到广播分为有序广播和无序广播,有序广播:无序广播又称普通广播,其中的利弊我也一时没搞清楚,我用的是有序广播实现的,具 ...
- linux 下链接无线网络
无线网卡配置此页由Linux Wiki用户Chenxing于2008年11月27日 (星期四) 09:28的最后更改. 在1233456的工作基础上.本文介绍在Linux命令行界面中手动配置无线网卡的 ...
- 详解:数据库名、实例名、ORACLE_SID、数据库域名、全局数据库名、服务名及手工脚本创建oracle数据库
数据库名.实例名.数据库域名.全局数据库名.服务名 , 这是几个令很多初学者容易混淆的概念.相信很多初学者都与我一样被标题上这些个概念搞得一头雾水.我们现在就来把它们弄个明白. 一.数据库名 什么是数 ...