recursion - 以相反的顺序打印单向链表

标签 recursion linked-list nodes reverse iteration

好的,这是路易斯安那大学东南部 CMPS 280 测试中的额外问题。三行反向打印单向链表。有任何想法吗?

最佳答案

C 实现您的奖金问题,分三行:

#include <stdio.h>

struct node {
    int data;
    struct node* next;
};

void ReversePrint(struct node* head) {
    if(head == NULL) return;
    ReversePrint(head->next);
    printf("%d ", head->data);
}

int main()
{
    struct node first;
    struct node second;
    struct node third;

    first.data = 1;
    second.data = 2;
    third.data = 3;

    first.next = &second;
    second.next = &third;

    ReversePrint(&first); // Should print: 3 2 1
    printf("\n");

    return 0;
}

关于recursion - 以相反的顺序打印单向链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27047351/

相关文章:

python-3.x - 将单一递归调用算法转换为分支、多重递归调用算法

javascript - 普通 JavaScript 中的滚动动画仅适用于导航链接

c++ - 错误 :expected primary-expression before ',' token//function call

xml - 使用 Powershell 删除一组 XML 元素

javascript - 如何在找到第一个匹配项时停止 DOM 搜索循环?

recursion - 二叉搜索树的递归中序遍历

haskell - 如何使用 State Monad 和可变向量解决 Alphametics 难题?

c - 嵌套结构,读取字符串 C 的字符时出错

java - 使用迭代器删除时出现链接列表错误

javascript 对象被插入数组作为引用