[面试] 从尾到头打印链表-递归实现

/* 05_从尾到头打印链表 */
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
struct Node {
    int value;
    Node* next;
    Node() {}
    Node(int k) : value(k) {}
};
Node* creat_list(Node* p) {
    Node* top = p;
    for (int i = 1; i <= 9; i++) {
        p->next = new Node(i);
        p = p->next;
    }
    return top;
}
void re_print(Node* p) {
    if (p->next) {
        re_print(p->next);
    }
    cout << p->value << "->";
}
int main() {
    Node *ph = new Node();
    ph = creat_list(ph);
    re_print(ph);
    cout << "NULL" << endl;
    return 0;
}

posted @ 2013-11-13 11:07  小尼人00  阅读(143)  评论(0编辑  收藏  举报