不定长链表队列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 ...
随机推荐
- AFN和ASI区别
AFN和ASI区别 一.AFN和ASI的区别 1.底层实现1> AFN的底层基于OC的NSURLConnection和NSURLSession2> ASI的底层基于纯C语言的CFNetwo ...
- Java提高篇——对象克隆(复制)
假如说你想复制一个简单变量.很简单: int apples = 5; int pears = apples; 不仅仅是int类型,其它七种原始数据类型(boolean,char,byte,short, ...
- MVC之权限管理-网站开发之路
一.前言 刚到公司没多长时间就开始接触MVC到现在不能说懂了,只能说到达会用这个层次吧,感觉MVC用来写Web还是很强大的,层次清晰. 今天我来写写关于权限管理这一块,自我感觉网站的权限主要分为菜单权 ...
- 一个nginx匹配很诡异的问题
nginx配置如下: location ~ \.php$ { root /opt/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index inde ...
- async?
Here, I want to record one thing, as to async and await methods, I've seen many misuses. Since these ...
- (二)ADS1.2的安装教程以及使用 调试 (不会 AXD 调试工具)
安装教程: 参考百度 http://jingyan.baidu.com/article/cdddd41c7db85253cb00e1ae.html 具体使用看: 杨铸的那本书(嵌入式底层软件驱动开发) ...
- vmware workstation unrecoverable error: (vmui)报错解决方法
实验室7月份刚换了电脑,之前一直用vmware来跑linux搞嵌入式开发,无论是宿舍的笔记本,还是之前用的旧台式机,都可以妥妥的跑vmware没有问题,结果换了新电脑之后,装上vmware works ...
- Custom work flow
http://runjs.cn/detail/99epj1t2 http://www.cqroad.cn/ https://jsplumbtoolkit.com/demo/flowchart/dom. ...
- ps一些图片的操作
ctrl+c 复制 ctrl+n 新建 ctrl+v粘贴 ctrl+s 保存 如果要将某个带字的背景去掉它的字体那么就是 ctrl+t
- Oracle RAC安装部署文档
1. 部署环境步骤 1.1 软件环境 操作系统:CentOS release 6.5(推荐使用5.*的系统)192.168.1.151 racnode1 192.168.1.152 ...