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. hibernate-聚合函数分组统计数据查询

    聚合函数: 实例: package Test; import static org.junit.Assert.*; import java.util.List; import org.hibernat ...

  2. Beta Daily Scrum 第六天

    [目录] 1.任务进度 2.困难及解决 3.燃尽图 4.代码check-in 5.总结 1. 任务进度 学号 今日完成 明日完成 612 昨天完成任务,今天看有没有bug,找一找 好像没什么事 615 ...

  3. SQL Server的数据库连接的极限在哪儿?

    在软件设计中,关于多层的设计,有一部份是有关数据库的. 设计上分成这样三层 客户端UI -- 应用服务器 -- 数据库服务器 有个说法是,可以在应用服务器这一层共享使用数据库连接池,从而减轻数据库服务 ...

  4. Ubuntu学习总结-02 Ubuntu下的FTP服务的安装和设置

    一 安装vsftpd 在安装前vsftpd,先更新apt-get下载的数据源输入如下命令: sudo apt-get update 然后安装vsftpd sudo apt-get install vs ...

  5. JavaWeb学习总结-07 Filter 学习和使用

    一 Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...

  6. zabbix监控模式、分布式、自动化

    适用场景: 1.监控主机多,性能瓶颈 2.多机房,防火墙 zabbix监控模式 针对agent来说 - 被动模式 - 主动模式(主动汇报服务端) 1)当监控主机超过300台,建议使用主动模式 2)当队 ...

  7. BZOJ3685: 普通van Emde Boas树

    显然这题的所有操作都可以用set,但是直接用set肯定要T,考虑到读入量较大,使用fread读入优化,就可以卡过去了. #include<bits/stdc++.h> using name ...

  8. 模拟退火解决TSP问题

    // monituihuo.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdio.h> #includ ...

  9. 安装scikit-learn过程记录

    环境:Windows 10 X64企业版. 安装python2.7: 官网下载python-2.7.12.amd64.msi安装文件,过程不赘述.安装完成后在PATH环境变量中加入%PYTHON_HO ...

  10. LeetCode —— Merge k Sorted Lists

    /* ** 算法的思路: ** 1.将k个链表的首元素进行建堆 ** 2.从堆中取出最小的元素,放到链表中 ** 3.如果取出元素的有后续的元素,则放入堆中,若没有则转步骤2,直到堆为空 */ #in ...