以相反的顺序打印单链表

Anj*_*ala 5 iteration recursion reverse linked-list nodes

好的,这是在路易斯安那州东南部大学的CMPS 280考试中的奖金问题.以三行反向打印单链表.有任何想法吗?

syn*_*gma 17

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;
}
Run Code Online (Sandbox Code Playgroud)