环形对列(circle queue 1329 P103)
来源:互联网 发布:网络监控光纤收发器 编辑:程序博客网 时间:2024/05/01 23:06
描述:使用链表(LinkedList) 实现一个对列。给出最后一个Node ;
思路:
1.环形链表的实现方式:最后一个节点的next指向第一个节点。
2.入列方式:将最后一个节点last指向新的节点,
并将新的节点node的next指向last.next(原节点last的next, 这里为首节点).
最后把新节点node定义为最后一个节点last.
3.出列方式:找到最后一个节点的next,也就是首节点,first = last.next
然后将last.next 指向 first.next;跳过首节点first,定义首节点first.next为新的首节点。
输出 first.item
注意:不能直接输出 last.next.item;因为只有一个节点的这种情况会导致last 为空,输出的item也必然为空的情况!
思路草图:
代码如下:
package chapter1.a3;
/**
* Created by fengjw on 2017/8/13
* Code Change The World!
*/
public class Example1329<Item> {//circle queue
private Node last;
private class Node{
Item item;
Node next;
public Node(Item item){
this.item = item;
}
}
/**
* enqueue operation
*/
public void enqueue(Item item){
Node node = new Node(item);
if (last ==null){
last = node;
last.next =last;
}else {//cycle
Node origin = last.next;
last.next = node;
node.next = origin;// origin is the first Node
last = node;// by the 'last' point to the last Node
}
}
/**
* dequeue operation
*/
public Itemdequeue(){
if (last ==null){
throw new NullPointerException("queue is empty!");
}
Node first = last.next;
if (first == last){
last = null;// '@Important'
}else {
last.next = first.next;
}
return first.item;// or last.next.item? Error!
// Because : if LinkedList only have one Node,
//'last = null'; return last.next is mean return null; not get the item.
}
}
阅读全文
0 0
- 环形对列(circle queue 1329 P103)
- 环形内存,circle memory
- Team Queue (优先对列的嵌套)
- GregoranCalandar类(java核心P103)
- HDU 4669 Mutiples on a circle (环形数列DP)byPlato
- Codeforce Circle Line 环形数据操作
- 浅析并实现栈(stack)和对列(queue)
- STL中队列(queue)的使用方法
- 第七周 项目一建立顺序环形对列算法库
- 递归:解决汉诺塔问题(数据结构3.2 P103)
- 删除链表最后尾节点(1319 P103)
- 删除某一个节点k的方法(1320 P103)
- 环形缓冲区(ring buffer),环形队列(ring queue) 原理
- 环形缓冲区(ring buffer),环形队列(ring queue) 原理
- 无锁队列的环形数组实现(Lock Free Queue Implementation in Ring Array)
- C#中队列Queue
- java中队列Queue
- 有关优先对列的题——UVA12100 - Printer Queue
- 《java 多线程编程核心技术》读书笔记三(补充内容)
- CodeForces round 428 div2 only
- C++多线程学习(一)
- Mac VMware 序列号
- Linux的内存理解
- 环形对列(circle queue 1329 P103)
- maven查询网站
- poj2349 Kruskal题解
- 网易2017内推 [编程题] 交错01串@Java
- lintcode --寻找旋转排序数组中的最小值 II
- Superset搭建
- Java虚拟机对象创建管理与垃圾回收知识整理与分析
- Leetcode算法学习日志-121 Best Time to Buy and Sell Stock
- CVE-2015-2545浅析-word释放重引用漏洞