日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PrincetonAlgorithm I - Assignment2 Deques and Randomized Queues

發布時間:2023/12/6 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

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

作業總結:

  • 對于文件的讀寫基本操作命令不夠熟悉
  • 對于問題的定義 出現了沒有搞清楚題目要求的現象,包括Deque的基本操作 以及Permutation 類當中,應當是讀取全部數據,輸出k個數據,而不是讀取k個數據,輸出全部數據的問題
  • 轉載于:https://www.cnblogs.com/kong-xy/p/9028179.html

    總結

    以上是生活随笔為你收集整理的PrincetonAlgorithm I - Assignment2 Deques and Randomized Queues的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。