C:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int val;
struct Node * next;
};
void CreateList(struct Node ** head) {
int v;
if(scanf("%d", &v)) {
*head = (struct Node*)malloc(sizeof(struct Node));
(*head)->val = v;
(*head)->next = NULL;
CreateList(&(*head)->next);
}
}
void PositivePrint(struct Node * head) {
if (head) {
printf("%d ", head->val);
PositivePrint(head->next);
}
}
void InversePrint(struct Node * head) {
if (head) {
InversePrint(head->next);
printf("%d ", head->val);
}
}
void Destory(struct Node ** head) {
if (*head) {
Destory(&(*head)->next);
free(*head);
}
}
int main()
{
Node * head;
CreateList(&head);
PositivePrint(head);
InversePrint(head);
Destory(&head);
return 0;
}
C++:
#include <iostream>
struct Node {
int val;
Node * next;
Node() {}
Node(int v) : val(v), next(NULL) {}
~Node() { delete next; }
};
void CreateList(Node * & head) {
int v;
if(std::cin >> v) {
head = new Node(v);
CreateList(head->next);
}
}
void PositiveShow(Node * head) {
if(head) {
std::cout << head->val << ' ';
PositiveShow(head->next);
}
}
void InverseShow(Node * head) {
if(head) {
InverseShow(head->next);
std::cout << head->val << ' ';
}
}
int main()
{
Node * head;
CreateList(head);
PositiveShow(head);
InverseShow(head);
return 0;
}