Data Structures LightHall Class

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

GETTING STARTED WITH DATA STRUCTURES

Adora Nwodo
@adoranwodo

Hi, I’m Adora!


Software Engineer
Author
Digital Creator
@adoranwodo

A data structure is a particular way of organizing data in


a computer so that it can be used effectively.
@adoranwodo
@adoranwodo
@adoranwodo

Heaps
Arrays
Hash Tables
Linked Lists
Graphs
Stacks
Matrices
Queues
Tries
Binary Trees
B-Trees
Binary Search Trees
N-ary Trees
@adoranwodo

Arrays
@adoranwodo

An array is a finite collection of elements of the same type.


@adoranwodo

★ Arrays are zero indexed. This means that an array with n elements is
indexed from 0 to n-1.

★ An array has a fixed size. When creating an array, you specify the
number of elements the array has. That’s it’s size.

★ Arrays are mutable. Meaning their state or data can be changed.


@adoranwodo

var numbers = new int[] {12 , 8, 5, 78};

12 8 5 78

0 1 2 3
@adoranwodo
REAL LIFE USES OF ARRAYS

API RESPONSES

When we make an API request to fetch data, ife we are getting more than one record,
they will usually be returned as a JSON array.

When we get the valid JSON array as a response, we can perform whatever action we
want on the data.

/*
The example below shows looping through a valid API response
and printing out the data
*/

var response = GetAllPendingTransactions();


foreach (var transaction in response)
{
Console.WriteLine(transaction.Id);
Console.WriteLine(transaction.Description);
}
@adoranwodo
REAL LIFE USES OF ARRAYS

REPRESENTING MATRICES

Since 2 dimensional arrays are represented as rows & columns and matrices are also
represented as rows and columns, we can represent matrices as 2 dimensional arrays while
programming.

Consider the 3x3 matrix below:


2 9 4
0 7 8
3 5 1

It’s equivalent 2d array representation will be:

var matrix = new int[3, 3] {


// Row 0
{ 2 , 9, 4 },
// Row 1
{ 0 , 7, 8 },
// Row 2
{ 3 , 5, 1 },

}
@adoranwodo
REAL LIFE USES OF ARRAYS

STORING DATA

Generally, arrays are used for storing all sorts of data ranging from data from a server, to data in
the applications memory and so on. Depending on what your apps logic is, you might need to
store a list of things at some point and arrays are one of the most common data structures that
exist for this scenario.

This data can exist:


★ In your applications memory
★ On a backend database
★ In a cache somewhere
★ In a file on a computer
@adoranwodo

Linked Lists
@adoranwodo

A linked list is a data structure whose order of elements is not by their


physical placement in memory. Instead, each element has a reference to
the next element.

Element 1 Element 2 Element 3 NULL


@adoranwodo

Each element or node in a linked list has these fields.

❖ Data field: This field contains the data that is contained in the node. The
programmer can store whatever data they want here, based on what their
logic is.

❖ Reference field: This field contains the reference to the next (or previous)
node in the sequence depending on the type of linked list.

NODE A NODE B NODE C

45 NODE B 10 NODE C 200

Data Next Ref Data Next Ref Data Next Ref


@adoranwodo

Types of Linked Lists

The differences in Linked Lists are based on the multiple types of references
possible:

❖ Singly Linked Lists


❖ Doubly Linked Lists
❖ Circular Linked Lists
@adoranwodo

In a Singly Linked List, you are only allowed to have a “Next” reference. What this
means is that you have one reference field that points to the next element in the
sequence.

45 10 200 NULL

NODE A NODE B NODE C

45 NODE B 10 NODE C 200

Data Next Ref Data Next Ref Data Next Ref


@adoranwodo

In a Doubly Linked List, you are allowed to have two (double) references. One is a next reference
and the other, a previous reference. What this means is that you have two reference fields
where one points to the next element in the sequence and the other points to the previous
element

45 10 200 NULL

NODE A NODE B NODE C

45 NODE B NODE A 10 NODE C NODE B 200

Previous Next Previous Next Previous Next


Data Data Data
Reference Reference Reference Reference Reference Reference
@adoranwodo

In a Circular Linked List, you are allowed to have two (double) references. One is a next
reference and the other, a previous reference. What this means is that you have two reference
fields where one points to the next element in the sequence and the other points to the previous
element

45 10 200

NODE A NODE B NODE C

NODE C 45 NODE B NODE A 10 NODE C NODE B 200 NODE A

Previous Next Previous Next Previous Next


Data Data Data
Reference Reference Reference Reference Reference Reference
@adoranwodo

Creating A Music Playlist With Linked Lists


@adoranwodo
@adoranwodo
@adoranwodo
@adoranwodo
@adoranwodo
@adoranwodo
@adoranwodo
@adoranwodo

Hash Tables
@adoranwodo

Hash Table is a data structure which stores data in an associative


manner. In a hash table, data is stored in an array format, where each
data value has its own unique index value. Access of data becomes very
fast if we know the index of the desired data.
@adoranwodo

What is Hashing?
Hashing is a technique to convert a range of key values into a range of
indexes of an array.
@adoranwodo

What do you do when you don’t know the


answer to an interview question?
@adoranwodo

What do you do when you don’t know the


answer to an interview question?

USE A HASHMAP
@adoranwodo

Stacks
@adoranwodo
@adoranwodo
@adoranwodo
@adoranwodo
@adoranwodo
@adoranwodo

Stacks in real applications:


❖ Text editors
❖ String manipulation
❖ Browsers
@adoranwodo

Queues
@adoranwodo

Graphs, Tries and more. Next class?


@adoranwodo

Questions?

You might also like