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的更多相关文章

  1. Stack&&Queue

    特殊的容器:容器适配器 stack     queue     priority_queue:vector+堆算法---->优先级队列 stack:     1.栈的概念:特殊的线性结构,只允许 ...

  2. STL容器适配器 stack, queue

    stack是一种后进先出(last in first out)的数据结构.它只有一个出口,如图所示.stack允许新增元素,删除元素,取得最顶端元素.但除了最顶端外,没有其他任何地方可以存储stack ...

  3. 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 ...

  4. D3_book 11.2 stack

    <!-- book :interactive data visualization for the web 11.2 stack 一个堆叠图的例子 --> <!DOCTYPE htm ...

  5. 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 ...

  6. 数据结构设计 Stack Queue

    之前在简书上初步总结过几个有关栈和队列的数据结构设计的题目.http://www.jianshu.com/p/d43f93661631 1.线性数据结构 Array Stack Queue Hash ...

  7. programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation

    编程题常用知识点的review. most important: 想好(1)详尽步骤(2)边界特例,再开始写代码. I.vector #include <iostream> //0.头文件 ...

  8. C++11:基于std::queue和std::mutex构建一个线程安全的队列

    C++11:基于std::queue和std::mutex构建一个线程安全的队列 C++中的模板std::queue提供了一个队列容器,但这个容器并不是线程安全的,如果在多线程环境下使用队列,它是不能 ...

  9. 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 ...

随机推荐

  1. juqery 实现商城循环倒计时

    <html> <hand> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jqu ...

  2. 自定义JSP标签入门

    1.编写一个实现Tag接口的java类 package TagDemo; import javax.servlet.http.HttpServletRequest; import javax.serv ...

  3. C#--网络流Stream、字节数组保存到字符串中

    第一种方法: HttpWebRequest httpwebr = (HttpWebRequest)HttpWebRequest.Create(rstr); httpwebr.Method = &quo ...

  4. Red Black Tree in C

    http://web.mit.edu/~emin/www.old/source_code/red_black_tree/index.html

  5. 轻量级Image Library

    dlib http://sourceforge.net/projects/dclib/ stb https://github.com/nothings/stb

  6. input的onkeyup效果 超级简短代码

    效果代码 title="请输入正确的十六位数字" onkeyup="javascript: return this.value = this.value.toUpperC ...

  7. SPL--spl_autoload_register

    spl_autoload_register() : 调用未定义类时,系统会按顺序调用注册到spl_autoload_register()函数的所有函数,而不是调用__autoload函数. 解决问题: ...

  8. 为你的Visual Studio单独设置代理服务器

    http://blog.sina.com.cn/s/blog_58c506600101tycn.html 最近,因为国内访问Visual Studio Online(微软的免费代码托管服务,以前叫Te ...

  9. C-基本语法与运算

    编译: Technorati 标记: C 1, 编译compilers 命令make 将高级语言转换为低级语言. clang: 1,预处理(preprocessing) 2,编译(complition ...

  10. Orchard源码分析(2):Orchard.Web.MvcApplication类(Global)

    概述 分析一个的ASP.NET项目源码,首先可以浏览其项目结构,大致一窥项目其全貌,了解项目之间的依赖关系.其次可以浏览Web.config和Global.asax文件,找到应用程序的入口点. 本 文 ...