码迷,mamicode.com
首页 > 其他好文 > 详细

PrincetonAlgorithm I - Assignment2 Deques and Randomized Queues

时间:2018-05-12 12:47:09      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:课程笔记   client   string   recently   exception   create   link   调用   空间   

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 -> IllegalArgumentException
    remove() 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 operation
        Node oldfirst = first;
        first = new Node(); //Attention: must craete a new instance here
        first.item = item;
        first.next = oldfirst;
    }

    public String pop() {
        String item = first.item;
        first = first.next;
        return item;
    }

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

Stack的链表实现
Stack的数组实现(resize array)

Queue的链表实现
Queue的数组实现(resize array)

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

技术分享图片

作业总结:

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

PrincetonAlgorithm I - Assignment2 Deques and Randomized Queues

标签:课程笔记   client   string   recently   exception   create   link   调用   空间   

原文地址:https://www.cnblogs.com/kong-xy/p/9028179.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!