Aleph-w  1.9
General library for algorithms and data structures
tpl_dyn_slist_nc.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 # ifndef TPL_DYN_SLIST_NC_H
28 # define TPL_DYN_SLIST_NC_H
29 
30 # include <tpl_snode_nc.H>
31 
32 namespace Aleph
33 {
50  template <typename T> class Dyn_Slist_Nc : public Snode_Nc <T>
51  {
52  public:
53  typedef Snode_Nc <T> Node;
54 
55  private:
56  Node * head;
57  size_t num_items;
58 
59  public:
60 
62  void empty()
63  {
64  while (not this->is_empty())
65  delete Node::remove_next();
66  this->reset();
67  head = this;
68  num_items = 0;
69  }
70 
81  T & insert(const T & data) throw(std::exception, std::bad_alloc)
82  {
83  Node * p = new Node(data);
84  Node::insert(p);
85 
86  if (num_items++ == 0)
87  head = p;
88 
89  return p->get_data();
90  }
91 
102  T & append(const T & data) throw(std::exception, std::bad_alloc)
103  {
104  assert(head not_eq nullptr);
105  Node * p = new Node(data);
106  head->insert(p);
107 
108  head = p;
109  ++num_items;
110  return p->get_data();
111  }
112 
114  T & get_first()
115  {
116  if (this->is_empty())
117  throw std::underflow_error("List is empty");
118 
119  return this->get_next()->get_data();
120  }
121 
123  T & get_last()
124  {
125  if (this->is_empty())
126  throw std::underflow_error("List is empty");
127 
128  return head->get_data();
129  }
130 
138  {
139  if (this->is_empty())
140  throw std::underflow_error("List is empty");
141 
142  Node * p = Node::remove_next();
143  T ret_val = p->get_data();
144  delete p;
145  if (--num_items == 0)
146  head = this;
147  return ret_val;
148  }
149 
151  T & put(const T & item) { return append(item); }
152 
154  T get() { return remove_first(); }
155 
157  T & rear() { return get_last(); }
158 
160  T & front() { return get_first(); }
161 
163  T & push(const T & item) { return insert(item); }
164 
166  T pop() { return remove_first(); }
167 
169  T & top() const { return get_first(); }
170 
171  const size_t & size() const { return num_items; }
172 
173  class Iterator : public Node::Iterator
174  {
175  /* TODO: Agregar atributos y métodos que hagan falta
176  la implementación de este iterador la hice minimalista
177  para pruebas de Dyn_Slist_Nc
178  */
179  public:
180  Iterator(Dyn_Slist_Nc<T> & list) : Node::Iterator(list)
181  {
182  // empty
183  }
184 
185  T & get_curr_n() const noexcept
186  {
187  return Node::Iterator::get_curr()->get_data();
188  }
189 
190  T & get_curr() const { return Dyn_Slist_Nc::Iterator::get_curr(); }
191 
192  };
193 
195  Dyn_Slist_Nc() : Node(), head(this), num_items(0)
196  {
197  // empty
198  }
199 
201  Dyn_Slist_Nc(const Dyn_Slist_Nc & l) : Node(l), head(this), num_items(0)
202  {
203  for (Iterator it(const_cast <Dyn_Slist_Nc &>(l));
204  it.has_curr(); it.next_ne())
205  append(it.get_curr_ne());
206  }
207 
208  ~Dyn_Slist_Nc()
209  {
210  empty();
211  }
212 
222  {
223  if (this == &list)
224  return *this;
225 
226  empty();
227 
228  assert(this->is_empty());
229 
230  for (Iterator it(const_cast<Dyn_Slist_Nc &>(list));
231  it.has_curr(); it.next_ne())
232  append(it.get_curr_ne());
233 
234  return *this;
235  }
236 
237  T & operator [] (const size_t & n)
238  {
239  Iterator it(*this);
240  for (int i = 0; i < n and it.has_curr(); ++i, it.next_ne());
241 
242  return it.get_curr();
243  }
244  };
245 }
246 
247 # endif
248 
Definition: tpl_dyn_slist_nc.H:50
T pop()
Si this es una pila, entonces elimina el tope.
Definition: tpl_dyn_slist_nc.H:166
void empty()
Vacía totalmente a la lista.
Definition: tpl_dyn_slist_nc.H:62
Dyn_Slist_Nc(const Dyn_Slist_Nc &l)
Constructor de copia.
Definition: tpl_dyn_slist_nc.H:201
Dyn_Slist_Nc()
Constructor vacío.
Definition: tpl_dyn_slist_nc.H:195
T & top() const
Si this es una pila, entonces retorna el tope.
Definition: tpl_dyn_slist_nc.H:169
T & put(const T &item)
Si this es una cola, entonces mete el elemento item.
Definition: tpl_dyn_slist_nc.H:151
T & get_last()
Retorna una referencia al último elemento de la lista.
Definition: tpl_dyn_slist_nc.H:123
T & insert(const T &data)
Definition: tpl_dyn_slist_nc.H:81
Definition: ah-comb.H:35
T remove_first()
Definition: tpl_dyn_slist_nc.H:137
T & push(const T &item)
Si this es una pila, entonces inserta item.
Definition: tpl_dyn_slist_nc.H:163
T & rear()
Si this e suna cola, entonces retorna el elemento más joven.
Definition: tpl_dyn_slist_nc.H:157
Dyn_Slist_Nc & operator=(const Dyn_Slist_Nc &list)
Definition: tpl_dyn_slist_nc.H:221
T & get_first()
Retorna una referencia al primer elemento de la lista.
Definition: tpl_dyn_slist_nc.H:114
T & front()
Si this e suna cola, entonces retorna el elemento más antiguo.
Definition: tpl_dyn_slist_nc.H:160
Definition: List.H:49
T & append(const T &data)
Definition: tpl_dyn_slist_nc.H:102

Leandro Rabindranath León