双端队列
来源:互联网 发布:mac os 10.9 安装u盘 编辑:程序博客网 时间:2024/06/05 08:16
#include <stdio.h>
#define QUEUESIZE 8
typedef char DataType;
typedef struct DQueue
{
DataType queue[QUEUESIZE];
int end1;
int end2;
}DQueue;
int EnQueue(DQueue *DQ, DataType e, int tag)
{
switch (tag)
{
case 1:
if (DQ->end1 != DQ->end2)
{
DQ->queue[DQ->end1] = e;
DQ->end1 = (DQ->end1 - 1) % QUEUESIZE;
return 1;
}
else
{
return 0;
}
break;
case 2:
if (DQ->end1 != DQ->end2)
{
DQ->queue[DQ->end2] = e;
DQ->end2 = (DQ->end2 + 1) % QUEUESIZE;
return 1;
}
else
{
return 0;
}
break;
}
return 0;
}
int DeQueue(DQueue *DQ, DataType *e, int tag)
{
switch (tag)
{
case 1:
if ((DQ->end1 + 1) % QUEUESIZE != DQ->end2)
{
DQ->end1 = (DQ->end1 + 1) % QUEUESIZE;
*e = DQ->queue[DQ->end1];
return 1;
}
else
{
return 0;
}
break;
case 2:
if ((DQ->end2 - 1) % QUEUESIZE != DQ->end1)
{
DQ->end2 = (DQ->end2 - 1) % QUEUESIZE;
*e = DQ->queue[DQ->end2];
return 1;
}
else
{
return 0;
}
break;
}
return 0;
}
//利用顺序存储结构实现双端队列的入队和出队操作
void main()
{
DQueue Q;
char ch;
Q.end1 = 3;
Q.end2 = 4;
if (!EnQueue(&Q, 'a', 1))
printf("队列已满,不能入队!");
else
printf("a左端入队:\n");
if (!EnQueue(&Q, 'b', 1))
printf("队列已满,不能入队!");
else
printf("b左端入队:\n");
if (!EnQueue(&Q, 'c', 1))
printf("队列已满,不能入队!");
else
printf("c左端入队:\n");
if (!EnQueue(&Q, 'd', 2))
printf("队列已满,不能入队!");
else
printf("d右端入队:\n");
if (!EnQueue(&Q, 'e', 2))
printf("队列已满,不能入队!");
else
printf("e右端入队:\n");
printf("队列左端出队一次:");
DeQueue(&Q, &ch, 1);
printf("%c\n", ch);
printf("队列左端出队一次:");
DeQueue(&Q, &ch, 1);
printf("%c\n", ch);
printf("队列右端出队一次:");
DeQueue(&Q, &ch, 2);
printf("%c\n", ch);
printf("队列右端出队一次:");
DeQueue(&Q, &ch, 2);
printf("%c\n", ch);
}
#define QUEUESIZE 8
typedef char DataType;
typedef struct DQueue
{
DataType queue[QUEUESIZE];
int end1;
int end2;
}DQueue;
int EnQueue(DQueue *DQ, DataType e, int tag)
{
switch (tag)
{
case 1:
if (DQ->end1 != DQ->end2)
{
DQ->queue[DQ->end1] = e;
DQ->end1 = (DQ->end1 - 1) % QUEUESIZE;
return 1;
}
else
{
return 0;
}
break;
case 2:
if (DQ->end1 != DQ->end2)
{
DQ->queue[DQ->end2] = e;
DQ->end2 = (DQ->end2 + 1) % QUEUESIZE;
return 1;
}
else
{
return 0;
}
break;
}
return 0;
}
int DeQueue(DQueue *DQ, DataType *e, int tag)
{
switch (tag)
{
case 1:
if ((DQ->end1 + 1) % QUEUESIZE != DQ->end2)
{
DQ->end1 = (DQ->end1 + 1) % QUEUESIZE;
*e = DQ->queue[DQ->end1];
return 1;
}
else
{
return 0;
}
break;
case 2:
if ((DQ->end2 - 1) % QUEUESIZE != DQ->end1)
{
DQ->end2 = (DQ->end2 - 1) % QUEUESIZE;
*e = DQ->queue[DQ->end2];
return 1;
}
else
{
return 0;
}
break;
}
return 0;
}
//利用顺序存储结构实现双端队列的入队和出队操作
void main()
{
DQueue Q;
char ch;
Q.end1 = 3;
Q.end2 = 4;
if (!EnQueue(&Q, 'a', 1))
printf("队列已满,不能入队!");
else
printf("a左端入队:\n");
if (!EnQueue(&Q, 'b', 1))
printf("队列已满,不能入队!");
else
printf("b左端入队:\n");
if (!EnQueue(&Q, 'c', 1))
printf("队列已满,不能入队!");
else
printf("c左端入队:\n");
if (!EnQueue(&Q, 'd', 2))
printf("队列已满,不能入队!");
else
printf("d右端入队:\n");
if (!EnQueue(&Q, 'e', 2))
printf("队列已满,不能入队!");
else
printf("e右端入队:\n");
printf("队列左端出队一次:");
DeQueue(&Q, &ch, 1);
printf("%c\n", ch);
printf("队列左端出队一次:");
DeQueue(&Q, &ch, 1);
printf("%c\n", ch);
printf("队列右端出队一次:");
DeQueue(&Q, &ch, 2);
printf("%c\n", ch);
printf("队列右端出队一次:");
DeQueue(&Q, &ch, 2);
printf("%c\n", ch);
}
0 0
- 队列,优先队列,双端队列
- 队列、双端队列与优先队列
- Java 模拟队列(一般队列、双端队列、优先级队列)
- 栈,队列,双端队列
- 队列与双端队列
- 数据结构(队列):双端队列
- [zz]栈和队列,双端队列
- 队列的应用:双端队列
- 队列Queue、双端队列Deque
- nyoj1117 鸡蛋队列 (双端队列,deque)
- Java队列Queue、双端队列Deque
- 四.队列与双端队列
- java队列Queue、双端队列Deque
- 队列Queue、双端队列Deque
- 队列Queue、双端队列Deque
- 单调栈、双端队列、单调队列
- java-队列,双端队列、栈
- 栈和队列、双端队列
- AndroidUtils——DensityUtils
- 升级gcc
- 采用递归逆序打印单链表(递归出口的三种写法)
- 【UNITY多语言化问题】NGUI的多语言本地化Localization到底能不能使用
- RN----导入组件,import from 'xxxx'的用法详解
- 双端队列
- MyEclipse中怎么快速生成get和set函数
- 测试驱动开发心得体会
- (转)一些你需要知道的布局优化技巧
- mysql学习笔记之管理维护(三)
- 解方程 NOIP2014 提高组 Day2 T3
- 关于go的MD5 hash 加密算法
- 怎样在 CentOS 里下载 RPM 包及其所有依赖包
- 队列在杨辉三角中的应用