数据结构之双端队列
来源:互联网 发布:禁止域用户安装软件 编辑:程序博客网 时间:2024/06/04 18:19
摘要:有时候一个队列可能需要能从两端进出,这时候称呼它为双端队列。具体思路很简单,从代码可以直接看出来。
#include "stdafx.h"#include "malloc.h"#include "stdlib.h"typedef struct DequeueRecord *queue;struct DequeueRecord{ int Capcity; int size; int Front ; int Rear; int * Array;};queue Create(int MaxElements){ queue Q; Q = (queue)malloc(sizeof(DequeueRecord)); Q->Capcity = MaxElements; Q->Array = (int *)malloc(sizeof(int )*MaxElements); Q->size = 0; Q->Front = -1; Q->Rear = Q->Capcity; return Q;}void Inject(queue Q,int x)//后端入队{ if (Q->size == Q->Capcity) { puts("error: it is impossible to insert an element inot a full dequeue"); return; } //插入到尾端 Q->Array[--Q->Rear] = x; Q->size++;}void Push(queue Q,int x)//前端入队{if (Q->size == Q->Capcity) { puts("error: it is impossible to insert an element inot a full dequeue"); return; }//插入前端Q->Array[++Q->Front] = x;Q->size++;} int Eject(queue Q)//后端出队{ if (Q->size == 0 ) { puts("it is impossible to delete an element from a empty dequeue"); return (-1); } //删除后端 Q->Rear = (Q->Capcity + Q->Rear)%Q->Capcity; Q->size -- ; return Q->Array[Q->Rear++];}int Pop(queue Q)//前端出队{ if (Q->size == 0 ) { puts("it is impossible to delete an element from a empty dequeue"); return (-1); } //删除前端 Q->Front = (Q->Capcity + Q->Front)%Q->Capcity; Q->size -- ; return Q->Array[Q->Front--];}int _tmain(int argc, _TCHAR* argv[]){ int i = 0; queue Q; Q = Create(20); while(i++<5) Push(Q,i); while(i-->1) Inject(Q,i); while(i++ <9) printf("%d ",Pop(Q)); puts("\n"); i = 0; while(i++<3) printf("%d",Eject(Q)); return 0;}
0 0
- 数据结构之双端队列
- 数据结构(队列):双端队列
- Java 数据结构之Deque(双端队列)
- 数据结构之队列(链式队列)
- 数据结构之队列(数组队列)
- 数据结构之队列-顺序队列
- 数据结构之循环队列
- 数据结构之顺序队列
- 数据结构之链队列
- java数据结构之队列
- 数据结构之队列
- 数据结构复习之【队列】
- 内核数据结构之队列
- 数据结构之队列实例
- 数据结构学习之队列
- 复习数据结构之队列
- C 队列之数据结构
- 基础数据结构之队列
- 【产品进阶】自己如何获取数据+分析数据
- HDU 5347 MZL's chemistry(水~)
- solr入门教程
- Spring依赖注入
- SQL:将查询结果插入到另一个表的三种情况
- 数据结构之双端队列
- 测试
- 深入理解Java内存模型
- 第一篇
- Python个人代码库---t3scan扫描器
- 高德软件有限公司python试题 及 答案
- busybos常见的错误详解
- adapter 中的viewholder的个人见解笔记
- 深入理解Java:SimpleDateFormat安全的时间格式化