简体   繁体   中英

Reversing Linked List with Recursion, using STL

Code for Reversing Linked List with Recursion, using STL

          #include<iostream>
#include<conio.h>
#include<list>
using namespace std;
template<typename T>
class node
{
public:
    T data;
    node<T> *next;
    node(){ next = NULL; }
    node(const T& item, node<T> *nextnode = NULL)
    {
        data = item;
        next = nextnode;
    }
};
template<typename T>
class Reverse_list
{
private:
    node<T> *head;
    void reverse(node<T> *front);
public:
    Reverse_list(){ head = NULL; }
    //template<typename T>
    void Reverse();
    template<typename T>
    void Display( list<T>& alist );
};


int main()
{
    Reverse_list <int> rl;
    list<int> intlist;
    int size, no;
    cout << "Size of List ?? ";
    cin >> size;
    for (int i = 1; i <= size; i++)
    {
        cout << "Enter the " << i <<" "<< "element";
        cin >> no;
        intlist.push_front(no);
    }


    rl.Display(intlist);

    rl.Reverse();
    rl.Display(intlist);
    _getch();
    return 0;

}

template<typename T>
void Reverse_list<T>::Display(list<T>& alist)
{
    list<int>::iterator iter = alist.begin();
    while (iter != alist.end())
    {
        cout << *iter << "  ";
        iter++;
    }
}

template<typename T>
void Reverse_list<T>::reverse(node<T> *front)
{
    if (front->next == NULL)
    {
        head = front;
        return;
    }
    reverse(front->next);
    node<int> *back = front->next;
    back->next = front;
    front->next = NULL;
}
template<typename T>
void Reverse_list<T>::Reverse()
{
    reverse(head);
}

The above code generates 2 errors.

Error 1) No instance of function template matches the argument list. ( No error number.)

If I remove line 1 ( mentioned in a code ) then above error is no more. ( Why? )

Error 2) C2783: 'void Reverse_list::Reverse1(void)' : could not deduce template argument for 'T'

How to solve above errors.

In above program , I wanted to pass " head" node ( which is private ) as
argument to Reverse function . But we can not access private member outside of the class. So I passed indirectly. Is this a correct way of passing ?? Or there is some other way of accessing private data ??

I'm not sure to understand your intentions but...

You're trying to declare a method ( reverse() ) inside another method ( Reverse() ) ? Uhmmm....

We return to this later.

Imagine that the following instruction is correct instruction of Reverse_list<T>::Reverse()

node<T> *back = front->next;

Why you declare back as a pointer to a generic Node<T> when you assign front->next (so a specific Node<int> ) to it?

If you define back as a node<int> pointer, the method Reverse() has no longer reason to be a template (dependant from T ) method. And you can avoid both errors.

With your actual code, when you call

rl.Reverse();

you call a template method but the compiler doesn't know how to determine the type T . You could explicit it in this way

rl.Reverse<int>();

but, as written before, I thik it's better if you remove the whole template part.

Or, alternatively, you can transform the whole class in a template class; where head is a pointer to a generic Node<T> , not a specifica Node<int> .

Something like (if I understand correctly your intentions)

template <typename T>
class Reverse_list
 {
   private:
      node<T> *head;

      void reverse (node<T> * front);

   public:
      Reverse_list() : head(NULL)
       { }

      void Reverse();
      void Display(list<T>& alist);
 };

template<typename T>
void Reverse_list<T>::reverse (node<T> * front)
 {
   if (front->next == NULL)
    {
      head = front;
      return;
    }
   reverse(front->next);
   node<T> *back = front->next;
   back->next = front;
   front->next = NULL;
 }

template<typename T>
void Reverse_list<T>::Reverse()
 { reverse(head); }

In this case, in main() , rl should be declared as

Reverse_list<int> rl;

fixing T as int , and the call to Reverse() should be

rl.Reverse();

--- EDIT 2016.05.10 ---

With the "template Reverse_list" solution, you should correct three points (at last).

1) in Reverse_list class declaration, you have commented the template<typename T> row before void Reverse() ; good; you should delete (comment) the same line (for the same reason) before void Display( list<T>& alist ); ; so the class become

template<typename T>
class Reverse_list
{
private:
    node<T> *head;
    void reverse(node<T> *front);
public:
    Reverse_list(){ head = NULL; }
    //template<typename T>
    void Reverse();
    //template<typename T>
    void Display( list<T>& alist );
};

2) Display() now is a method of a templated class; so the line

list<int>::iterator iter = alist.begin();

become

list<T>::iterator iter = alist.begin();

3) reverse() now is a method of a templated class; so the line

node<int> *back = front->next;

become

node<T> *back = front->next;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM