如何编写递归类附件? (以递归顺序打印我的链表) [英] How to write a recursive class accessory ? ( to print my linked list in recursive order)
问题描述
这里是我的代码:
这里我写了程序创建一个结构和一个不同的函数来实现我的链表:
(我想做的是以相反的顺序打印我给定的链接列表..我只是不想反转整个链表但我想以相反的顺序打印我给定的链表..)
i能够使用函数方法实现它..但不能使用类方法。
plz请参考代码...
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;
}
以上的东西是完美的.. rev_print是我用来递归打印链表的递归函数订单。
*这里使用类我的代码*
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;
}
//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;
}
我的尝试:
请参考我的代码因为我在那里评论了我的问题...
i已经尝试使用结构并为我想要在我的链表中做的不同事情制作函数。 。
i只是在尝试使用类而且我在进行递归类定义时遇到了困难。
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.
推荐答案
以递归方式执行此操作类方法与非类方法中的相同 - 所有你需要做的就是传递节点进行打印:
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:
void revPrint(node* pn)
{
if (pn != NULL)
{
revPrint(pn->next);
count << pn->data << " ";
}
}
然后只需从你的r_Print方法中调用它,并将其传递给列表的头部。
Then just call that from your r_Print method, and pass it the head of the list.
这篇关于如何编写递归类附件? (以递归顺序打印我的链表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!