Programming Assignment2 - Deque and Randomized Queues Review

Assignment Specification

课程笔记

Subtext: Modular Programming

  • Stacks and Queues are fundamental data types

    • Value: collection of objects
    • Basic Operation: insert, remove, iterate.
    • Difference: which item do we move? -> Stack: LIFO(last in first out) Queue: FIFO(first in first out)
  • Client, implementation, interface
    • Client: program using operations defined in interface
    • Implementation: actual code implementing operations
    • Interface: description of data type, basic operations

Stack Programming API:

public class StackOfStrings
StackOfStrings() //create an empty stack
void push(String item)  //insert a new string onto stack
String pop() //remove and return the string most recently added
boolean isEmpty()  //is the stack empty?

linked-list implementation

//Attention: Stack have only one exit -> only one pointer is enough
/*Corner Cases:client add a null item -> IllegalArgumentExceptionremove() from empty stack -> NoSuchElementException
*/
public class StackOfStrings {private Node first;private class Node {String item;Node next;}public boolean isEmpty() {return first == null;}public StackOfStrings {Node first = null;}public void push(String item) {//Improve: add exception to deal with invalid operationNode oldfirst = first;first = new Node(); //Attention: must craete a new instance herefirst.item = item;first.next = oldfirst;}public String pop() {String item = first.item;first = first.next;return item;}

Proposition: Every operation takes constant time in the worst case. A stack with N items uses 40N bytes
Object overhead (16) + inner class extra overhead(8) + item reference (8) + next reference(8) = 40

array implementation

/*
Underflow: throw exception if pop from an empty stack
Overflow: use resizing array for array implementation
*/
public class FixedCapacityStackOfStrings {private String[] s;private int N = 0;public FixedCapacityStackOfStrings (int capacity) {s = new String[capacity];}public String pop() {//Attention: declare null pointer to avoid loitering so garbage collector can reclaim memoryString item = s[--N];s[N] = null;return item;}public void push(String item) {s[N++] = item;}public boolean isEmpty() {return n == 0;}
}
  • Resizing array

    • Problem: Require client to provide capacity does not implement API. Constructor should not have int input
    • Question: How to grow and shrink array?
    • Answer: grow: double shrink: quarter - > Why? ->
      • double array for grow-> cost of is Linear N + (2 + 4 + 8 + .... + N) ~ 3N Geometric sequence: Sn = (a1 - an * q) / 1 - q
      • quarter for shrink -> avoid thrashing push - pop - push - pop when sequence is full -> each operation takes time propotional to N

//Note: array is between 25% and 100% full
public class ResizingArrayStackOfStrings {private String[] s;public ResizaingArrayStackOfStrings() {s = new String[1];}public void push(String item) {if (N == s.length) resize (2 * s.length);s[N++] = item;}private void resize(int capacity) {//create a double size array, copy the element from the old String array, update the pointerString[] copy = new String[capacity];for (int i = 0; i < capacity; i++) {copy[i] = s[i];s = copy;}public String pop() {String item = s[--N];S[N] = null;if (N > 0 && N = s.length/4) resize(s.length / 2);return item;}
}
  • Queue Programming API

    • QueueOfStrings()
    • void enqueue(String item)
    • String dequeue()
    • boolean isEmpty()
      Same API with stack, only name changed
*linked list implementation
/*Attention:
Queue has two exit, so it needs two pointers
*/
public class LinkedQueueOfStrings {public LinkedQueueOfStrings() {Node first, last;int N = 0;}private class Node {String item;Node next;}public boolean isEmpty() {return first == null;}//enqueue elements added to the last of the queuepublic void enqueue(String item) {Node oldlast = last; // here last already points to an exist instance//Create a totally new Nodelast = new Node();last.item = item;last.next = null;//linked back with the queueif (isEmpty()) {//there is only one element exist ->first = last;}else {oldlast.next = last;}}public String dequeue() {String item = first.item;first = first.next;if (isEmpty()) {last = null;}return item;}
}
  • Generic data types: autoboxing and unboxing

    • Autoboxing: Automatic cast between a primitive type and its wrapper
    Stack<Integer> s = new Stack<Integer>();
    s.push(17);  //s.push(Integer.valueOf(17));
    int a = s.pop();  //int a = s.pop().intValue();

在写代码的过程当中,心里需要有转换角色的意识,当你在实现一个API的时候,需要考虑的是
* 实现某个方法所要使用的数据结构,
* 调用方法 or 自己写方法,
* API的性能要求 -> 使用哪种算法可以满足要求 查找,插入,删除 时间 + 空间

  • Iterators

    • What is an Iterable?
    • What is an Iterator?
    public interface Iterator<Item> {boolean hasNext();Item next();
    }
    • Why make data structures Iterable ?
  • Java collections library
    List Interface. java.util.List is API for an sequence of items

    • java.util.ArrayList uses resizing array -> only some operations are effieient
    • java.util.LinkedList uses linked list -> only some operations are effieient
      tip: 不清楚library的具体实现的时候,尽量避免调用相关的方法。可能效率会很低。
  • Arithmetic expression evaluation
    ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
    Two-stack algorithm. 【E. W. Dijkstra】

