Skip to content

Commit abf8670

Browse files
committed
fix: dynamic data for CSI281
1 parent 24739eb commit abf8670

3 files changed

Lines changed: 133 additions & 2 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Dynamic data
2+
3+
In C++'s Standard Library, we have a bunch of data structures already implemented for us. But you need to understand what is inside it in order do ponder the best tool for your job. In this week we are going to cover Dynamic Arrays (equivalent of std::vector) and Linked Lists(equivalent of std::list) .
4+
5+
## Dynamic Arrays
6+
7+
A dynamic array is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard libraries in many modern mainstream programming languages. Let's try to implement one here for the sake of teaching purposes;
8+
9+
```c++
10+
template<typename T>
11+
struct Vector {
12+
private:
13+
size_t _size;
14+
size_t _capacity;
15+
T* _data;
16+
public:
17+
// constructors
18+
Vector() : _size(0), _capacity(1), _data(new T[1]) {}
19+
explicit Vector(size_t size) : _size(size), _capacity(size), _data(new T[size]) {}
20+
21+
// destructor
22+
~Vector() { delete[] _data;}
23+
24+
// accessors
25+
size_t size() const { return _size; }
26+
size_t capacity() const { return _capacity;}
27+
28+
// push_back takes care of resizing the array if necessary
29+
// this insertion will amortize the cost of resizing
30+
void push_back(const T& value) {
31+
if (_size == _capacity) {
32+
// growth factor of 2
33+
_capacity = _capacity == 0 ? 1 : _capacity * 2;
34+
// allocate new memory
35+
T* new_data = new T[_capacity];
36+
// copy the old data into the new memory
37+
for (size_t i = 0; i < _size; ++i)
38+
new_data[i] = _data[i];
39+
// release the old memory
40+
delete[] _data;
41+
// update the data pointer
42+
_data = new_data;
43+
}
44+
_data[_size++] = value;
45+
}
46+
47+
// operator[] for read-write access
48+
T& operator[](size_t index) { return _data[index]; }
49+
50+
// other functions
51+
// ...
52+
};
53+
```
54+
55+
With this boilerplate you should be able to implement the rest of the functions for the Vector class.
56+
57+
## Linked Lists
58+
59+
A linked list is a linear access, variable-size list data structure that allows elements to be added or removed without the need of resizing. It is supplied with standard libraries in many modern mainstream programming languages. Let's try to build one here for the sake of teaching purposes;
60+
61+
```c++
62+
// linkedlist
63+
template <typename T>
64+
struct LinkedList {
65+
private:
66+
// linkedlist node
67+
struct LinkedListNode {
68+
T data;
69+
LinkedListNode *next;
70+
LinkedListNode(T data) : data(data), next(nullptr) {}
71+
};
72+
73+
LinkedListNode *_head;
74+
size_t _size;
75+
public:
76+
LinkedList() : _head(nullptr), _size(0) {}
77+
78+
// delete all nodes in the linkedlist
79+
~LinkedList() {
80+
while (_head) {
81+
LinkedListNode *temp = _head;
82+
_head = _head->next;
83+
delete temp;
84+
}
85+
}
86+
87+
// _size
88+
size_t size() const { return _size; }
89+
90+
// is it possible to make it O(1) instead of O(n)?
91+
void push_back(T data) {
92+
if (!_head) {
93+
_head = new LinkedListNode(data);
94+
_size++;
95+
return;
96+
}
97+
auto* temp = _head;
98+
while (temp->next)
99+
temp = temp->next;
100+
temp->next = new LinkedListNode(data);
101+
_size++;
102+
}
103+
104+
// operator[] for read-write access
105+
T &operator[](size_t index) {
106+
auto* temp = _head;
107+
for (size_t i = 0; i < index; i++)
108+
temp = temp->next;
109+
return temp->data;
110+
};
111+
112+
// other functions
113+
};
114+
```
115+
116+
## Homework
117+
118+
For both, implement the following functions:
119+
- `T* find(const T& value)`: returns a pointer to the first occurrence of the value in the collection, or nullptr if the value is not found.
120+
- `bool contains(const T& value)`: returns true if the value is found in the collection, false otherwise.
121+
- `T& at(size_t index)`: returns a reference to the element at the specified index. If the index is out of bounds, throw an `std::out_of_range` exception.
122+
- `void push_front(const T& value)`: adds a new element to the beginning of the collection.
123+
- improve push_back of the linkedlist to be O(1) instead of O(n);
124+
- `void insert_at(size_t index, const T& value)`: inserts a new element at the specified index. If the index is out of bounds, throw an `std::out_of_range` exception.
125+
- `void pop_front()`: removes the first element of the collection.
126+
- `void pop_back()`: removes the last element of the collection. Is it possible to make it O(1) instead of O(n)?
127+
- `void remove_all(const T& value)`: removes all occurrences of the value from the collection.
128+
- `void remove_at(size_t index)`: removes the element at the specified index. If the index is out of bounds, throw an `std::out_of_range` exception.
129+
130+
Now compare the complexity of linked list and dynamic array for each of the functions you implemented and create a table. What is the best data structure for each use case? Why?

courses/portfolio/03-structure/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
Create a single page app containing most of these features listed here.
44

5-
## Head
5+
## Head / Summary
6+
67
Chose carefully what to you use as a head of your page. It is the first thing a person reads. It can be an impactful message, a headline, personal statement, background video or very limited interactive section.
78

89
!!! note

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ nav:
4545
- courses/algorithms/README.md
4646
- Introduction to Algorithms: courses/algorithms/01-introduction/README.md
4747
- Algorithm Analysis: courses/algorithms/02-analysis/README.md
48-
- Dynamic Dana: courses/algorithms/03-dynamic-data/README.md
48+
- Dynamic Data: courses/algorithms/03-dynamic-data/README.md
4949
- Sorting: courses/algorithms/04-sorting/README.md
5050
- Divide and Conquer: courses/algorithms/05-divide-and-conquer/README.md
5151
- Hashtables: courses/algorithms/06-hashtables/README.md

0 commit comments

Comments
 (0)