Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build@Mercari Week 3 Data Structure

mercari
August 11, 2020
3.2k

Build@Mercari Week 3 Data Structure

mercari

August 11, 2020
Tweet

Transcript

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

    Memory • Linked Lists • Stacks • Queues
  2. 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
  3. Pointers • Pointers ◦ Locations of Data (Addresses) ◦ Value

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

    1 Pointer 3 0x00c 0x004 Value Pointer Address
  5. 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
  6. 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
  7. 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
  8. 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
  9. Stack Applications • Stack Frame ◦ Function Calls FuncA() {

    FuncB() { FuncC() { FuncD() } } } FuncA Runtime FuncB FuncC FuncD
  10. 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
  11. 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
  12. Queue Applications • CPU Scheduling ◦ 1 CPU Core =

    1 process at a time ◦ Use a Queue to schedule processes
  13. 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
  14. 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