    • value: push onto the value stack
    • Operator: push onto the operator stack
    • Left parenthesis: ignore
    • Right parenthesis: pop operator and two values; push the result of applying that operator to those values onto the operand stack

总结:
Stack的链表实现
Stack的数组实现(resize array)
Queue的链表实现
Queue的数组实现(resize array)

对于双向队列的理解有误,导致错误实现。
双向对别不应当是两个队列的水平叠加,见figure 1

作业总结:

  1. 对于文件的读写基本操作命令不够熟悉
  2. 对于问题的定义 出现了没有搞清楚题目要求的现象,包括Deque的基本操作 以及Permutation 类当中,应当是读取全部数据,输出k个数据,而不是读取k个数据,输出全部数据的问题

转载于:https://www.cnblogs.com/kong-xy/p/9028179.html

PrincetonAlgorithm I - Assignment2 Deques and Randomized Queues相关推荐

  1. 「数据结构」普林斯顿算法课第二周作业

    「数据结构」普林斯顿算法课第二周作业 Algorithm I, Princeton 编程作业: Deques and Randomized Queues 思路 Deque.java Randomize ...

  2. Week2 Assignment - Princeton-Algorithms-PartI

    题注 同样是一片我新浪博客的文章,搬到CSDN中帮助更多的人吧!   虽然是春节,但Coursera竟然没有任何让人休息的意思,课程还是接着上-这周的题目看起来比较简单,但是实际上问题一堆,我从上午2 ...

  3. java println()中_java中system.out.println()是什么意思

    在Java编程中,我们常常用System.out.println()方法来输出字符串,也许我们都已经猜到println()是方法名,但System是什么,out又是什么呢? 这里就涉及用到一个stat ...

  4. java队列 双队列_Java队列– Java队列

    java队列 双队列 Java Queue is an interface available in java.util package and extends java.util.Collectio ...

  5. Operation Queues并发编程

    并发.异步在我们的编程中,见到的太多了.在iOS中,实现并发的主要三个途径Operation Queues.Dispatch Queues.Dispatch Sources,今天我们就来详细介绍Ope ...

  6. 柯南君:看大数据时代下的IT架构(5)消息队列之RabbitMQ--案例(Work Queues起航)...

    二.Work Queues(using the Java Client) 走起 在第上一个教程中我们写程序从一个命名队列发送和接收消息.在这一次我们将创建一个工作队列,将用于分发耗时的任务在多个工作者 ...

  7. PCA、碎石图、PCA+正确的维度个数、增量PCA(IncrementalPCA)、随机PCA(Randomized PCA)、KernelPCA

    PCA.碎石图.PCA+正确的维度个数.增量PCA(IncrementalPCA).随机PCA(Randomized PCA).KernelPCA 目录 PCA

  8. RabbitMQ(二):Work Queues、循环分发、消息确认、持久化、公平分发

    内容翻译自:RabbitMQ Tutorials Java版 RabbitMQ(一):Hello World程序 RabbitMQ(二):Work Queues.循环分发.消息确认.持久化.公平分发 ...

  9. C++ Queues(队列)

    Queues队列 back empty front pop push size back 语法: TYPE &back(); back()返回一个引用,指向队列的最后一个元素. empty 语 ...

最新文章

  1. 如何使得按确定和取消按纽转到两个不同的页面!
  2. oracle 相同的sql执行两次 执行计划会不一样吗,一条SQL语句,两次执行计划的差距...
  3. 准备:新V8即将到来,Node.js的性能正在改变
  4. 利用python对微信云数据库_如何用python看看女神的微信百度云里面有啥?
  5. Git 学习看这篇就够了!
  6. matlab设计译码器,基于MATLAB的循环码编译码器设计与仿真.doc
  7. 求1到N的全排列 (转载)
  8. 【12-05】面试题
  9. 2018辛苦一年了,程序员这样跟大boss谈2019加薪,谈薪杯具变喜剧
  10. 完全免费的在线遥感影像下载器-转载
  11. 用线段树写Dijkstar
  12. Win8系统108个运行命令
  13. 如何同时登陆2个微信
  14. Vue + element-ui合并单元格后,checkbox多选单选取值问题
  15. 用AI生成假员工,8天众筹3万美元,这家创业公司2/3的成员都是假的!
  16. (实战)[re:Invent 2018]-001:赛道分析-(致敬1024)
  17. 服务器能当电脑用吗?与普通电脑有何区别?
  18. APM应用性能管理工具Pinpoint测评
  19. Android开发之仿QQ表情实现(上)
  20. 湖北黄冈计算机考试,湖北黄冈2020年注会什么时候考试?是计算机考试吗?

热门文章

  1. git 几种还原版本_Git恢复之前版本的两种方法reset、revert(图文详解)
  2. 读书笔记(06) - 语法基础 - JavaScript高级程序设计
  3. 制造领域的人工智能技术
  4. c++读取和写入TXT文件的整理
  5. android 4.4以上能够实现的沉浸式状态栏效果
  6. 线程管理(九)使用本地线程变量
  7. 关于捕获键盘信息的processDialogkey方法2--具体应用
  8. struts2中jsp页面上验证码的生成
  9. sql2005数据导入出错问题
  10. 环境在c盘_如何给女朋友解释为什么 Windows 上面的软件都把自己安装在 C 盘