Deqeue&ArrayDeque

本文介绍了Java中的双端队列Deque,它不仅具备队列的特性,还能作为栈来使用。ArrayDeque是Deque的一个实现,基于循环数组,线程不安全,且在需要时会按2的幂进行扩容。文章通过理论和代码测试详细讲解了ArrayDeque的工作原理和使用方法。
摘要由CSDN通过智能技术生成

1.什么是Deqeue

Deque的含义是“double ended queue”,双端队列,传统队列操作受限FIFO,而这个队列可以双端进出,比较灵活;

它继承自Queue的(注意这儿是接口继承接口);

public interface Deque<E> extends Queue<E> {
   
	......
}

队列是一种特殊的线性容器,它是一种先进先出(FIFO)的数据结构。
它只允许在容器的头部进行删除操作,而在表的后端进行插入操作。进行插入操作的端成为队尾,进行删除操作的端称为队头。
当队列中没有元素时,被称为空队列。
同理当插入元素已经充满队列,被称为满队列.

再看下双端队列的Api:

可以看到扩展了不少功能,注意我划黄线的地方,可以看到它提供了一堆这种XXXfirst(),XXXlast()的方法。意思就是它不再是和队列一样,只能固定的FIFO的操作容器,而是既可以操作首,也可以操作尾,也可以LIFO;

而且它还有pop,push这样的方法,pop是哪儿的方法?看栈的Api:

意思就是说,依仗可以首尾操作的特性,它不仅仅可以作为一个队列来用,它还可以作为一个栈来用,并且官方也是建议用Dequeue替换Stack:

/**
 * ......
 * 更完整和一致的一组LIFO堆栈操作是由{@ Link Deque}接口及其实现提供应该优先使用这个类。
 * 
 * A more complete and consistent set of LIFO stack operations is
 * provided by the {@link Deque} interface and its implementations, which
 * should be used in preference to this class.  For example:
 * <pre>   
 * {@code
 *   Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
 *
 * @author  Jonathan Payne
 * @since   JDK1.0
 */
public
class Stack<E> extends Vector<E> {
   
      ......
}

小结下Deque的Api:它都是根据是否抛异常提供俩套api

然后对应总结下它和队列

帮我写出下列代码修正后的正确代码以及输出结果#include "stdio.h" #include "stdlib.h" #include "malloc.h" #include "string.h" #define MAXQSIZE 5 #define ERROR 0 #define OK 1 typedef struct {char *base; int front; int rear; int length; }hc_sqqueue; void main() {hc_sqqueue *initqueue_hc(); int cshqueue_hc(hc_sqqueue *q); int enqeue_hc(hc_sqqueue *q,char e); int deqeue_hc(hc_sqqueue *q); int printqueue_hc(hc_sqqueue *q); hc_sqqueue *q; char f,e; printf("建立队列(C)\n"); printf("初始化队列(N)\n"); printf("入队列元素(I)\n"); printf("出队列元素(D)\n"); printf("退出(E)\n\n"); do {printf("输入要做的操作:"); flushall(); f=getchar(); if(f=='C')q=initqueue_hc(); else if(f=='N') {cshqueue_hc(q);printqueue_hc(q);} else if(f=='I') {printf("输入要的入队的元素:"); flushall();e=getchar(); enqeue_hc(q,e);printqueue_hc(q);} else if(f=='D') {deqeue_hc(q);printqueue_hc(q);} }while(f!='E'); hc_sqqueue *initqueue_hc() {hc_sqqueue q; q=(hc_sqqueue)malloc(sizeof(hc_sqqueue)); if(!q)exit(ERROR); return(q);} int cshqueue_hc(hc_sqqueue q) {char e; int enqeue_hc(hc_sqqueue q,char e); q->base=(char)malloc(MAXQSIZEsizeof(char)); if(!q->base)exit(ERROR); q->front=q->rear=0;q->length=0; printf("输入元素以#结束:\n"); flushall(); e=getchar(); while(e!='#') {enqeue_hc(q,e); if(q->length==MAXQSIZE)return(ERROR); else {flushall();e=getchar();}} return(OK);} int enqeue_hc(hc_sqqueue *q,char e) {if(q->length==MAXQSIZE)return(ERROR); q->base[q->rear]=e; q->rear=(q->rear+1)%MAXQSIZE; q->length++; return(OK);} int deqeue_hc(hc_sqqueue *q) {if(q->length==0)return (ERROR); printf("出队的元素为:%c\n",q->base[q->front]); q->front=(q->front+1)%MAXQSIZE;q->length--; return (OK);} int printqueue_hc(hc_sqqueue *q) {int t=q->front; if(q->length==0){printf("队空!\n");return(ERROR);} if(q->length==MAXQSIZE)printf("队满!\n"); printf("当前队列中元素为:\n"); do{printf("%c\n",q->base[t]); t=(t+1)%MAXQSIZE;}while(t!=q->rear); return(OK);}
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值