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)
{
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 *
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct list
{
private:
struct node
{
int data;
node *next;
};
node* head;
public:
list()
{
head = NULL;
}
void f_insert(int n)
{
node *temp;
temp = new node;
temp->data = n;
temp->next = head;
head = temp;
}
void print()
{
node *ptr = head;
while (ptr != NULL)
{
cout << ptr->data << " ";
ptr = ptr->next;
}
cout << endl;
}
void r_print()
{
}
};
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(); l1.r_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.