PrincetonAlgorithm I - Assignment2 Deques and Randomized Queues

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 
        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 API

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

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

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

sketch of the Deque

作业总结:
1.对于文件的读写基本操作命令不够熟悉

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

猜你喜欢

转载自www.cnblogs.com/kong-xy/p/9028179.html