比如:輸入一個鏈表,按鏈表從尾到頭的順序返回一個ArrayList
#include"stdio.h"
#include"stdlib.h"
typedef struct node * pointer;
struct node
{
char dat;
pointer next;
};
pointer creat( pointer rear)
{
pointer p,head;
char c;
head = (struct node *)malloc(sizeof(struct node));
rear = head;
while (scanf_s("%c",&c), c != '$'){
p = (struct node *)malloc(sizeof(struct node));
p->dat = c;
rear->next = p;
rear = p;
}
rear->next = NULL;
return head;
}
void recursion_printf(pointer head)
{
pointer p;
p = head->next;
if (p != NULL)
{
recursion_printf(p);
printf("%c", p->dat);
}
}
void main()
{
pointer head, rear;
rear = head = NULL;
head=creat(rear);
recursion_printf(head);
printf("\n");
}