Slide 1

Slide 1 text

Data Structures 1 Hunter Chen

Slide 2

Slide 2 text

Table of Contents ● Introduction ● Quick Review of Computer Memory ● Linked Lists ● Stacks ● Queues

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Pointers ● Pointers ○ Locations of Data (Addresses) ○ Value = Address of another piece of data 0x000 0x004 0x014 0x008 0x00c 0x010 2 0x014 0x018 Value Address

Slide 5

Slide 5 text

Linked Lists ● Nodes ○ Nodes = 1 Value + 1 Pointer 3 0x00c 0x004 Value Pointer Address

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Linked Lists ● Applications: ○ Stacks ○ Queues ○ Hash Maps ○ Trees

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Break Time!

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Stack Implementation ● Using Arrays ○ Array Implementation ● Using Linked Lists ○ Linked List Implementation

Slide 13

Slide 13 text

Stack Applications ● Stack Frame ○ Function Calls FuncA() { FuncB() { FuncC() { FuncD() } } } FuncA Runtime FuncB FuncC FuncD

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Break Time!

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Queue Implementation ● Using Arrays ○ Array Implementation ● Using Linked Lists ○ Linked List Implementation

Slide 18

Slide 18 text

Queue Applications ● CPU Scheduling ○ 1 CPU Core = 1 process at a time ○ Use a Queue to schedule processes

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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