Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here goes my code:
here i have written the program creating a structure and a different functions to implement my linked list :

(what i want to do is to print my given linked list in reverse order.. i just dont want to reverse the whole linked list but i want to print my given linked list in reverse order..)

i am able to do it using the function method .. but not with the class method.
plz do refer the code...

struct node
{
    int data;
    struct node *next;
};
void print(struct node *heal)
{
        if(heal== NULL)
            return;
        printf("%d ",heal->data);
        print(heal->next);
}
void rev_print(struct node *heal)
{
       //my reverse print function but not a part of class accessory.

 if(heal== NULL)
            return;
        rev_print(heal->next);
        printf("%d ",heal->data);

}

int main()
{
    int i,j,no,x;
    printf("how many no u want to enter");
    scanf("%d",&i);
    for(j= 0 ; j< i ;  j++)
    {
            scanf("%d",&no);
            insert(no);
    }
    print(head_dup);
    printf("\nthe reve list is -->\n");
    rev_print(head_dup);
    return 0;
}


the above thing works perfecty.. rev_print is the recursive function i have used to print my linked list in recursive order.

*here goes my code using classes *
C++
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct list
{
private:
	struct node
	{
		int data;
		node *next;
	};
	node* head;
public:
	list()
	{
		head = NULL;
	}
	//front insertion
	void f_insert(int n)
	{
		node *temp;
		temp = new node;
		temp->data = n;
		temp->next = head;
		head = temp;
	}
	//printing all data
	void print()
	{
		node *ptr = head;
		while (ptr != NULL)
		{
			cout << ptr->data << " ";
			ptr = ptr->next;
		}
		cout << endl;
	}
	void r_print()
	{
		//what should be here because this is the function that should be recursive so that i can print my linked list in the recursive way?	
	}
};
int main()
{
	list l1;
	l1.f_insert(1);
	l1.f_insert(2);
	l1.f_insert(3);
	l1.f_insert(4);
	l1.b_insert(5);
	l1.b_insert(6);
	l1.print();                // 6 5 4 3 2 1
	l1.r_print(); // what it should print : 1 2 3 4 5 6(reverse print)
	return 0;
}


What I have tried:

please do refer my code because i have commented my questions there...
i have tried it using structures and making functions for different things that i want to do in my linkedlist ...
i was just trying it with classes and i am facing a difficulty in making a recursive class defination.
Posted
Updated 11-Jun-16 5:03am
Comments
nv3 11-Jun-16 11:23am    
Solution 1 tells you how to do this. But note that using a recursive function for reversing the order of a linked list is not a good idea. Imagine your list contains a million entries! You would be putting a million stack frames on your stack for the million-deep nesting of your revPrint function. Most likely, your stack will overflow and your program crash.

A much better method would be to use a double-linked list, in which you simply start at the end and follow the backward links.

I suspect, this is homework and your teacher has given you that as an assignment. If that is so, tell your teacher that this was a rather bad example. There are better examples for recursion that really make sense.
Sergey Alexandrovich Kryukov 11-Jun-16 12:37pm    
Exactly. This is one of the problems totally unsuitable for recursive methods.
—SA

1 solution

To do it recursively in the "Class method" is the same as to do it in the non-class method - all you have to do is pass the node to print in:
C++
void revPrint(node* pn)
   {
   if (pn != NULL)
      {
      revPrint(pn->next);
      count << pn->data << " ";
      }
   }
Then just call that from your r_Print method, and pass it the head of the list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900