doubt procedure output by screen data from a list c ++

0

Good, I have an exercise in which I'm stuck, this is the statement:

  

Implement the RECURSIVE method (DO NOT CONFUSE PLEASE WITH ANOTHER EXERCISE THAT YOU PUT WHERE YOU SAY NO RESOURCE, THIS IS RECURSIVE) of the class dll_t ostream& dll_t<T>::write_reverse(dll_node_t<T>* n, ostream& os) const that shows in reverse order the content of the linked list invoked from the node n . Only the dll_node_t* get_next() method of the class dll_node_t can be used to access the next element. In no case will the method dll_node_t* get_prev() be allowed.

THIS ARE THE CLASSES THAT GIVE ME:

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);
};

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 it this way, I know that my approach is wrong, that I have gaps in    some aspects, but I appreciate any help:

template<class T>

   ostream& dll_t<T>:: write_reverse(dll_node_t<T>* n, ostream& os) const {
     assert(!empty());
     while(n!=NULL){
       n=n->get_data();
       n.write(os);
   }

I stayed there, I appreciate any help.

Thanks.

    
asked by AER 24.07.2017 в 20:39
source

1 answer

2

Problem.

It seems that you have problems understanding the recursion or they have not explained it to you in detail. Allow me to quote an excerpt from Wikipedia (the highlight is mine):

  

Most programming languages support recursion by allowing a function to call itself from the program text.

That is, for a call to be recursive, the recursive function must call itself, and you are not doing it 1 :

write_reverse(dll_node_t<T>* n, ostream& os) const {
    assert(!empty());
    while(n!=NULL){
        n=n->get_data();
        n.write(os);
    }
} // DONDE esta la llamada a write_reverse dentro de write_reverse?!

Proposal.

To print any sequence in reverse order using recursion, you must jump to the last node and, once in this one, print and unscrew the recursion.

write_reverse(dll_node_t<T>* n, ostream& os) const {
    if (n) { // Si el nodo no es null.
        write_reverse(n->next_, os); // Avanzamos.
        n.write(os); // Imprimimos.
    }
}

In this possible approach to the problem, since it is first advanced and then printed, the list would be shown in the opposite direction; since it calls itself it would be recursive. Beware, assume that the% po_de% pointer is null when there is no next element.

Council.

Review the basic concepts of C ++, it seems that you do not have enough clear and this will make it difficult for you to address questions like this (or more complex) without help.

1 In addition to the fact that your function is incorrect since the closing key is missing, the template mark belongs to your class, not the member function and the call to next_ is meaningless (and should not even compile).

    
answered by 25.07.2017 / 09:31
source