Lecture 9: C to LC-3
@NCStateECE
Connect to slido.com
#ece209
ECE 209
Computer Systems Programming
Spring 2021
Lectures 19: Structs and Linked
Data Structures
Announcements
Z8: 4/13 – tomorrow!
No class on 4/14
Quiz 4: 4/19
TOPICS: strings, file I/O, struct
PS6: 4/21
Photo by Jason Rosewell, unsplash
Struct = multi-part value
Represent any multi-part “object” or concept
Playing card
• suit
• rank
Student
• name
• grade
• major
• creditHrs
Polygon
• points
• lineColor
• fillColor
Book
• title
• author
• isbn
• price
State
• name
• population
• area
• income
• capital
Let’s create struct definitions for these!
Struct = multi-part value
Can be used just like any other value type
Array element
Struct element
Argument to function
Return value from function
Only certain operators (can’t add, compare, etc.)
Linked Data Structures
Sometimes, we want a struct to point to
another struct. Avoids replication of data.
State
• name
• population
• area
• price
• capital
City
• name
• population
• state
Linked Data Structures
Many struct values might point to
the same struct value.
Raleigh
Charlotte
Asheville
North Carolina Wilmington
Clayton
Durham
Embedded vs. Linked
Raleigh North Carolina Wilmington
North Carolina
Raleigh
North Carolina
Wilmington
Advantages of linked?
ⓘ Start presenting to display the poll results on this slide.
Advantages of embedded?
ⓘ Start presenting to display the poll results on this slide.
Dynamic Allocation
Call malloc to allocate memory on the heap.
Discussion, examples…
Linked List
Sequential collection of same-typed values.
Built using structs — each points to the next
Advantage: Size is determined dynamically.
Disadvantage: Sequential access.
Lectures 19: Structs and Linked�Data Structures
Slide Number 2
Struct = multi-part value
Struct = multi-part value
Linked Data Structures
Linked Data Structures
Embedded vs. Linked
Slide Number 8
Slide Number 9
Dynamic Allocation
Linked List