Data structures

On storing data with care

Quick review

  1. When to use a loop?
     
  2. How to look into each array element?
     
  3. How to access array element?
     
  4. How to get the array size?

Weekly quiz ( 10 min )

Homework

ToDo list

Contents

  1. Why data structures

  2. Queues

  3. Stacks

  4. Lists

  5. Maps

  6. Summary

Why data structures?

Computer programs need
to store data

e.g. keep array of user names

The right data structure might make a program run 10x or more faster
e.g. search for contact in your phone, get or add new item in your shopping list

Queues

Some examples

  1. When you order in line to pay your electricity bill
  2. When a police officer stops multiple cars they are processed as a queue
  3. When you go see your GP and other patients are waiting too

Do you have more?

Queue

  • Add new elements at the back (tail)
  • Get existing elements from the front (head)

What queues do? (2)

Front

Back

Add

Poll

First In First Out (FIFO)

Example: Waiting in line

Ivancho is in EVN office to pay his bill

and... disaster strikes!

The line is not moving!!!

They need a hero (or a good programmer) to fix that!

In a galaxy far far away....

Operations on queues

ArrayDeque<Integer> queue = new ArrayDeque<>();
queue.add(1);

why not int?

Add element at the back of the queue

Create empty queue of integers

Operations on queues (2)

Get element from the head of the queue and REMOVE IT

int element = queue.poll();
int element = queue.peek();

Get element from the head of the queue and DO NOT remove it

Operations on queues (3)

Check if the queue is empty

int elementCount = queue.size();

Check how many elements are in the queue

boolean isQueueEmpty = queue.isEmpty();

Questions?

Stacks

Some examples

  1. Pile of dishes you have to wash
  2. Undo (Ctrl + z) operation in Android Studio
  3. Company, in a crisis, sometimes fire the most junior member first (last hired)

Do you have more?

Stack

  • Add new elements at the top
  • Get existing elements from the top

What stacks do? (2)

Top

Bottom

Push

Pop

Last In First Out (LIFO)

Example: Waiting in line
revisited

Ivancho is again in the EVN office to pay his bill

the line is huge!

Can Ivancho skip the line?

Operations on stacks

Stack<Integer> stack = new Stack<>();
stack.push(1);

Add element at the top of the stack

Create empty stack of integers

Operations on stacks (2)

Get element from the top of the stack and REMOVE IT

int element = stack.pop();
int element = stack.peek();

Get element from the top of the stack and DO NOT remove it

Operations on stacks (3)

Check if the stack is empty

int elementCount = stack.size();

Check how many elements are in the stack

boolean isStackEmpty = stack.empty();

Questions?

Break (10 min)

Lists

How to decide what size your array should be?

What if you can't hire
Chuck Norris?

Use lists and you won't have to

List

  • Do not have to specify size
  • All the goodies from the arrays

Example: Shopping list

Ivancho have to buy all the items from the near
grocery shop that his mom ordered him.

if he does, he can spend a night in Galaxy with Mariika 

Ivancho has lousy memory but still can do it!
Idea: create a shopping list and store all items in it.
He has no pen and paper. Can he write an app?

Operations on lists

ArrayList<String> list = new ArrayList<>();
list.add("Semki");

Add element of the back of the list

Create empty list of strings

Operations on lists (2)

list.get(0);
list.remove(0);

Remove an item at specified index

Get an item at specified index

Operations on lists (3)

list.set(0, "Slaninka");
int elementCount = list.size();

Get the size of the list

Add item at specific place (index)

Questions?

Maps

Some examples

  1. Dictionary of words from Bulgarian to English or vice versa
  2. Look up a student by his ID number
  3. Associate contact name with his phone number

Do you have more?

Map

  • Add a pair of key and element (value)
  • Get element by it's key
  • Can give you an element in (near) constant time if you know it's key
  • Only one element for key (some do)
  • Slow when iterating over elements

Example: Shopping list
revisited

His mom asks him to visit the grocery shop again!
This time the task is just a bit (maybe more) harder.
He has to buy some groceries in different quantities.
His reward will be a week in Amsterdam,
close to the Red Light District...

Ivancho is very happy with his experience
with Mariika in Galaxy (hmm?).

Ivancho is very
enthusiastic about it!
He just have to 
modify his app.
Can you help?

Operations on maps

HashMap<String, Integer> map = new HashMap<>();
map.put("Krushi", 3);

Add element with a key

Create empty map of strings to integers

Operations on maps (2)

int quantity = map.get("Krushi");
map.remove("Krushi");

Remove an item with key

Get value for a key

Operations on maps (3)

boolean hashKrushi = map.contains("Krushi");
int elementCount = map.size();

Get the size of the map

Check if contains value with key

Questions?

Summary

  • Queues get elements from the front and add to the back
  • Stacks get elements from the top and add to the top
  • Lists are like arrays but you can store (almost) unlimited elements in them
  • Maps allow association of one element with another

Daily quiz

Questions?

Homework or not

Thank you :)

Lecture 5 - Data structures

By naughtyspirit

Lecture 5 - Data structures

  • 509