Note 1
Data Structures

So far, we discussed out C++ syntax, basic data structure, and brute forcing some of the problems. In this note, we are going to discuss about data structures. Learning data structures is not only important in competitive programming but also in computer science in general. Learning how to handle data more efficiently will make us better programmers, so let’s get right into it! Also, one more thing. This note is largely based on the video “Data Structures - Full Course Using C and C++” by Harsha and Animese [HA21].

1.1 Lists and Linked Lists

The first data structures that we are going to discuss are lists and linked lists. Now we went over lists briefly before when we discussed about arrays, but we haven’t really gotten into how arrays work under the hood. But before we get started, we need to understand how memory is stored in RAM, what a memory address is, and what a pointer is. I wrote a section specifically on that in Section ??, so if you are unfamiliar with these topics, please read that section first!

As always, let’s get started with definition.

Definition 1.1.1

A data structure is a method for efficiently storing and using data.

So an example would be arrays. We used array to store some list of data. Also when we say data structures, there are two main things: Abstract Data Types (ADT) and implementation. But we will usually refer to ADT, which defines the behavior of data structure and how we use it without revealing underlying implementation of them. Again, list is an exmple of ADT where arrays and linked list would be implementation.

Okay, now on to lists and linked list. So what is a list? An example of a list would be array. We could store, write, and read elements given a position. Array is a list because say we have array A. We can access to read/write/modify elements in A by using its index like A[10]. This is basically what list do!

[Picture]

Now list is very powerful when we know how many elements we have and we know exactly how much memory we would need. But what about the case when we don’t know it? Usually when we define array in C and C++, the allocator reserves consecutive slots in RAM for the array. In other words if we run out of slot because we need more elements, the allocator should reserve new slots with more consecutive blocks and copy all the previous elements.

What about time complexity? If we want to insert, remove, or add any element in a list, the worst case scenario would be to travel all elements in the list to the place we want, so the time complexity would be \(\mathcal {O}(n)\). And by the nature of lists, accessing would be constant time.

But again lists are good when we know exactly how many elements we have, but it’s not the best if we don’t know. Creating a new list and copying everything from before is very inefficient. This is where linked list comes in! Well, there are other alternatives as well, but let’s focus on linked list for now.

Linked list is basically a list whose slots are not necessary consecutive in memory. And theses slots are linked with pointers. So say we we need to store an integer value. The allocator will allocate \(4\) bytes of memory for the integer and next \(4\) or \(8\) bytes for pointer to the next element in the list. Then, the next element will have a value and a pointer to the next element in the list. The last element will have a value and \(0\) because no pointer that points to an existing memory address is zero and it tells the linked list that it is is the last element. So basically, linked list is a list that is connected through pointers.

[Picture]

Now let’s see time complexity for linked lists. Inserting and accessing elements requires linear time because in the worst case scenario, we have to go through every links. But for inserting at the beginning, it is constant time because we only care about the head.

Compared to lists like an array, accessing have worse time complexity. Also, it is harder to use! But there are certainly cases when linked list is better. We don’t waste memory! Large arrays may sometimes be unavailable because the slots are consecutive. Also, if we don’t use all slots, then we are basically wasting memory with fixed size. But for linked lists, we don’t waste any because request for what we need and we have more flexibility because we are not requesting for a large block. Now let’s see how we can implement a linked list with C++.

Code 1.1.2: 26sbqhf-01

1#include <iostream> 
2 
3struct element { 
4    int val; 
5    element* ptr; 
6}; 
7 
8int main() { 
9    element* HEAD = new element(); 
10    HEAD->val = 0; 
11 
12    element* temp = HEAD; 
13    for (int i = 1; i < 10; i++) { 
14        element* current = new element; 
15        current->val = i; 
16        current->ptr = nullptr; 
17 
18        temp->ptr = current; 
19        temp = current; 
20    } 
21 
22    element* search = HEAD; 
23    while ((*search).ptr != nullptr) { 
24        std::cout << (*search).val << " "; 
25        search = search->ptr; 
26    } 
27    std::cout << (*search).val << "\n"; 
28 
29    return 0; 
30}

I know this can look intimidating at first, but let’s break this part by part. First, we defined the struct element, which is literally an element in our linked list. Each element will contain a value and a pointer that points to another struct element.

Now, we will declare HEAD to be the pointer to the first head element declared with new element(). We then give value \(0\). Next, we are going to do the same thing. We declare another pointer variable temp, but this time, its value is initially HEAD.

Inside the for loop, we declare new pointer element that contains the value i and has a null pointer. So every current is the last element in our linked list. Then, we will tell temp that the next element succeeding it is current. Now, we are going to move the temp to the current new element. With this, we declared our linked list!

Now we want to search and print it. It works the same as we did it now. We are going to create another dummy pointer variable search and this is going to be the first element HEAD. Lastly, we print the last element by hand because while loop excludes it. Compiling and running it, we get the expected result!

Output 1.1.3

0 1 2 3 4 5 6 7 8 9