一.代码部分

1.定义接口:

public interface Queue<E> {

    void enqueue(E e);
E dequeue();
E getFront();
int getSize();
boolean isEmpty(); }

2.基于数组的实现

public class ArrayQueue<E> implements Queue<E> {

    private ArrayList<E> arrayList;
public ArrayQueue(int capacity){
arrayList = new ArrayList<>(capacity);
}
public ArrayQueue(){
arrayList = new ArrayList<>();
} @Override
public void enqueue(E e) {
arrayList.addLast(e);
} @Override
public E dequeue() {
return arrayList.removeFirst();
} @Override
public E getFront() {
return arrayList.get(0);
} @Override
public int getSize() {
return arrayList.getSize();
} @Override
public boolean isEmpty() {
return arrayList.isEmpty();
} }

3.改进链表实现队列:

public class LinkedListQueue<E> implements Queue<E> {

    //节点,用来存放数据:数据+下一个元素的引用
private class Node{
private E e;
private Node next;
public Node(E e,Node next){
this.e = e;
this.next = next;
}
public Node(E e){
this(e,null);
}
public Node(){
this(null,null);
}
public String toString(){
return e.toString();
}
}
private Node head;//头节点
private Node tail;//尾节点
private int size; public LinkedListQueue(){
head = null;
tail = null;
size =0;
} @Override
public void enqueue(E e) {
if (tail == null){
tail = new Node(e);
head = tail;
}else {
tail.next = new Node(e);
tail = tail.next;
}
size++;
} @Override
public E dequeue() {
if(isEmpty())
throw new IllegalArgumentException("empty");
Node retNode = head;
head = head.next;
retNode.next = null; if (head == null){
tail = null;
}
size--;
return retNode.e;
} @Override
public E getFront() {
if(isEmpty())
throw new IllegalArgumentException("empty");
return head.e;
} @Override
public int getSize() {
return size;
} @Override
public boolean isEmpty() {
return size ==0;
} @Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("queue:front");
Node cur = head;
while (cur != null){
res.append(cur + "->");
cur = cur.next; }
res.append("null tail");
return res.toString();
}
}

3.循环队列:

public class LoopQueue<E> {

    private E[] data;
private int front;//队列头
private int tail;//队列尾
private int size; /**
* 初始化容量的构造方法
* @param capacity
*/
public LoopQueue(int capacity){
data = (E[]) new Object[capacity+1]; //注意加一
front = 0;
tail = 0;
size = 0;
} /**
* 无参的构造方法
*/
public LoopQueue(){
this(10);
} public int getCapacity(){
return data.length-1;
} public boolean isEmpty(){
return front == tail;
} public int getSize(){
return size;
} /**
* 改变数组容量
* @param newCapacity
*/
private void resize(int newCapacity){
E[] newData = (E[]) new Object[newCapacity+1];
for (int i = 0; i < size; i++) {
newData[i] = data[(i+front)%data.length];
}
data = newData;
front = 0;
tail = size;
} /**
* 入队
* @param e
*/
public void enqueue(E e){
if ((tail+1)%data.length == front){
resize(getCapacity()*2);
}
data[tail] = e;
tail = (tail+1)%data.length;//注意这里不是tail++
size++;
} /**
* 出队
* @return
*/
public E dequeue(){
if (isEmpty()){
throw new IllegalArgumentException("can't dequeue");
}
E ret = data[front];
data[front] = null;
front = (front+1)%data.length;//注意这里不是front++
size--;
if(size == getCapacity()/4 && getCapacity()/2 != 0){
resize(getCapacity()/2);
}
return ret;
} /**
* 队列头
* @return
*/
public E getFront(){
if (isEmpty()){
throw new IllegalArgumentException("queue is empty");
}
return data[front];
} @Override
public String toString() { StringBuilder res = new StringBuilder();
res.append(String.format("queue size =%d, capacity = %d\n"),size,getCapacity());
res.append("front [");
for (int i = front; i != tail; i = (i=1)%data.length) {
res.append(data[i]);
if((i+1)%data.length != tail){
res.append(",");
}
}
res.append("] tail");
return res.toString();
}
}

