双端队列 ADT接口 链表实现
Deque ADT接口 DEQUEUE.h:
#include <stdlib.h>
#include "Item.h" typedef struct DEQUEUEnode *link;
struct DEQUEUEnode
{
Item item;
link next;
link last;
}; void DEQUEUEinit(int);
void DEQUEUEerror(void);
Item DEQUEUEheadget(void);
Item DEQUEUEtailget(void);
void DEQUEUEheadput(Item);
void DEQUEUEtailput(Item);
int DEQUEUEisEmpty(void);
int DEQUEUEisFull(void);
Deque ADT接口实现 DEQUEUE.c:
static link head,tail;
static int N; //备用 void DEQUEUEinit(int maxN)
{
head=malloc(sizeof(*head));
tail=head;
tail->next=NULL;
tail->last=NULL;
head->next=NULL;
head->last=NULL;
N=maxN;
}
void DEQUEUEerror(void)
{
printf("节点为空或节点已满");
exit();
}
Item DEQUEUEheadget(void)
{
Item temp;
temp=head->item;
head=head->next;
free(head->last);
head->last=NULL;
return temp;
}
Item DEQUEUEtailget(void)
{
Item temp;
tail=tail->last;
temp=tail->item;
free(tail->next);
tail->next=NULL;
return temp;
}
void DEQUEUEheadput(Item value)
{
head->last=malloc(sizeof(*head));
if(DEQUEUEisFull())
DEQUEUEerror();
head->last->next=head;
head=head->last;
head->item=value;
head->last=NULL;
}
void DEQUEUEtailput(Item value)
{
tail->item=value;
tail->next=malloc(sizeof(*head));
if(DEQUEUEisFull())
DEQUEUEerror();
tail->next->last=tail;
tail=tail->next;
tail->next=NULL;
}
int DEQUEUEisEmpty(void)
{
if(head==NULL&&tail==NULL)
return ;
return ;
}
int DEQUEUEisFull(void)
{
if((head==NULL&&tail!=NULL)||(head!=NULL&&tail==NULL))
return ;
return ;
}
Item.h:
1 typedef char Item;
主程序 main.c:
#include <stdio.h> int main(void)
{
int N; printf("输入需要申请内存大小:");
if(scanf("%d", &N))
DEQUEUEinit(N);
else
DEQUEUEerror();
getchar();
printf("输入%d个字符",N);
printf("('+'从队头get '*'从队尾get '大写字母'"
"从队头put '小写字母'从队尾put):\n"); while((N=getchar())!='\n')
{
switch(N)
{
case '+':
putchar(DEQUEUEheadget());
break;
case '*':
putchar(DEQUEUEtailget());
break;
default:
if(N>&&N<)
DEQUEUEheadput(N);
else
DEQUEUEtailput(N);
}
} return ;
}
双端队列 ADT接口 链表实现的更多相关文章
- 双端队列 ADT接口 数组实现
Deque ADT接口 DEQUEUE.h: #include <stdlib.h> #include "Item.h" void DEQUEUEinit(int); ...
- FIFO队列 ADT接口 链表实现
FIFO.h (接口) #include "Item.h" void QUEUinit(int); int QUEUempty(void); void QUEUput(Item); ...
- 自己动手实现java数据结构(四)双端队列
1.双端队列介绍 在介绍双端队列之前,我们需要先介绍队列的概念.和栈相对应,在许多算法设计中,需要一种"先进先出(First Input First Output)"的数据结构,因 ...
- 用Python实现的数据结构与算法:双端队列
一.概述 双端队列(deque,全名double-ended queue)是一种具有队列和栈性质的线性数据结构.双端队列也拥有两端:队首(front).队尾(rear),但与队列不同的是,插入操作在两 ...
- Python实现的数据结构与算法之双端队列详解
一.概述 双端队列(deque,全名double-ended queue)是一种具有队列和栈性质的线性数据结构.双端队列也拥有两端:队首(front).队尾(rear),但与队列不同的是,插入操作在两 ...
- Java 双端队列接口 Deque
Deque 是一种支持在两端进行操作的线性结构,包含了栈和队列的功能.Java 中建议使用 Dqueue 的实现来替代遗留的 Stack 类.本文将介绍 Deque 提供的主要 API. 双端操作 A ...
- PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)
1074 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed ...
- 用python实现栈/队列/双端队列/链表
栈是元素的有序集合,添加操作与移除操作都发生在其顶端,先进后出栈操作:创建空栈,增删(顶端),查(顶端元素,元素个数,是否为空)应用:将十进制数转换成任意进制数 class Stack: # 用列表创 ...
- Java 集合深入理解(10):Deque 双端队列
点击查看 Java 集合框架深入理解 系列, - ( ゜- ゜)つロ 乾杯~ 什么是 Deque Deque 是 Double ended queue (双端队列) 的缩写,读音和 deck 一样,蛋 ...
随机推荐
- 关于CATransform3D矩阵变换的简单解析
关于CATransform3D矩阵变换的简单解析 效果图: 我能能够用上的CATransform3D其实很简单,并不复杂. CATransform3D有着4种东西我们可以设置. 1. 透视效果(由m3 ...
- 用ISA2006配置单网卡缓存服务器
有些公司在部署ISA服务器之前已经有了自己的网络访问解决方案,例如通过硬件防火墙访问互联网,如果这种网络访问解决方案运行效果较好,公司就未必希望用ISA来替代当前的方案,毕竟稳定是第一位的.但在这种情 ...
- delete in javascript
Key word delete. 1. Delete global object. x = 42; // creates the property x on the global object var ...
- 8086 CPU 寄存器简介
转载:http://www.cnblogs.com/BoyXiao/archive/2010/11/20/1882716.html 引子 打算写几篇稍近底层或者说是基础的博文,浅要介绍或者说是回顾一些 ...
- The Shapes of CSS(css的形状)
All of the below use only a single HTML element. Any kind of CSS goes, as long as it's supported in ...
- July 31st 2017 Week 31st Monday
Elegance is the only beauty that never fades. 优雅是唯一不会褪色的美. Even the most beautiful apperace would be ...
- December 25th 2016 Week 53rd Sunday
Patience is bitter, but its fruit is sweet. 忍耐是痛苦的,但它的果实是甜蜜的. What can we do if there is no fruit of ...
- Python、R对比分析
一.Python与R功能对比分析 1.python与R相比速度要快.python可以直接处理上G的数据:R不行,R分析数据时需要先通过数据库把大数据转化为小数据(通过groupby)才能交给R做分析, ...
- 关于javascript的单线程和异步的一些问题
关于js单线程和异步方面突然就糊涂了,看别人的文章越看越糊涂,感觉这方面是个坑,跳进去就不好跳出来.再去看,看着看着感觉自己明白了一些东西,也不知道对不对,反正是暂时把自己说服了,这样理解能理解的通, ...
- Redis命令、数据结构场景、配置文件总结
本文大纲 一.常用数据类型简介二.redis操作命令三.redis配置文件详解四.redis数据类型使用场景 一.常用数据类型简介 redis常用五种数据类型:string,hash,list,set ...