Doubt with exercise doubly linked lists

0

I have a battery exercise, it says:

  

Implement the dll_t void class method   select (dll_t * > & L, const T & x), which saves
  within the list L the pointers to nodes of the invoking list that has as content x.

also, they give me the following classes:

template <class T>
class dll_t {
private:
 dll_node_t<T>* head_;
 dll_node_t<T>* tail_;
 int sz_;
public:
  dll_t(void);
 ~dll_t(void);
  void insert_tail(dll_node_t<T>*);
  void insert_head(dll_node_t<T>*);
  dll_node_t<T>* extract_tail(void);
  dll_node_t<T>* extract_head(void);
  dll_node_t<T>* get_tail(void) const;
  dll_node_t<T>* get_head(void) const;
  bool empty(void);
  void remove(dll_node_t<T>*);
  ostream& write(ostream& os) const;
  void select(dll_t<dll_node_t<T>*>& L, const T& x);
private:
  bool igual(const T& a, const T&b);
};

and this one:

template <class T>
class dll_node_t {

private:
  dll_node_t<T>* next_;
  dll_node_t<T>* prev_;
  T data_;
public:
  dll_node_t();
  dll_node_t(const T& data);
  virtual ~dll_node_t(void);
  void set_next(dll_node_t<T>*);
  void set_prev(dll_node_t<T>*);
  dll_node_t<T>* get_next(void) const;
  dll_node_t<T>* get_prev(void) const;
  void set_data(const T& data);
  T get_data(void) const;
  ostream& write(ostream& os) const;
};

I tried to do so, before compiling it I knew I had some basic error in the approach:

template<class T>
void vector_t :: select (dll_t<dll_node_t<T>*>& L, const T& x){
dll_t<T> lista;
dll_node_t<T>* aux = lista.get_head();
while(aux!=NULL){

  T dato = aux->get_data();

  if ( dato == x){

    aux = new dll_node_t<T> (T(dato));
    L.insert_tail(aux);

  }

  aux= aux -> get_next();
}

I do not know where I have this error in the approach to the exercise, I'm still looking for it.

Thanks

    
asked by AER 23.07.2017 в 01:24
source

1 answer

1

Error in the approach if It says

  

saves within the L list the pointers to nodes of the invoking list

It does not say recreating the nodes again for each node that is found you delete this line aux = new dll_node_t<T> (T(dato)); and already correct that

It says:

  

invoking list

That is, lista1->select(lista2,datox) or lista1.select(lista2,datox) It is a method in the class dll_t In summary if I understood well it should be like this:

template<class T>
//Err1 NO es: void vector_t ::... es dll_t
void dll_t :: select (dll_t<dll_node_t<T>*>& L, const T& x){

//dll_t<T> lista;    
//El metodo se ejecuta en una lista pertenece a dll_t "this.get_head()"
dll_node_t<T>* aux = get_head();

while(aux!=NULL){

  T dato = aux->get_data();

  if ( dato == x){
    //Eliminar esta linea de abajo
    //aux = new dll_node_t<T> (T(dato));
    L.insert_tail(aux);    
  }    
  aux= aux -> get_next();
}    
}
    
answered by 24.07.2017 / 19:20
source