Skip to content

Commit d43e26b

Browse files
authored
Merge pull request #37 from urbytes21/dev_branch
id 1770626386
2 parents 2e4a02f + acfdc21 commit d43e26b

6 files changed

Lines changed: 159 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ set(APP_SOURCES
134134
"src/core/datatypes/container/unordered/UnorderedMap.cpp"
135135
"src/core/expression/Lambda.cpp"
136136
"src/core/expression/FunctionPointer.cpp"
137+
"src/core/datatypes/container/adapter/Queue.cpp"
138+
"src/core/datatypes/container/adapter/Stack.cpp"
139+
"src/core/datatypes/container/sequence/Deque.cpp"
137140
## LC
138141
"src/leetcode/arrays/two_sum/TwoSum.cpp"
139142
"src/leetcode/arrays/median_two_arrays/MedianTwoSortedArrays.cpp"

src/core/basics/ControlFlow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void loops() {
8282
++j;
8383
} while (j < 2);
8484

85-
// for
85+
// for(initialization; condition; update)
8686
for (int k = 0; k < 3; ++k) {
8787
cout << "for loop k = " << k << "\n";
8888
}

src/core/datatypes/container/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ Unordered associative containers implement unsorted (hashed) data structures tha
1919
|unordered_map|collection of key-value pairs, hashed by keys, keys are unique|
2020
|unordered_set|collection of unique keys, hashed by keys|
2121

22+
## 3. Adapter Container
23+
**Container adaptors** provide a different interface for **sequential containers**.
24+
|name|description|
25+
|---|---|
26+
|queue|apdapts a container to provide queue (**FIFO** data structure)|
27+
|stack|adapts a container to provide stack (**LIFO** data structure)|
28+
2229
TBD
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// cppcheck-suppress-file []
2+
3+
/**
4+
* std::queue
5+
* template<
6+
* class T,
7+
* class Container = std::deque<T>
8+
* > class queue;
9+
*
10+
* Queue acts as a wrapper to the underlying container (sequence container e.g. std::deque, std::list)
11+
* but only a specific
12+
* set of functions is provided.
13+
* It pushs the elements on the back of the underlying container and pops them from the front
14+
*
15+
*/
16+
#include <iostream>
17+
#include <queue> // for std::queue
18+
19+
namespace {
20+
void run() {
21+
// 1. Init
22+
std::queue<int> m_queue{};
23+
24+
// 2. Modifiers
25+
m_queue.push(0); // inserts elements at the end
26+
m_queue.push(1); // q = 0 1
27+
m_queue.push(2); // q = 0 1 2
28+
m_queue.push(3); // q = 0 1 2 3
29+
30+
m_queue.pop(); // removes the 1st element q = 1 2 3
31+
32+
// 3. Element access
33+
std::cout << "fisrt ele: " << m_queue.front() << "\n";
34+
std::cout << "last ele: " << m_queue.back() << "\n";
35+
36+
// 4. Get all elements and remove
37+
std::cout << "all ele: \n";
38+
for (; !m_queue.empty(); m_queue.pop()) {
39+
std::cout << m_queue.front() << " ";
40+
}
41+
std::cout << "\n";
42+
}
43+
} // namespace
44+
45+
struct QueueRunner {
46+
QueueRunner() {
47+
std::cout << "\n--- std::queue Example ---\n";
48+
run();
49+
}
50+
};
51+
52+
static QueueRunner autoRunner;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// cppcheck-suppress-file []
2+
3+
/**
4+
* std::stack
5+
* template<
6+
* class T,
7+
* class Container = std::deque<T>
8+
* > class stack;
9+
*
10+
* Queue acts as a wrapper to the underlying container (sequence container e.g. std::deque, std::list)
11+
* but only a specific
12+
* set of functions is provided.
13+
* It pushs and pops the element from the back of the underlying container, a.k.a the top of the stack
14+
*
15+
*/
16+
#include <iostream>
17+
#include <stack> // for std::queue
18+
19+
namespace {
20+
void run() {
21+
// 1. Init
22+
std::stack<int> m_stack{};
23+
24+
// 2. Modifiers
25+
m_stack.push(0); // inserts elements at the end
26+
m_stack.push(1); // q = 0 1
27+
m_stack.push(2); // q = 0 1 2
28+
m_stack.push(3); // q = 0 1 2 3
29+
m_stack.pop(); // removes the top element q = 0 1 2
30+
31+
// 3. Element access
32+
std::cout << "top ele: " << m_stack.top()
33+
<< "\n"; // access the top of the stack
34+
35+
// 4. Get all elements and remove
36+
std::cout << "all ele (reverse): \n";
37+
for (; !m_stack.empty(); m_stack.pop()) {
38+
std::cout << m_stack.top() << " ";
39+
}
40+
std::cout << "\n";
41+
}
42+
} // namespace
43+
44+
struct StackRunner {
45+
StackRunner() {
46+
std::cout << "\n--- std::stack Example ---\n";
47+
run();
48+
}
49+
};
50+
51+
static StackRunner autoRunner;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// cppcheck-suppress-file []
2+
3+
/**
4+
* Double-ended queue is an indexed sequence container that allows fast insertion and deletion
5+
* and never invalidates pointers or references to the rest of the elements because it use two pointer for 1 elem.
6+
=> Indexing and iterating performance < vector
7+
* The elements of a deque are not stored contiguously
8+
9+
* Insertion or removal of elements - linear O(n)
10+
* Random access - constant O(1)
11+
*/
12+
#include <deque>
13+
#include <iostream>
14+
15+
namespace {
16+
void run() {
17+
// 1) Init
18+
std::deque<int> m_deque = {9, 100, 0};
19+
20+
// 2) Modifiers
21+
m_deque.clear();
22+
23+
m_deque.push_back(1);
24+
m_deque.push_back(2);
25+
m_deque.push_back(3);
26+
m_deque.push_back(4);
27+
m_deque.pop_back();
28+
m_deque.pop_front();
29+
m_deque.push_front(99);
30+
31+
for (const auto& e : m_deque) {
32+
std::cout << e << " ";
33+
}
34+
std::cout << std::endl;
35+
}
36+
} // namespace
37+
38+
struct DequeRunner {
39+
DequeRunner() {
40+
std::cout << "\n--- std::deque Example ---\n";
41+
run();
42+
}
43+
};
44+
45+
static DequeRunner autoRunner;

0 commit comments

Comments
 (0)