I found a solved exercise where batteries and lists are used that generated doubts, this is:
Implement the non-recursive method of class
dll_t ostream& write_reverse (dll_node_t<T>* n, ostream& os) const
that shows in reverse order the content of the invoked linked list from node n with help from a stack that stores node pointers. Only thedll_node_t* get_next()
method of the classdll_node_t
can be used to access the next element. In no case will the methoddll_node_t* get_prev()
be allowed.
The exercise solved:
template <class T>
ostream& dll_t<T>::write_reverse2(dll_node_t<T>* n, ostream& os) const {
stack_v_t<dll_node_t<T>*> stack;
while (n != NULL) {
stack.push_back(n);
n = n->get_next();
}
while(!stack.empty()){
stack.top()->write(os);
stack.pop();
}
return os;
}
template <class T>
ostream& dll_t<T>::write_reverse2(ostream& os) const {
reverse2(head_, os);
return os;
}
the class stack he uses:
template <class T>
class stack_v_t{
private:
vector_t<T> v_;
int top_;
public:
stack_v_t(int max_sz);
~stack_v_t(void);
bool empty(void);
T top(void);
void pop(void);
void push(T dato);
};
My doubt: When creating the stack, should not it have a fixed size, does not give it a size, would it have to have it not?