【JAVA数据结构】带dummy节点的双向队列
来源:互联网 发布:c语言学习班 编辑:程序博客网 时间:2024/06/06 06:48
package dequeue;import java.util.Iterator;public class Deque<Item> implements Iterable<Item> {public class Node{Item value = null;Node next = null;Node prev = null;}publicclass terminal extends Node{private int cp;terminal(int cp){this.cp = cp;}}public terminal head = new terminal(-1);public terminal tail = new terminal(-1);private int size;public Deque(){head.next = tail;tail.prev = head;size = 0;}public boolean isEmpty() {return size == 0 ;}public int size() {return size;}public void addFirst(Item item) {Node tmp = new Node();tmp.value = item;tmp.prev = head;tmp.next = head.next;head.next = tmp;size++;}public void addLast(Item item){// add the item to the endNode tmp = new Node();tmp.value = item;tmp.prev = tail.prev;tail.prev.next = tmp;tmp.next = tail;tail.prev = tmp;size++;}public Item removeFirst(){// remove and return the item from the frontif(size==0){throw new java.util.NoSuchElementException();}Item result = head.next.value;head.next = head.next.next;size--;return result;}public Item removeLast(){// remove and return the item from the endif(size==0){throw new java.util.NoSuchElementException();}Item result = tail.prev.value;Node tmp = tail.prev;tail.prev = tmp.prev;tmp.prev.next = tail;tmp = null;size--;return result;}public Iterator<Item> iterator(){return new DequeIterator();}private class DequeIterator implements Iterator<Item> {private Node currentNode = head.next;@Overridepublic boolean hasNext() {// TODO Auto-generated method stubreturn currentNode != tail;}@Overridepublic Item next() {// TODO Auto-generated method stubif (!hasNext()) throw new java.util.NoSuchElementException();Item item = currentNode.value;currentNode = currentNode.next;return item;}}}
0 0
- 【JAVA数据结构】带dummy节点的双向队列
- java数据结构之LinkedDeque2(用链表实现的双端双向队列,addBack时调用节点的构造函数稍有不同)
- java数据结构之LinkedDeque(用链表实现的双端(即头尾两个哨兵节点)双向(node里两个指向)队列)
- Java 数据结构之Deque(双向队列)
- Java 数据结构之Deque(双向队列)
- 一个用Java实现的双向队列,可以分别在头尾插入和删除节点
- 数据结构----双向队列
- 带表头节点的双向循环链表编程
- 【算法导论】10.2不带哨兵节点和带哨兵节点的双向链表
- 数据结构示例之带头节点的双向循环链表
- 数据结构示例之带头节点的双向循环链表
- Java数据结构之队列_动力节点Java学院整理
- Java双向队列,用双向链表实现的
- 数据结构--双向带哨兵的循环链表
- 《Java数据结构与算法》笔记-CH4-5不带计数字段的循环队列
- 【数据结构】求节点的哈夫曼的带权路径长度
- 数据结构--二叉树--节点的修改(链式结构--队列)
- 双向队列的应用
- 阿里云Ubuntu主机搭建Lamp环境
- CQL(Cassandra Query Language)数据操作
- GCC编译命令
- PHP 连接数据库
- 客户端储存的三种方式localStorage 、sessionStorage、Cookies
- 【JAVA数据结构】带dummy节点的双向队列
- 微信小程序css篇----字体(Font)
- Jar命令构建jar包
- 阿里云主机搭建lamp环境
- spark core 2.0 LiveListenerBus
- android7.0-PopupWindow-showAsDropDown
- JSP简介
- org.apache.ibatis.binding.BindingException: Parameter 'receptionList' not found. Available parameter
- 常用的几种文件封装格式