Lecture06

  • Array 和Linked List 相比的优缺点

  • 双向链表的常见操作(addToFront()、addToLast()、removeAtIdx())

  • Stack: Last in, First on的特性及常见操作(push(), pop(),peak(), size(), isEmpty())

  • Recursion

  • Size: Since data can only be stored in contiguous blocks of memory in an array, its size cannot be altered at runtime due to risk of overwriting over other data. However in a linked list, each node points to the next one such that data can exist at scattered address; this allows for a dynamic size which can change at runtime.

  • Memory allocation: For arrays at compile time and at runtime for linked lists.

  • Memory efficiency: For the same number of elements, linked lists use more memory as a reference to the next node is also stored along with the data. However, size flexibility in linked lists may make them use less memory overall; this is useful when there is uncertainty about size or there are large variations in the size of data elements; memory equivalent to the upper limit on the size has to be allocated (even if not all of it is being used) while using arrays, whereas linked lists can increase their sizes step-by-step proportionately to the amount of data.

  • Execution time: Any element in an array can be directly accessed with its index; however in case of a linked list, all the previous elements must be traversed to reach any element. Also, better cache locality in arrays (due to contiguous memory allocation) can significantly improve performance. As a result, some operations (such as modifying a certain element) are faster in arrays, while some other (such as inserting/deleting an element in the data) are faster in linked lists.

Abstract Data Type: Stack
A stack(or sequence) is an Abstract Data Type that represents a finite sequence of elements in which the only element that can be removed is the element that was most recently inserted(referred to as the top element of the stack).

Stacks:

  • are a Last-In /First-out(LIFO) structures / collections,
  • restricted access structures : only the top element can be accessed,
  • can be :
    • static with bounded capacity
    • dynamic with logically unlimited capacity

Recursion in Computer Science

  • The power of recursion evidently lies in the possibility of defining an infinite set of objects by a finite statement. In the same manner, an infinite number of computations can be described by a finite recursive program, even if this program contains no explicit repetitons.

Recursive vs. Iterative Algorithms

  • Key difference:

    • A recursive method is implemented using decision constructs(if / else statements) and calls itself.
    • An iterative method is implemented with looping constructs(while or for statements) and repeatedly executes the loop.
  • Any recursive algorithms can be solved using iterations

  • Some problems are easier(more natural to express) to implement using recursion

  • Considerations when deciding to use recursion or iteration include:
    --Efficiency of the method at execution time : often, recursion is slower because of overhead associated with method calls.
    --Readability and maintenance: often, a recursive formulation is easier to read and understand that its iterative equivalent.

你可能感兴趣的:(Lecture06)