第11天 Stack Queue
1.Stack
package algs4; import java.util.Iterator;
import java.util.NoSuchElementException; public class Stack<Item> implements Iterable<Item> { private Node<Item> first;
private int n; private static class Node<Item> {
private Item item;
private Node<Item> next;
} public Stack() {
first = null;
n = 0;
} public boolean isEmpty() {
return first == null;
} public int size() {
return n;
} public void push(Item item) {
Node<Item> oldfirst = first;
first = new Node<Item>();
first.item = item;
first.next = oldfirst;
n++;
} public Item pop() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
Item item = first.item;
first = first.next;
n--;
return item; } public Item peek() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
return first.item;
} public String toString() {
StringBuilder s = new StringBuilder();
for (Item item : this)
s.append(item + " ");
return s.toString();
} @Override
public Iterator<Item> iterator() {
// TODO Auto-generated method stub
return new ListIterator<Item>(first);
} private class ListIterator<Item> implements Iterator<Item> {
private Node<Item> current; public ListIterator(Node<Item> first) {
// TODO Auto-generated constructor stub
current = first;
} @Override
public boolean hasNext() {
// TODO Auto-generated method stub
return current != null;
} @Override
public Item next() {
// TODO Auto-generated method stub
if (!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
} @Override
public void remove() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException();
} } public static void main(String[] args) {
Stack<String> stack = new Stack<String>();
// while (!StdIn.isEmpty()) {
//
// }
} }
2.Queue
package algs4; import java.util.Iterator;
import java.util.NoSuchElementException; public class Queue<Item> implements Iterable<Item> {
private Node<Item> first;
private Node<Item> last;
private int n; private static class Node<Item> {
private Item item;
private Node<Item> next;
} public Queue() {
first = null;
last = null;
n = 0;
} public boolean isEmpty() {
return first == null;
} public int size() {
return n;
} public Item peek() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
return first.item;
} public void enqueue(Item item) {
Node<Item> oldlast = last;
last = new Node<Item>();
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else oldlast.next = last;
n++;
} public Item dequeue() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
Item item = first.item;
first = first.next;
n--;
if (isEmpty()) last = null;
return item;
} public String toString() {
StringBuilder s = new StringBuilder();
for (Item item : this)
s.append(item + " ");
return s.toString();
} @Override
public Iterator<Item> iterator() {
// TODO Auto-generated method stub
return new ListIterator<Item>(first);
} private class ListIterator<Item> implements Iterator<Item> {
private Node<Item> current; public ListIterator(Node<Item> first) {
// TODO Auto-generated constructor stub
current = first;
} @Override
public boolean hasNext() {
// TODO Auto-generated method stub
return current != null;
} @Override
public Item next() {
// TODO Auto-generated method stub
if (!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
} @Override
public void remove() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException();
} }
}
第11天 Stack Queue的更多相关文章
- Stack&&Queue
特殊的容器:容器适配器 stack queue priority_queue:vector+堆算法---->优先级队列 stack: 1.栈的概念:特殊的线性结构,只允许 ...
- STL容器适配器 stack, queue
stack是一种后进先出(last in first out)的数据结构.它只有一个出口,如图所示.stack允许新增元素,删除元素,取得最顶端元素.但除了最顶端外,没有其他任何地方可以存储stack ...
- STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map
list vector deque stack queue priority_queue set [unordered_set] map [unordered_map] multimap [uno ...
- D3_book 11.2 stack
<!-- book :interactive data visualization for the web 11.2 stack 一个堆叠图的例子 --> <!DOCTYPE htm ...
- tensorflow 基本函数(1.tf.split, 2.tf.concat,3.tf.squeeze, 4.tf.less_equal, 5.tf.where, 6.tf.gather, 7.tf.cast, 8.tf.expand_dims, 9.tf.argmax, 10.tf.reshape, 11.tf.stack, 12tf.less, 13.tf.boolean_mask
1. tf.split(3, group, input) # 拆分函数 3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值 import tensorflow ...
- 数据结构设计 Stack Queue
之前在简书上初步总结过几个有关栈和队列的数据结构设计的题目.http://www.jianshu.com/p/d43f93661631 1.线性数据结构 Array Stack Queue Hash ...
- programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation
编程题常用知识点的review. most important: 想好(1)详尽步骤(2)边界特例,再开始写代码. I.vector #include <iostream> //0.头文件 ...
- C++11:基于std::queue和std::mutex构建一个线程安全的队列
C++11:基于std::queue和std::mutex构建一个线程安全的队列 C++中的模板std::queue提供了一个队列容器,但这个容器并不是线程安全的,如果在多线程环境下使用队列,它是不能 ...
- js in depth: event loop & micro-task, macro-task & stack, queue, heap & thread, process
js in depth: event loop & micro-task, macro-task & stack, queue, heap & thread, process ...
随机推荐
- java-HashMap方法讲解
前言:Java8之后新增挺多新东西,在网上找了些相关资料,关于HashMap在自己被血虐之后痛定思痛决定整理一下相关知识方便自己看.图和有些内容参考的这个文章:http://www.importnew ...
- Beta版本冲刺第二天 12.6
一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 昨天完成任务 今天完成任务 第三天冲刺要做任务 问题困难 心得体会 胡泽善 完成了"记住密码"的的逻辑以及BUG修改 ...
- 对JavaScript优化及规范的一些感想
变量...... 1.一个变量只存一种类型的数据,2.尽量减少对隐式转换的依赖,这样可增强程序的可读性,日后修改程序时不至于混乱,3.使用匈牙利命名法,4.使用局部变量时记得加 var 进行声明,不然 ...
- HDU5670Machine(抽象进制)
有一个机器,它有 m (2\leq m\leq 30)m(2≤m≤30) 个彩灯和一个按钮.每按下按钮时,最右边的彩灯会发生一次变换.变换为: 1. 如果当前状态为红色,它将变成绿色: 2.如果当前状 ...
- Glusterfs分布式存储介绍(一)
环境准备 1.centos6.8 系统的虚拟机(四台) 2.关闭iptables和SELinux 3.预装glusterfs软件包 yum install -y centos-release-glus ...
- WebDataGrid设置某行某列的值
<ig:WebDataGrid ID="grid" OnRowSelectionChanged="grid_RowSelectionChanged" O ...
- LyX-220-Installer-3
所见即所得 单独安装这个写作业可以了,要发论文用CTeX Ctrl + M 打开数学输入,里面可以输入 TeX 代码
- Game Programming Pattern
http://gameprogrammingpatterns.com/contents.html
- Quartz.NET总结(一)入门
前段时间,花了大量的时间,将原先的计划任务,切换到Quartz.NET来进行管理.原先的后台定时服务都是通过计划任务来实现的,但是随着业务增长,计划任务也越来越多,每个后台服务,都得创建一个计划任务. ...
- Unity 下载
Unity历史版本 http://wiki.ceeger.com/unity:history#unity_522f1 UNITY 下载存档 http://unity3d.com/cn/get-unit ...