【算法】队列的C语言实现
2014-12-28 00:25
323 查看
/*** *ElemType.h - ElemType的定义 * ****/ #ifndef ELEMTYPE_H #define ELEMTYPE_H typedef int ElemType; int compare(ElemType x, ElemType y); void visit(ElemType e); #endif /* ELEMTYPE_H */ /*** *ElemType.cpp - ElemType的实现 * ****/ #include <stdio.h> #include "ElemType.h" int compare(ElemType x, ElemType y) { return(x-y); } void visit(ElemType e) { printf("%d\n", e); } /*** *DynaLnkQueue.cpp - 动态链式队列,即队列的动态链式存储实现 * ****/ #include <stdlib.h> #include <malloc.h> #include <memory.h> #include <assert.h> #include "DynaLnkQueue.h" #define NULL 0 /*------------------------------------------------------------ 操作目的: 初始化队列 初始条件: 无 操作结果: 构造一个空的队列 函数参数: LinkQueue *Q 待初始化的队列 返回值: bool 操作是否成功 ------------------------------------------------------------*/ bool InitQueue(LinkQueue *Q) { Q ->front = Q ->rear = (QueuePtr)malloc(sizeof(QNode)); if(Q ->front == NULL) return false; Q ->front ->next = NULL; return true; } /*------------------------------------------------------------ 操作目的: 销毁队列 初始条件: 队列Q已存在 操作结果: 销毁队列Q 函数参数: LinkQueue *Q 待销毁的队列 返回值: 无 ------------------------------------------------------------*/ void DestroyQueue(LinkQueue *Q) { assert(Q != NULL); while(Q ->front) { Q ->rear = Q ->front ->next; free(Q ->front); Q ->front = Q ->rear; } } /*------------------------------------------------------------ 操作目的: 判断队列是否为空 初始条件: 队列Q已存在 操作结果: 若Q为空队列,则返回true,否则返回false 函数参数: LinkQueue Q 待判断的队列 返回值: bool 是否为空 ------------------------------------------------------------*/ bool QueueEmpty(LinkQueue Q) { assert(Q.front != NULL && Q.rear != NULL); if(Q.front == Q.rear) return true; else return false; } /*------------------------------------------------------------ 操作目的: 得到队列的长度 初始条件: 队列Q已存在 操作结果: 返回Q中数据元素的个数 函数参数: LinkQueue Q 队列Q 返回值: int 数据元素的个数 ------------------------------------------------------------*/ int QueueLength(LinkQueue Q) { assert(Q.front != NULL); QueuePtr p = Q.front; int Length = 0; while (p != Q.rear) { Length++; p = p->next; } return Length; } /*------------------------------------------------------------ 操作目的: 得到队列首元素 初始条件: 队列Q已存在 操作结果: 用e返回队列首元素 函数参数: LinkQueue Q 队列Q ElemType *e 队列首元素的值 返回值: bool 操作是否成功 ------------------------------------------------------------*/ bool GetHead(LinkQueue Q, ElemType *e) { assert(Q.front != NULL); if(QueueEmpty(Q)) return false; else { *e = Q.front ->next ->data; return true; } } /*------------------------------------------------------------ 操作目的: 遍历队列 初始条件: 队列Q已存在 操作结果: 依次对Q的每个元素调用函数fp 函数参数: LinkQueue Q 队列Q void (*fp)() 访问每个数据元素的函数指针 返回值: 无 ------------------------------------------------------------*/ void QueueTraverse(LinkQueue Q, void (*fp)(ElemType)) { assert(Q.front != NULL); QueuePtr p = Q.front ->next; while(p) { (*fp)(p ->data); p = p ->next; } } /*------------------------------------------------------------ 操作目的: 清空队列 初始条件: 队列Q已存在 操作结果: 将队列清空 函数参数: LinkQueue *Q 队列Q 返回值: 无 ------------------------------------------------------------*/ void ClearQueue(LinkQueue *Q) { assert(Q ->front != NULL); QueuePtr p = Q ->front ->next; while(p) { Q ->front ->next = p ->next; free(p); p = Q ->front ->next; } } /*------------------------------------------------------------ 操作目的: 在队列末尾插入元素e 初始条件: 队列Q已存在 操作结果: 插入元素e作为队列新的尾结点 函数参数: LinkQueue *Q 队列Q ElemType e 待插入的数据元素 返回值: bool 操作是否成功 ------------------------------------------------------------*/ bool EnQueue(LinkQueue *Q, ElemType e) { QueuePtr temp = (QueuePtr )malloc(sizeof(QNode)); if(!temp) return false; temp ->data = e; temp ->next = NULL; Q->rear ->next = temp; Q ->rear = temp; return true; } /*------------------------------------------------------------ 操作目的: 删除链式队列的头结点 初始条件: 队列Q已存在 操作结果: 删除链式队列的头结点 函数参数: LinkQueue *Q 队列Q ElemType *e 待插入的数据元素 返回值: bool 操作是否成功 ------------------------------------------------------------*/ bool DeQueue(LinkQueue *Q, ElemType *e) { if(Q ->front == Q->rear) return false; QueuePtr temp = Q->front ->next; *e = temp ->data; Q ->front ->next= temp ->next; if(Q ->rear == temp) Q ->rear = Q ->front; free(temp); return true; } /*** *DynaLnkQueue.h - 动态链式队列的定义 * ****/ #if !defined(DYNALNKQUEUE_H) #define DYNALNKQUEUE_H #include "ElemType.h" /*------------------------------------------------------------ // 链式队列结构的定义 ------------------------------------------------------------*/ typedef struct Node { ElemType data; // 元素数据 struct Node *next; // 链式队列中结点元素的指针 } QNode, *QueuePtr; typedef struct { QueuePtr front; // 队列头指针 QueuePtr rear; // 队列尾指针 } LinkQueue; /*------------------------------------------------------------ // 链式队列的基本操作 ------------------------------------------------------------*/ bool InitQueue(LinkQueue *Q); void DestroyQueue(LinkQueue *Q); bool QueueEmpty(LinkQueue Q); int QueueLength(LinkQueue Q); bool GetHead(LinkQueue Q, ElemType *e); void QueueTraverse(LinkQueue Q, void (*fp)(ElemType)); void ClearQueue(LinkQueue *Q); bool EnQueue(LinkQueue *Q, ElemType e); bool DeQueue(LinkQueue *Q, ElemType *e); #endif /* DYNALNKQUEUE_H */ #include <stdio.h> #include <stdlib.h> #include "DynaLnkQueue.h" int main() { // TODO: Place your test code here /*LinkQueue W; InitQueue(&W); EnQueue(&W,1); EnQueue(&W,2); QueueTraverse(W,visit);*/ system("PAUSE"); return 0; }
相关文章推荐
- 《算法导论》第六章之堆和优先级队列相关算法C语言实现
- 算法与数据结构【四】——C语言实现循环队列
- 数据结构C语言实现之链式队列的6种算法代码
- 寒假算法第一编——C语言实现队列
- 算法与数据结构-队列的基本操作C语言实现
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 一个UUID生成算法的C语言实现 --- WIN32版本
- 一个UUID生成算法的C语言实现 --- WIN32版本
- 数据结构C语言实现系列——队列
- 10个重要的算法C语言实现源代码(其4和5---龙贝格和牛顿迭代)
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- C语言实现的可动态增长的队列
- 10个重要的算法C语言实现源代码(8-9-10-----秦九昭和幂法和高斯塞德尔)
- 10个重要的算法C语言实现源代码(其1-拉格朗日)
- 10个重要的算法C语言实现源代码(其2--牛顿插值)
- C语言中实现点在多边形内的算法
- 一个UUID生成算法的C语言实现 --- WIN32版本
- C语言实现的扫描线种子填充算法
- md5 算法的c语言实现及应用