Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dyn_slist_nc.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
39# ifndef TPL_DYN_SLIST_NC_H
40# define TPL_DYN_SLIST_NC_H
41
42# include <tpl_snode_nc.H>
43# include <ah-errors.H>
44
45namespace Aleph
46{
63 template <typename T> class Dyn_Slist_Nc : public Snode_Nc <T>
64 {
65 public:
67
68 private:
70 size_t num_items;
71
72 public:
73
75 void empty()
76 {
77 while (not this->is_empty())
78 delete Node::remove_next();
79 this->reset();
80 head = this;
81 num_items = 0;
82 }
83
94 T & insert(const T & data) throw(std::exception, std::bad_alloc)
95 {
96 Node * p = new Node(data);
97 Node::insert(p);
98
99 if (num_items++ == 0)
100 head = p;
101
102 return p->get_data();
103 }
104
115 T & append(const T & data) throw(std::exception, std::bad_alloc)
116 {
117 assert(head not_eq nullptr);
118 Node * p = new Node(data);
119 head->insert(p);
120
121 head = p;
122 ++num_items;
123 return p->get_data();
124 }
125
128 {
129 ah_underflow_error_if(this->is_empty()) << "List is empty";
130
131 return this->get_next()->get_data();
132 }
133
136 {
137 ah_underflow_error_if(this->is_empty()) << "List is empty";
138
139 return head->get_data();
140 }
141
149 {
150 ah_underflow_error_if(this->is_empty()) << "List is empty";
151
152 Node * p = Node::remove_next();
153 T ret_val = p->get_data();
154 delete p;
155 if (--num_items == 0)
156 head = this;
157 return ret_val;
158 }
159
161 T & put(const T & item) { return append(item); }
162
164 T get() { return remove_first(); }
165
167 T & rear() { return get_last(); }
168
170 T & front() { return get_first(); }
171
173 T & push(const T & item) { return insert(item); }
174
176 T pop() { return remove_first(); }
177
179 T & top() const { return get_first(); }
180
181 const size_t & size() const { return num_items; }
182
184 {
185 /* TODO: Agregar atributos y métodos que hagan falta
186 la implementación de este iterador la hice minimalista
187 para pruebas de Dyn_Slist_Nc
188 */
189 public:
191 {
192 // empty
193 }
194
196 {
197 return Node::Iterator::get_curr()->get_data();
198 }
199
201
202 };
203
206 {
207 // empty
208 }
209
212 {
213 for (Iterator it(const_cast <Dyn_Slist_Nc &>(l));
214 it.has_curr(); it.next_ne())
215 append(it.get_curr());
216 }
217
219 {
220 empty();
221 }
222
232 {
233 if (this == &list)
234 return *this;
235
236 empty();
237
238 assert(this->is_empty());
239
240 for (Iterator it(const_cast<Dyn_Slist_Nc &>(list));
241 it.has_curr(); it.next_ne())
242 append(it.get_curr());
243
244 return *this;
245 }
246
247 T & operator [] (const size_t & n)
248 {
249 Iterator it(*this);
250 for (int i = 0; i < n and it.has_curr(); ++i, it.next_ne());
251
252 return it.get_curr();
253 }
254 };
255}
256
257# endif
258
Exception handling system with formatted messages for Aleph-w.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:368
Iterator(Dyn_Slist_Nc< T > &list)
T & get_curr_n() const noexcept
Lista dinámica de elementos de tipo T.
T & rear()
Si this e suna cola, entonces retorna el elemento más joven.
T remove_first()
Elimina el primer elemento de la lista: retorna una copia del elemento eliminado.
T pop()
Si this es una pila, entonces elimina el tope.
T & operator[](const size_t &n)
T & top() const
Si this es una pila, entonces retorna el tope.
Dyn_Slist_Nc(const Dyn_Slist_Nc &l)
Constructor de copia.
T & front()
Si this e suna cola, entonces retorna el elemento más antiguo.
T & get_first()
Retorna una referencia al primer elemento de la lista.
T & insert(const T &data)
Inserta un elemento al principio de la lista.
T & put(const T &item)
Si this es una cola, entonces mete el elemento item.
T & push(const T &item)
Si this es una pila, entonces inserta item.
const size_t & size() const
T & get_last()
Retorna una referencia al último elemento de la lista.
T get()
Si this es una cola, entonces extrae el elemento más antiguo.
Dyn_Slist_Nc()
Constructor vacío.
T & append(const T &data)
Inserta un elemento al final de la lista.
void empty()
Vacía totalmente a la lista.
Dyn_Slist_Nc & operator=(const Dyn_Slist_Nc &list)
Asignación de lista dinámica con semántica lvalue.
Snode_Nc * remove_next()
Elimina el nodo siguiente a this.
Snode_Nc *& get_next()
Retorna el nodo siguiente a this.
T & get_data()
Retorna una referencia al dato contenido en el nodo.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:107
DynList< T > maps(const C &c, Op op)
Classic map operation.
Typed singly linked node (no check).
DynList< int > l