Data Structures and Algorithms

Sequential Data Structures: Linked Lists & Arrays

executive summary

Store:
a sequence of elements

Manipulate:

add/remove elements (at one of the ends or in the middle)
update an exisiting element (at one of the ends or in the middle)

link to companion notes · visualgo (arrays) · visualgo (linked lists)

Approaches: linked lists, arrays, dynamic arrays

Sets & Sequential Data

The atomic types have served us well,

but it is now time to build on them.

Say you are a real estate agent &

need to reserve some houses in a new

apartment complex.

The builder can block any contiguous chunk

of N houses for you.

Your list of clients may evolve over time.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

⛔️

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

⛔️

👇

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

🔑

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

🔑

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

🔑

🔑

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

🔑

🔑

Say you are a real estate agent &

need to reserve some houses in a new apartment complex.

🔑

🔑

🔑

🔑

Arrays

Linked Lists

Looking up the i-th element 

Adding a new element at the start

Adding a new element at the end

Removing the i-th element

instant

instant

instant*

\(\approx\) n

\(\approx\) n

\(\approx\) n-i

\(\approx\) i

Adding a new element at the i-th location

\(\approx\) n-i

\(\approx\) min( i,n-i)**

\(\approx\) min( i,n-i)**

* Assuming we have a tail pointer.

** Assuming we have backward pointers.

Why can't everything be fast?

DYNAMIC ARRAYS

Whenever the array overflows, double its size.

Total cost = total #insertions + total #copy operations

Total cost = total #insertions + total #copy operations

\(\leqslant\) N + N = 2N

...since each copy can be associated with a distinct insert,

& the #inserts = N; so #copy operations \(\leqslant\) N

Why can't everything be fast?

DYNAMIC ARRAYS

Whenever the array overflows, double its size.

TBA

Notes and slides on implementation details

Examples and problems