Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

ECE 150, Section 001, Fall 2017

Data Structures

1 / 25
Motivating Problems

Linked Lists

Stack and Queue

Sorted Lists

2 / 25
Motivating Problems

Linked Lists

Stack and Queue

Sorted Lists

3 / 25
Look at Assignment 4, Q3

bool statistics(const float dataset[], const int size,


float& minimum, float& average, float& maximum,
float& popStdDev, float& smplStdDev, float& median);

I Six different values returned in six separate variables


I But they are all connected with each other ...
I And what if we had additional statistics:
I Range
I Average without outliers
I Histogram information
I ...
I Want to keep all of the statistics about a dataset in one spot
I ... and keep the raw data in that spot while we are at it ...

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;

powerData = statistics(dataset, size);


5 / 25
A quick note on syntax

Suppose we have:

Statistics powerData;
Statistics* pPowerData = &powerData;

I Then, the following are completely equivalent:


I powerData.minimum
I (*pPowerData).minimum
I (&powerData)->minimum
I pPowerData->minimum
I Use “.” to access a field if you have the structure
I Use “->” to access a field if you have a pointer to the
structure

6 / 25
Question: Where is powerData?

I Recall all local variables are on the stack


I As was the case with arguments to function calls
I The only difference is that there is more than one thing kept
in the variable
I What about float* dataSet ?

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

Stack and Queue

Sorted Lists

10 / 25
What If I Have Several Fractions?

Method One: Use an Array of struct

const int maxFractions = 23;


Fractions interestingFractions[maxFractions];
Fraction& veryInterestingFraction = [13];

interestingFractions[0].numerator = 22;
interestingFractions[0].denominator = 7;
...
veryInterestingFraction.numerator = 67;
veryInterestingFraction.denominator = 133

cout << interestingFractions[13].numerator << "/"


<< interestingFractions[13].denominator << endl;

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

I I want to store a database of electrical components.

enum TYPE {RESISTOR,CAPACITOR,INDUCTOR,TRANSITOR,DIODE};


struct Parts {
TYPE t; // Part type
float value; // Value or part number
};

I Operations I want to support, dynamically:


I Check whether a component is in my database.
I Add a new component to my database.
I Remove an entry from my database.
I I do not know with what frequency any of those may occur.

13 / 25
Linked List vs. Array — via Picture

vs.

14 / 25
Linked List Visualization

Some folks think of linked lists as the following:

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

Stack and Queue

Sorted Lists

21 / 25
Stack

Add/Remove to front of list

22 / 25
Queue

Add to one end of list, remove from other end of list

23 / 25
Motivating Problems

Linked Lists

Stack and Queue

Sorted Lists

24 / 25
Sorted Lists

Check the order when you insert

25 / 25

You might also like