EjercicioLista

=Creación de una lista= El objeto del presente ejercicio es que usted cree una lista doblemente enlazada utilizando C++. Una lista será una concatenación de nodos los cuales tendrán el valor almacenado, y punteros al siguiente y anterior elementos. Para hacer la lista lo más genérica posible, el nodo será una clase plantilla con la siguiente definición:

1. Clase Node
File: node.h code format="cpp" using namespace std;
 * 1) ifndef _Node_H_
 * 2) define _Node_H_
 * 3) include

namespace edi{ namespace list{

/**\brief A node of a list. The node has a link to previous and a link to next element in the list. Besides, it has a value indicating the node content template class Node {

public: /**Empty constructor */   Node;

/**Paramtrized constructor. Creates the element and set its value */   Node(const T& v);

/**Copy construtor */   Node(const Node &N);

/**Pointer to the next element */   Node  *next;

/**Pointer to the prev element */   Node  *prev;

/**Value */   T value; }; } }


 * 1) endif

code

Usted deberá completar todo lo necesario.

Nota: Los espacios de nombre se pueden concatenar, como ve arriba. Despues, para usarlos, code format="cpp" //EN el main using namespace edi::list; int main {

} code

2. Clase Iterator
Los iteradores serán mecanismos para recorrer una estructura de datos. Son una abstracción para avanzar o retroceder en los elementos de una ED. Cree la clase ListIterator.

code format="cpp" namespace edi{
 * 1) ifndef _Iterator_H_
 * 2) define _Iterator_H_
 * 3) include

namespace list{

/**\brief Iterator to a list. template class Iterator {
 * An iterator is a class that wraps a pointer to a list node. The class allows to move the pointer forward and backward safely using operators.
 * An iterator is a class that wraps a pointer to a list node. The class allows to move the pointer forward and backward safely using operators.

public: /**Empty constructor */   Iterator;

/**Copy constructor */   Iterator(const Iterator &I); /** Parametrized constructor. Assigns this iterator to the element passed */   Iterator(Node * ptr);

/**Assign operator */  Iterator & operator=(const Iterator &I);

/**Increment operator. Moves the pointer to the next node of the list. If the next element is NULL does nothing */  Iterator & operator++(int);

/**Decrement operator. Moves the pointer to the previous node of the list. However, if the previous node is NULL, this function does nothing */  Iterator & operator--(int);

/**Increments several pos positions. You can implement this by calling pos times to operator++ */  Iterator   operator+(int pos);

/**Decrement several pos positions. You can implement this by calling pos times to operator-- */  Iterator   operator-(int pos);

/**Comparison operator */ bool operator==(const Iterator &I);

/**Comparison operator */ bool operator!=(const Iterator &I);

/**Returns the value of the node passed */ T & operator(void);

/** Returns the internal pointer to the node */ Node * ptr; private: /** */  Node *_ptr; }; } }


 * 1) endif

code

3. Clase List
Ahora deberemos crear la clase lista. La clase lista que crearemos tendrá siempre un elemento inútil (aun cuando esté) vacía. Este elemento inútil será siempre el último elemento y nos servirá para conocer el final de la lista. Este elemento siempre está detrás del último elemento que se inserte en la lista.



Además, la lista contará con un puntero al primer elemento y otro al último. Al inicio, cuando la lista está vacía, ambos apuntan al elemento inútil. Cuando el tamaño aumenta, el puntero al inicio cambia, pero el puntero al final se mantiene en el elemento inútil que es siempre el último.

code format="cpp" using namespace std; namespace edi{
 * 1) ifndef _List_H_
 * 2) define _List_H_
 * 3) include 
 * 4) include 
 * 5) include

namespace list{ /**\brief List of generic types template class List { public: /** */  List {     _begin=_end=&_dummy; _size=0; }
 * This class represent a list of doubled linked noded.
 * \section Implementation Implementation details
 * The list has an extra node, called _dummy, that is always the last element of the list. This helps
 * to detect where is the end of the list. This artifact allows to use iterators moving from the beginning to the end in a straightforward manner since one can know if they * have passed the last element, i.e., if you are in the _dummy element, then you passed the last "real" list node.
 * The _dummy->next element always point to NULL and the dummy->prev element always points to the last node of the list.
 * The list have also a pointer to the beginning "_begin" and a pointer to the end "_end". Of course, _end always points to the _dummy element
 * The _dummy->next element always point to NULL and the dummy->prev element always points to the last node of the list.
 * The list have also a pointer to the beginning "_begin" and a pointer to the end "_end". Of course, _end always points to the _dummy element
 * The list have also a pointer to the beginning "_begin" and a pointer to the end "_end". Of course, _end always points to the _dummy element
 * The list have also a pointer to the beginning "_begin" and a pointer to the end "_end". Of course, _end always points to the _dummy element

/** ~List{clear;}

/**Returns an iterator to the first element Iterator beginconst {return Iterator(_begin);}

/**Returns an iterator to an element behind the list. */ Iterator endconst {return Iterator(_end);}

/**Adds an element at the position indicated and returns the new iterator to the new element */ Iterator add(Iterator pos,const T&v);

/**Deletes the element specified */ Iterator del(Iterator pos);

/**Finds an iterator to first elemento equal to "val" that is bahind "beg". If the element is not in the list, returns a end iterator */ Iterator<T> find(Iterator<T> beg,T &val);

/**Returns the number of elements in the list */ unsigned int sizeconst;

/**Clear all elements of the list */ void clear ;

/**Prints the list information */ void printCheck { cout<<"begin="<<_begin<<" _end="<<_end<<endl; Node<T> *ptr=_begin; while(ptr!=_end) {   cout<<"val="<<ptr->value<<" ptr="<<ptr<<" prev="<<ptr->prev<<" next="<<ptr->next<<endl; ptr=ptr->next; } }

private: Node<T> _dummy; //last element, it is not accesible Node<T>* _begin;//pointer to the first element of the list. If list is empty, to the _dummy element Node<T>* _end;//always points to _dummy, an element after the list elements unsigned int _size; };

} }
 * 1) endif

code

4. Prueba de su codigo
Puede probar su código con el siguiente programa code format="cpp" using namespace std; using namespace edi::list; int main { List ilist; Iterator It; ilist.add(ilist.end,1); ilist.add(ilist.end,3); ilist.add(ilist.end,4); ilist.add(ilist.end,5); It=ilist.begin; It++; It=ilist.add(It,2); It--; It=ilist.add(It,0);
 * 1) include <list.h>
 * 2) include
 * 3) include

for(It=ilist.begin;It!=ilist.end;It++) cout<< It<<" "; cout<<endl;

It=ilist.end-1; do{ cout<< It<<" ";cout.flush; It--; } while(It!=ilist.begin);

cout<<endl;

List flist; Iterator fIt;

fIt=flist.add(flist.end,1.1); flist.add(fIt,0.1);

for(fIt=flist.begin;fIt!=flist.end;fIt++) cout<< fIt<<" "; cout<<endl;

fIt=flist.begin+1; flist.del(fIt); for(fIt=flist.begin;fIt!=flist.end;fIt++) cout<< fIt<<" "; cout<<endl; } code