How to Win Coding Competitions: Secrets of Champions
Week 2: Computational complexity. Linear data structures
Lecture 4: List
Pavel Krotkov
Saint Petersburg 2016
Operations on list
Let’s define operations we need for this data structure.
2/9
Operations on list
Let’s define operations we need for this data structure.
O(1) operations
2/9
Operations on list
Let’s define operations we need for this data structure.
O(1) operations
I inserting an element to any place of data structure
2/9
Operations on list
Let’s define operations we need for this data structure.
O(1) operations
I inserting an element to any place of data structure
I removing an element from any place of data structure
2/9
Operations on list
Let’s define operations we need for this data structure.
O(1) operations
I inserting an element to any place of data structure
I removing an element from any place of data structure
Accessing element by its index can be implemented in linear time.
2/9
Main idea
Consider the following structure.
3/9
Main idea
Consider the following structure.
I all elements are stored separately
3/9
Main idea
Consider the following structure.
I all elements are stored separately
I link to the whole structure is link to the first element (or null if the structure is
empty)
3/9
Main idea
Consider the following structure.
I all elements are stored separately
I link to the whole structure is link to the first element (or null if the structure is
empty)
I first element stores its value and link to the second element (or null if the
structure consists of one element)
3/9
Main idea
Consider the following structure.
I all elements are stored separately
I link to the whole structure is link to the first element (or null if the structure is
empty)
I first element stores its value and link to the second element (or null if the
structure consists of one element)
I second element stores its value and link to the third element (or null if the
structure consists of two elements)
3/9
Main idea
Consider the following structure.
I all elements are stored separately
I link to the whole structure is link to the first element (or null if the structure is
empty)
I first element stores its value and link to the second element (or null if the
structure consists of one element)
I second element stores its value and link to the third element (or null if the
structure consists of two elements)
I etc.
3/9
Main idea
Consider the following structure.
I all elements are stored separately
I link to the whole structure is link to the first element (or null if the structure is
empty)
I first element stores its value and link to the second element (or null if the
structure consists of one element)
I second element stores its value and link to the third element (or null if the
structure consists of two elements)
I etc.
head 3 6 2 null
3/9
Insertions
We can easily insert any element to any place in this structure.
4/9
Insertions
We can easily insert any element to any place in this structure.
I insertion takes only creating a new element and changing one link
4/9
Insertions
We can easily insert any element to any place in this structure.
I insertion takes only creating a new element and changing one link
head 3 6 2 null
4/9
Insertions
We can easily insert any element to any place in this structure.
I insertion takes only creating a new element and changing one link
head 3 6 2 null
Note
I we need to have a link to an element after which we want to insert a new one
4/9
Deletions
We also can delete any element from any place in this structure.
5/9
Deletions
We also can delete any element from any place in this structure.
I deletion takes only changing one link
5/9
Deletions
We also can delete any element from any place in this structure.
I deletion takes only changing one link
head 3 6 2 null
5/9
Deletions
We also can delete any element from any place in this structure.
I deletion takes only changing one link
head 3 6 2 null
Note
I we need to have a link to an element previous to an element we want to delete
5/9
Code examples
C++ code example
template <typename T>
struct list node {
list node *next;
T value;
};
6/9
Code examples
C++ code example
template <typename T>
struct list node {
list node *next;
T value;
};
Java code example
class ListNode <T> {
ListNode next;
T value;
}
6/9
Code examples
insert(p, new)
new.next ← p.next
p.next ← new
7/9
Code examples
insert(p, new)
new.next ← p.next
p.next ← new
delete next(p)
p.next ← p.next.next
7/9
Final thoughts
You should be very careful with potential null references while implementing this data
structure.
8/9
Final thoughts
You should be very careful with potential null references while implementing this data
structure.
List can also be doubly linked.
I each node store links to next and previous nodes
8/9
Thank you
for your attention!
9/9