Build@Mercari Week 3 Data Structure

92cdcff298e89e2fcd2fb705155c2d4b?s=47 mercari
August 11, 2020
910

Build@Mercari Week 3 Data Structure

92cdcff298e89e2fcd2fb705155c2d4b?s=128

mercari

August 11, 2020
Tweet

Transcript

  1. Data Structures 1 Hunter Chen

  2. Table of Contents • Introduction • Quick Review of Computer

    Memory • Linked Lists • Stacks • Queues
  3. How Computer Memory Works • Data is stored in locations

    ◦ Address ◦ Value Array: [3, 6, 4, 4, 7, 9, 2] 3 0x000 6 0x004 4 0x008 4 0x00c 7 0x010 9 0x014 2 0x018 Value Address Array calculation: Starting address + Index * Size
  4. Pointers • Pointers ◦ Locations of Data (Addresses) ◦ Value

    = Address of another piece of data 0x000 0x004 0x014 0x008 0x00c 0x010 2 0x014 0x018 Value Address
  5. Linked Lists • Nodes ◦ Nodes = 1 Value +

    1 Pointer 3 0x00c 0x004 Value Pointer Address
  6. Linked Lists • Linked List = 1 or more nodes

    ◦ First node = Head 3 0x008 0x004 LinkedList: (3, 6, 4, 4, 7, 9, 2) 6 0x014 0x008 4 0x020 0x014 4 0x02c 0x020 7 0x01c 0x02c 9 0x028 0x01c 2 null 0x028
  7. Linked Lists • Pros ◦ Does not have to be

    one block of memory ◦ Very easy to change the linked list • Cons ◦ Cannot immediately get a value ◦ Harder to implement than an Array
  8. Linked Lists • Applications: ◦ Stacks ◦ Queues ◦ Hash

    Maps ◦ Trees
  9. Linked Lists • Linked List Manipulation ◦ Reversing a Linked

    List 3 0x008 0x004 6 0x004 0x008 4 0x008 0x014 4 0x014 0x020 7 0x020 0x02c 9 0x02c 0x01c 2 0x028 0x01c LinkedList: (2, 9, 7, 4, 4, 6, 3) null 0x028 0x01c 0x02c 0x020 0x014 null
  10. Break Time!

  11. Stacks • LIFO (Last In First Out) ◦ Most Recent

    data will be accessed first Data 1 Stack Data 2 Data 3 Data 4 Data 5 • Push ◦ Add data to top of stack • Pop ◦ Get data from top of stack
  12. Stack Implementation • Using Arrays ◦ Array Implementation • Using

    Linked Lists ◦ Linked List Implementation
  13. Stack Applications • Stack Frame ◦ Function Calls FuncA() {

    FuncB() { FuncC() { FuncD() } } } FuncA Runtime FuncB FuncC FuncD
  14. Stack Applications • Stack Frames in Memory FuncA 0x000 0x010

    0x00c 0xFFF FuncB 0x010 0x020 0x01c 0x00c FuncC 0x020 1 0x024 0x01c FuncA() { FuncB() { FuncC() { return 1; } } } Pointers Frame
  15. Break Time!

  16. Queues • FIFO (First In First Out) ◦ Like waiting

    in a line! • Enqueue ◦ Add data to back of queue • Dequeue ◦ Get data from front of queue Data 1 Data 2 Data 3 Data 4 Data 5 Queue
  17. Queue Implementation • Using Arrays ◦ Array Implementation • Using

    Linked Lists ◦ Linked List Implementation
  18. Queue Applications • CPU Scheduling ◦ 1 CPU Core =

    1 process at a time ◦ Use a Queue to schedule processes
  19. Queue Applications • Problems with Queues in CPU Scheduling ◦

    Wait time Process 1 600 ms Process 2 20 ms Process 3 30 ms CPU Process 1 600 ms Process 2 20 ms Process 3 30 ms Wait Time: 0ms Wait Time: 600ms Wait Time: 620ms Wait Time: 0ms Wait Time: 20ms Wait Time: 50ms
  20. Queue Applications • Solution: Priority Queues ◦ Assign Priority to

    data and insert according to priority Process 1 600 ms Priority: 3 Process 2 20 ms Priority: 1 Process 3 30 ms Priority: 2 Process 1 600 ms Priority: 3 Process 2 20 ms Priority: 1 Process 3 30 ms Priority: 2 Sort By Priority Dequeue