自己实现数据结构系列四---Queue的更多相关文章

  1. 【JavaScript数据结构系列】03-队列Queue

    [JavaScript数据结构系列]03-队列Queue 码路工人 CoderMonkey 转载请注明作者与出处 1. 认识队列Queue结构 队列,跟我们的日常生活非常贴近,我们前面举例了食堂排队打 ...

  2. <数据结构系列3>队列的实现与变形(循环队列)

    数据结构第三课了,今天我们再介绍一种很常见的线性表——队列 就像它的名字,队列这种数据结构就如同生活中的排队一样,队首出队,队尾进队.以下一段是百度百科中对队列的解释: 队列是一种特殊的线性表,特殊之 ...

  3. 【C#数据结构系列】查找

    一:查找 1.1 基本概念和术语 查找(Search)是在数据结构中确定是否存在关键码等于给定关键码的记录的过程.关键码有主关键码和次关键码.主关键码能够唯一区分各个不同的记录,次关键码通常不能唯一区 ...

  4. <数据结构系列2>栈的实现与应用(LeetCode<有效的的括号>)

    首先想要实现栈,就得知道栈为何物,以下一段摘抄至百度百科: 栈(stack)又名堆栈,它是一种运算受限的线性表.其限制是仅允许在表的一端进行插入和删除运算.这一端被称为栈顶,相对地,把另一端称为栈底. ...

  5. 【JavaScript数据结构系列】04-优先队列PriorityQueue

    [JavaScript数据结构系列]04-优先队列PriorityQueue 码路工人 CoderMonkey 转载请注明作者与出处 ## 1. 认识优先级队列 经典的案例场景: 登机时经济舱的普通队 ...

  6. 【JavaScript数据结构系列】00-开篇

    [JavaScript数据结构系列]00-开篇 码路工人 CoderMonkey 转载请注明作者与出处 ## 0. 开篇[JavaScript数据结构与算法] 大的计划,写以下两部分: 1[JavaS ...

  7. 前端构建大法 Gulp 系列 (四):gulp实战

    前端构建大法 Gulp 系列 (一):为什么需要前端构建 前端构建大法 Gulp 系列 (二):为什么选择gulp 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gulp专家 前 ...

  8. JAVA数据结构系列 栈

    java数据结构系列之栈 手写栈 1.利用链表做出栈,因为栈的特殊,插入删除操作都是在栈顶进行,链表不用担心栈的长度,所以链表再合适不过了,非常好用,不过它在插入和删除元素的时候,速度比数组栈慢,因为 ...

  9. Netty4.x中文教程系列(四) 对象传输

    Netty4.x中文教程系列(四)  对象传输 我们在使用netty的过程中肯定会遇到传输对象的情况,Netty4通过ObjectEncoder和ObjectDecoder来支持. 首先我们定义一个U ...

随机推荐

  1. 关于java中assert(断言)的使用讲解

    说明:写的不是很全面,有任何问题请留言,多交流,谢谢! 1.eclipse.myeclipse开启assert(断言),默认是关闭,如下: 说白了就是设置一下jvm的参数,参数是-ea或者-enabl ...

  2. [Hive_add_10] Hive 的 serde (序列化 & 反序列化) 操作

    0. 说明 serde 是序列化和反序列化 serialize & deserialize 是将文件中的字段映射成 Hive 中的列 1. 验证 1.1 openCSVSerde CSV 为逗 ...

  3. win10优化开机进程

    一种比杀毒软件都优化还给力的方法,还在为开机几百个程序启动发愁嘛.一般电脑在重装系统之后的开机进程在50左右,而随着安装程序的增多开机进程将越来越多.下面介绍怎么优化win10进程的方法 之后重启电脑 ...

  4. MATLAB简易画图

    给定一组特殊点,连线作图 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 以成绩隶属函数为例: score.m cj_x1=[ 0.1]; cj_y1= ...

  5. 直接运行vue+django项目

    直接运行vue+django项目 下载前后端代码 wget https://files.cnblogs.com/files/pyyu/luffy_boy.zip wget https://files. ...

  6. 并发控制--Concurrency control--乐观、悲观及方法

    In information technology and computer science, especially in the fields of computer programming, op ...

  7. Git 遇到的坑

    ssh出错 gitlab服务器添加完公钥之后,ssh服务器然后报了这个错误 sign_and_send_pubkey: signing failed: agent refused operation ...

  8. flash设置里面:您的 Flash 设置会一直保留到您退出 Chrome 为止。

    疑问:flash设置里面:您的 Flash 设置会一直保留到您退出 Chrome 为止. 我记得以前的版本配置后就一直用啊,允许的就可以一直允许,现在这个sb版本退出后就恢复到默认,允许列表的网站就清 ...

  9. 【转】Windows中设置Fiddler抓HTTPS请求的解决办法 Unable to configure Windows to Trust the Fiddler Root certificate .

    官网设置 Click Tools > Fiddler Options > HTTPS. Click the Decrypt HTTPS Traffic box. 按照上述要求,我的设置是这 ...

  10. redis单例模式写法

    <?php /**只看红色重点 * =========================================================== * ZW_Memory_Cache * ...