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 -> 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
- Queue Programming API
- QueueOfStrings()
- void enqueue(String item)
- String dequeue()
- boolean isEmpty()
Same API with stack, only name changed
- Generic data types: autoboxing and unboxing
- Autoboxing: Automatic cast between a primitive type and its wrapper
在寫代碼的過程當中,心里需要有轉換角色的意識,當你在實現一個API的時候,需要考慮的是
* 實現某個方法所要使用的數據結構,
* 調用方法 or 自己寫方法,
* API的性能要求 -> 使用哪種算法可以滿足要求 查找,插入,刪除 時間 + 空間
- Iterators
- What is an Iterable?
- What is an Iterator?
- 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
作業總結:
轉載于:https://www.cnblogs.com/kong-xy/p/9028179.html
總結
以上是生活随笔為你收集整理的PrincetonAlgorithm I - Assignment2 Deques and Randomized Queues的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何管理跨部门的沟通与协作?
- 下一篇: JS基础入门篇( 一 )