203using namespace Aleph;
212 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
213 cout <<
"║ EXAMPLE 1: Stack (LIFO - Last In First Out) ║\n";
214 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
216 cout <<
"Stack follows LIFO principle: last element pushed is first popped.\n\n";
221 cout <<
"--- DynListStack operations ---\n\n";
224 cout <<
"Pushing: Apple, Banana, Cherry, Date\n";
226 stack.
push(
"Banana");
227 stack.
push(
"Cherry");
230 cout <<
"Stack size: " << stack.
size() <<
"\n";
231 cout <<
"Top element: " << stack.
top() <<
"\n\n";
234 cout <<
"Popping elements (LIFO order):\n";
236 cout <<
" Pop: " << stack.
pop() <<
"\n";
238 cout <<
"\n--- Practical application: Balanced parentheses ---\n\n";
242 for (
char c : expr) {
243 if (c ==
'(' || c ==
'[' || c ==
'{')
245 else if (c ==
')' || c ==
']' || c ==
'}') {
248 if ((c ==
')' && top !=
'(') ||
249 (c ==
']' && top !=
'[') ||
250 (c ==
'}' && top !=
'{'))
257 auto test_expr = [&](
const string& expr) {
258 cout <<
" \"" << expr <<
"\" → "
275 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
276 cout <<
"║ EXAMPLE 2: Queue (FIFO - First In First Out) ║\n";
277 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
279 cout <<
"Queue follows FIFO principle: first element added is first removed.\n\n";
283 cout <<
"--- DynListQueue operations ---\n\n";
286 cout <<
"Enqueueing: Task1, Task2, Task3, Task4\n";
292 cout <<
"Queue size: " << queue.
size() <<
"\n";
293 cout <<
"Front element: " << queue.
front() <<
"\n";
294 cout <<
"Rear element: " << queue.
rear() <<
"\n\n";
297 cout <<
"Dequeueing elements (FIFO order):\n";
299 cout <<
" Dequeue: " << queue.
get() <<
"\n";
301 cout <<
"\n--- Practical application: Print job scheduler ---\n\n";
316 cout <<
"Print queue:\n";
320 cout <<
" Printing: " <<
job.name <<
" (" <<
job.pages <<
" pages)\n";
333 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
334 cout <<
"║ EXAMPLE 3: DynArray (Resizable Array) ║\n";
335 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
337 cout <<
"DynArray provides O(1) random access with dynamic resizing.\n\n";
341 cout <<
"--- Basic operations ---\n\n";
344 cout <<
"Appending: 10, 20, 30, 40, 50\n";
351 cout <<
"Size: " << arr.
size() <<
"\n";
352 cout <<
"Elements: ";
353 for (
size_t i = 0; i < arr.
size(); ++i)
354 cout << arr(i) <<
" ";
358 cout <<
"Random access:\n";
359 cout <<
" arr(0) = " << arr(0) <<
"\n";
360 cout <<
" arr(2) = " << arr(2) <<
"\n";
361 cout <<
" arr(4) = " << arr(4) <<
"\n\n";
364 cout <<
"Modifying arr(2) = 300\n";
366 cout <<
"Elements: ";
367 for (
size_t i = 0; i < arr.
size(); ++i)
368 cout << arr(i) <<
" ";
371 cout <<
"--- Functional operations ---\n\n";
374 for (
int i = 1; i <= 10; ++i)
377 cout <<
"Original: ";
395 cout <<
"Sum: " <<
sum <<
"\n";
405 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
406 cout <<
"║ EXAMPLE 4: DynList and DynDlist (Linked Lists) ║\n";
407 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
409 cout <<
"Linked lists allow O(1) insertion/deletion at any known position.\n\n";
412 cout <<
"--- DynList (singly linked) ---\n\n";
417 cout <<
"Inserting at front: 3, 2, 1\n";
423 cout <<
"Appending at end: 4, 5\n";
430 cout <<
"Size: " << slist.
size() <<
"\n\n";
433 cout <<
"--- DynDlist (doubly linked) ---\n\n";
437 cout <<
"Inserting: First, Second, Third\n";
443 dlist.
for_each([](
const string& s) {
cout << s <<
" "; });
450 cout <<
"Remaining: ";
451 dlist.
for_each([](
const string& s) {
cout << s <<
" "; });
462 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
463 cout <<
"║ EXAMPLE 5: Stack vs Queue Comparison ║\n";
464 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
470 cout <<
"Adding elements 1, 2, 3, 4, 5 to both structures:\n\n";
471 for (
int i = 1; i <= 5; ++i) {
477 cout <<
"Removal order:\n";
478 cout <<
" Stack (LIFO): ";
483 cout <<
" Queue (FIFO): ";
488 cout <<
"\nUse Stack for: undo/redo, recursion, backtracking\n";
489 cout <<
"Use Queue for: scheduling, BFS, buffering\n";
499 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
500 cout <<
"║ Linear Data Structures in Aleph-w Library ║\n";
502 cout <<
"║ Aleph-w Library - https://github.com/lrleon/Aleph-w ║\n";
503 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n";
512 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
513 cout <<
"║ Summary ║\n";
514 cout <<
"╠══════════════════════════════════════════════════════════════════╣\n";
515 cout <<
"║ DynListStack: Dynamic LIFO stack (linked list based) ║\n";
516 cout <<
"║ DynListQueue: Dynamic FIFO queue (circular list based) ║\n";
517 cout <<
"║ DynArray: Resizable array with O(1) access ║\n";
518 cout <<
"║ DynList: Singly linked list ║\n";
519 cout <<
"║ DynDlist: Doubly linked list ║\n";
521 cout <<
"║ All support functional operations: map, filter, fold, for_each ║\n";
522 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n\n";
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
Dynamic doubly linked list with O(1) size and bidirectional access.
T remove_last()
Remove the last item of the list; return a copy of removed item.
T & append(const T &item)
Append a copied item at the end of the list.
T remove_first()
Remove the first item of the list; return a copy of removed item.
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
constexpr size_t size() const noexcept
Return the number of elements.
T get()
Remove the oldest item of the queue.
T & front()
Return a modifiable reference to the oldest item in the queue.
T & rear()
Return a modifiable reference to the youngest item in the queue.
bool is_empty() const noexcept
Return true if this is empty.
Dynamic stack of elements of generic type T based on a singly linked list.
T & top()
Return a modifiable reference to the top item of the stack.
bool is_empty() const noexcept
Check if the stack is empty.
constexpr size_t size() const noexcept
Return the number of elements in the stack.
T pop()
Remove and return the top item of the stack.
T & push(const T &data)
Push an item by copy onto the top of the stack.
Dynamic singly linked list with functional programming support.
T & insert(const T &item)
Insert a new item by copy.
T & append(const T &item)
Append a new item by copy.
constexpr bool is_empty() const noexcept
Return true if list is empty.
size_t size() const noexcept
Count the number of elements of the list.
__T foldl(const __T &init, Op &op) const
Fold the elements of the container to a specific result.
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criteria.
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Main namespace for Aleph-w library functions.
DynList< T > maps(const C &c, Op op)
Classic map operation.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Circular queue implementations backed by arrays.
Stack implementations backed by dynamic or fixed arrays.
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.
Dynamic queue implementation based on linked lists.
Dynamic stack implementation based on linked lists.
Alias for htlist.H (DynList implementation).