【发布时间】:2015-07-24 21:06:40
【问题描述】:
逆序打印链表的元素
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace::std;
struct node
{
int data;
node* next;
};
class linklist
{
node* head;
public:
linklist()
{
head = NULL;
}
void create_list(int d);
void reverse_print_list(node*);
}
void reverse_print_list(node* p)
{
if (p == NULL)
return;
reverse_print_list(p->next);
cout << endl << p->data << " ";
}
int main()
{
linklist l1;
l1.create_list(10);
l1.create_list(20);
l1.create_list(30);
l1.create_list(40);
l1.create_list(50);
l1.print_list(& head); // Not allowed , gives compilation error
_getch();
return 0;
}
这里我要传递“head”的地址。但它不能在课堂外访问,因为它是私人数据
如何解决这个问题呢 ???
【问题讨论】:
-
为什么要作为参数传递?列表本身知道头部是什么——它是它的成员。
-
@ molbdnilo :我其实很困惑,但我想通过 main() 函数。后来我想递归调用 print_list。