Data Structures: Linked Lists and Hash Tables

Data structures

Data structures are a way of organizing collections of data in the computer's memory

Data structures

Data structures are a way of organizing collections of data in the computer's memory

Examples of data structures we have seen so far
  • Arrays

Data structures

Data structures are a way of organizing collections of data in the computer's memory

Examples of data structures we have seen so far
  • Objects

Data structures

Data structures are a way of organizing collections of data in the computer's memory

Examples of data structures we have seen so far
  • Networks of cities (Assignment 4), a social network (last class)

Data structures

Data structures are a way of organizing collections of data in the computer's memory

Examples of data structures we have seen so far
  • Arrays
  • Objects
  • Networks of cities (Assignment 4), a social network (last class)
  • Linked Lists (today's class)
  • Hashtables (today's class)

Linked Lists

Linked Lists

This is a visual representation of a linked list using the Person objects from the social network

Linked Lists

This is a visual representation of a linked list using the Person objects from the social network

Linked Lists

This is a visual representation of a linked list using the Person objects from the social network

Linked Lists

This is a visual representation of a linked list using the Person objects from the social network

Linked Lists

This is a visual representation of a linked list using the Person objects from the social network

Linked Lists

This is a visual representation of a linked list using the Person objects from the social network

Linked Lists

This is a visual representation of a linked list using the Person objects from the social network

Linked Lists

This is a visual representation of a linked list using int values

Going through the components of a LinkedList

We need to define a class for each element in the list: the Node class

The Node class should have:

Going through the components of a LinkedList

We need to define a class for doing operations on the list: the LinkedList class

The LinkedLsit class should have:

Inserting elements in a linked list

Inserting an element at the beginning

Inserting elements in a linked list

Inserting an element at the end

Inserting elements in a linked list

Inserting an element somewhere in the middle

Removing elements in a linked list

Removing an element from the list

Removing elements in a linked list

Removing an element from the list

Some remarks about LinkedList



In big O notation

Operations in a LinkedList have a running time of ==> O(n)

To try for yourself

Hash tables

Hash tables

Hash tables can be viewed as dictionaries

We use a hashing function to determine where an element should go

Implementing a Hashtable

We use an array to store the entries in our dictionary

We need to implement the hashing function to determine where an element should go

Hash tables

The hash function maps data to hash values (positions in the array)

In this case, the hashing function maps the first letter of a name to a position in the array

When two or more elements have the same hash value, a collision occurs.

Hash tables with multiple elements per entry

A first step to deal with collisions, is to make each entry point to another data structure; e.g. a LinkedList

Implementing a Hashtable with LinkedLists for each position in the array

We use an array to store the entries in our dictionary

Hash tables with multiple elements per entry

A first step to deal with collisions, is to make each entry point to another data structure; e.g. a LinkedList

Some remarks about Hash tables



In big O notation

Operations in a Hash Table have a running time of ==> O(k) , where k is the number of elements in each position of the entries array.

Operations in a Hash Table have a running time of ==> O(1)

Resources

/