Professional Documents
Culture Documents
ECE 150, Section 001, Fall 2017 Data Structures
ECE 150, Section 001, Fall 2017 Data Structures
Data Structures
1 / 25
Motivating Problems
Linked Lists
Sorted Lists
2 / 25
Motivating Problems
Linked Lists
Sorted Lists
3 / 25
Look at Assignment 4, Q3
4 / 25
Custom Data Structures in C/C++
struct Statistics {
float minimum;
float average;
float maximum;
float popStdDev;
float smplStdDev;
float median;
float mode;
float range;
int dataSetSize;
float* dataSet;
};
Statistics powerData;
Suppose we have:
Statistics powerData;
Statistics* pPowerData = &powerData;
6 / 25
Question: Where is powerData?
7 / 25
Another Example: Want to Maintain Fractions
I Why?
I float can cause loss of precision.
I E.g., 1/10 in binary is non-terminating
I Want to keep numerator and denominator together.
8 / 25
Custom Data Structures in C++
struct Fraction {
int numerator, denominator;
};
Fraction f;
f.numerator = 1;
f.denominator = 2;
See 5-fraction.cpp
9 / 25
Motivating Problems
Linked Lists
Sorted Lists
10 / 25
What If I Have Several Fractions?
interestingFractions[0].numerator = 22;
interestingFractions[0].denominator = 7;
...
veryInterestingFraction.numerator = 67;
veryInterestingFraction.denominator = 133
11 / 25
What happens when I decide some fraction isn’t
interesting?
Or I have more than 23 interesting fractions?
I How do I know how many fractions I have in the array?
I I could keep a fractionCount variable
I What if I have more than 23 fractions?
I I could create an array of double the size and copy the data
over
I Fraction intFracs2[] = new Fraction
[maxFractions*2];
I Is that a bad idea? Why? Why not?
I What if I want to remove a fraction?
I Add a marker in the fraction struct for deleted fractions?
I What if I only care about the last fraction added (stack style)?
I What if I want it sorted while adding items?
12 / 25
Requirements
13 / 25
Linked List vs. Array — via Picture
vs.
14 / 25
Linked List Visualization
15 / 25
A Simple Linked List, in Code
See 5-linkedlist-simple.cpp
16 / 25
Traversing a linked list
17 / 25
Insertion into a linked list
18 / 25
Deletion from a linked list
19 / 25
Code example
See 5-linkedlist-insertdelete.cpp
20 / 25
Motivating Problems
Linked Lists
Sorted Lists
21 / 25
Stack
22 / 25
Queue
23 / 25
Motivating Problems
Linked Lists
Sorted Lists
24 / 25
Sorted Lists
25 / 25