使用递归反向输出数组的内容
1 #include <stdio.h>
2
3 int list[6] = {1,2,3,4,5,6};
4
5 void invert_array(int j)
6 {
7 if(j < 6)
8 {
9 invert_array(j + 1);
10 printf("[%d]",list[j]);
11 }
12 }
13
14 int main()
15 {
16 int i;
17
18 printf("数组的内容:\n");
19 for(i = 0;i < 6;i++)
20 printf("[%d]",list[i]);
21 printf("\n");
22 printf("递归输出数组的内容:\n");
23 invert_array(0);
24 printf("\n");
25
26 return 0;
27 }
$gcc -o main *.c
$main
数组的内容:
[1][2][3][4][5][6]
递归输出数组的内容:
[6][5][4][3][2][1]
递归函数的实际处理过程
1 #include <stdio.h>
2
3 int factrial(int j)
4 {
5 int sum = 0;
6 int temp = 0;
7
8 if(j == 0)
9 {
10 sum = 1;
11 printf("到达终止条件(j = 0)\n");
12 }
13 else
14 {
15 printf("从函数factrial(%d)调用前的状态: sum = %d\n",j,sum);
16 temp = factrial(j - 1);
17 printf("返回函数factrial(%d)后的状态:sum = %d\n",j,sum);
18 sum = j * temp;
19 printf(" ==> 在计算%d!阶乘后的状态:sum = %d\n",j,sum);
20 }
21 return sum;
22 }
23
24 int main()
25 {
26 printf("4! = %d\n",factrial(4));
27
28 return 0;
29 }
$gcc -o main *.c
$main
从函数factrial(4)调用前的状态: sum = 0
从函数factrial(3)调用前的状态: sum = 0
从函数factrial(2)调用前的状态: sum = 0
从函数factrial(1)调用前的状态: sum = 0
到达终止条件(j = 0)
返回函数factrial(1)后的状态:sum = 0
==> 在计算1!阶乘后的状态:sum = 1
返回函数factrial(2)后的状态:sum = 0
==> 在计算2!阶乘后的状态:sum = 2
返回函数factrial(3)后的状态:sum = 0
==> 在计算3!阶乘后的状态:sum = 6
返回函数factrial(4)后的状态:sum = 0
==> 在计算4!阶乘后的状态:sum = 24
4! = 24
使用数组内容和递归方法创建基本链表和输出链表的内容
1 #include <stdio.h>
2 #include <stdlib.h>
3 struct list
4 {
5 int data;
6 struct list *next;
7 };
8
9 typedef struct list node;
10 typedef node* link;
11
12 void print_list(link ptr)
13 {
14 if(ptr != NULL)
15 {
16 printf("[%d]",ptr->data);
17 print_list(ptr->next);
18 }
19 }
20
21 link create_list(int *array,int len,int pos)
22 {
23 link head;
24
25 if(pos == len)
26 return NULL;
27 else
28 {
29 head = (link)malloc(sizeof(node));
30 if(!head)
31 return NULL;
32 head->data = array[pos];
33 head->next = create_list(array,len,pos + 1);
34 return head;
35 }
36 }
37
38 int main()
39 {
40 int list[6] = {1,2,3,4,5,6};
41 link head;
42
43 head = create_list(list,6,0);
44 if(!head)
45 {
46 printf("内存分配失败!\n");
47 exit(1);
48 }
49 printf("链表的内容:\n");
50 print_list(head);
51 printf("\n");
52
53 return 0;
54 }
$gcc -o main *.c
$main
链表的内容:
[1][2][3][4][5][6]
递归反向输出链表,仅仅是print_list和printf两行代码相互对调
1 #include <stdio.h>
2 #include <stdlib.h>
3 struct list
4 {
5 int data;
6 struct list *next;
7 };
8
9 typedef struct list node;
10 typedef node* link;
11
12 void print_list(link ptr)
13 {
14 if(ptr != NULL)
15 {
16 print_list(ptr->next);
17 printf("[%d]",ptr->data);
18 }
19 }
20
21 link create_list(int *array,int len,int pos)
22 {
23 link head;
24
25 if(pos == len)
26 return NULL;
27 else
28 {
29 head = (link)malloc(sizeof(node));
30 if(!head)
31 return NULL;
32 head->data = array[pos];
33 head->next = create_list(array,len,pos + 1);
34 return head;
35 }
36 }
37
38 int main()
39 {
40 int list[6] = {1,2,3,4,5,6};
41 link head;
42
43 head = create_list(list,6,0);
44 if(!head)
45 {
46 printf("内存分配失败!\n");
47 exit(1);
48 }
49 printf("链表的内容:\n");
50 print_list(head);
51 printf("\n");
52
53 return 0;
54 }
$gcc -o main *.c
$main
链表的内容:
[6][5][4][3][2][1]