第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 ...
随机推荐
- Dword、LPSTR、LPWSTR、LPCSTR、LPCWSTR、LPTSTR、LPCTSTR
DWORD:本来被定义为unsigned long,DWORD的含义就是双字,一个字是2字节,双字就是32字节. 但是在C/C++中,经常会用到把一个指针转换成数字储存,然后再将其传唤为指针调用.那么 ...
- 时间日期----java
Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Date ...
- select()
select(),确定一个或多个套接口的状态,本函数用于确定一个或多个套接口的状态,对每一个套接口,调用者可查询它的可读性.可写性及错误状态信息,用fd_set结构来表示一组等待检查的套接口,在调用返 ...
- JDK的安装和配置
JDK8 是JDK的最新版本,加入了很多新特性,如果我们要使用,需要下载安装: JDK8在windows xp下安装有点问题,所以在WIN7下安装 WIN7操作系统有32位和64位,分别要下载对应的J ...
- vs2013安装闪退及vs2010 vs2013打开时提示 未能完成的操作 及vs2013安装时出现图片后闪退
vs2013打开时提示如上图,vs2010只有 未能完成的操作 这样的提示. 这时.net 4.0开发的程序打开也毫无反应,应该是.net framework出了问题.查看控制面板-卸载程序,发现 ...
- Codeforces Round #371 (Div. 2)B. Filya and Homework
题目链接:http://codeforces.com/problemset/problem/714/B 题目大意: 第一行输入一个n,第二行输入n个数,求是否能找出一个数x,使得n个数中的部分数加上x ...
- yum安装指定(特定)版本(旧版本)软件包的方法
在命令行里输入: yum list SDL 注意这里类库的名字是区别大小写的. 参考 http://www.dabu.info/yum-install-specific-version-old-pac ...
- Http常用状态码
HTTP状态码(HTTP Status Code) 一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 所有状态解释:点击查看 1xx(临时响应 ...
- Druid使用起步—在javaWeb项目中配置监控 连接池
当我们在javaWEB项目中使用到druid来作为我们的连接池的时候,一定不会忘了添加监控功能.下面我们就来看一下,在一个简单的web项目中(尚未使用任何框架)我们是如果来配置我们的web.xml来完 ...
- C# 拷贝目录
public class DirectoryExtends { /// <summary> /// 拷贝目录 /// </summary> /// <param name ...