一个正序跟逆序输出的列表的C程序,求解释其中的一个函数
一个正序和逆序输出的列表的C程序,求解释其中的一个函数
#include <stdio.h>
#include <stdlib.h> /* has the malloc prototype */
#include <string.h> /* has the strcpy prototype */
#define TSIZE 45 /* size of array to hold title */
struct film {
char title[TSIZE];
int rating;
struct film * next; /* points to next struct in list */
};
void show_rec(const struct film * pf); /* recursive function */
int main(void)
{
struct film * head = NULL;
struct film * prev, * current;
char input[TSIZE];
puts("Enter first movie title:");
while (gets(input) != NULL && input[0] != '\0')
{
current = (struct film *) malloc(sizeof(struct film));
if (head == NULL) /* first structure */
head = current;
else /* subsequent structures */
prev->next = current;
current->next = NULL;
strcpy(current->title, input);
puts("Enter your rating <0-10>:");
scanf("%d", ¤t->rating);
while(getchar() != '\n')
continue;
puts("Enter next movie title (empty line to stop):");
prev = current;
}
if (head == NULL)
printf("No data entered. ");
else
printf ("Here is the movie list:\n");
current = head;
while (current != NULL)
{
printf("Movie: %s Rating: %d\n", current->title, current->rating);
current = current->next;
}
if (head != NULL)
{
printf("\nHere is the list in reverse order:\n");
show_rec(head);
}
printf("Bye!\n");
return 0;
}
void show_rec(const struct film * pf)/* 工作原理是什么?为什么能这样?我认为只会连续输出最后一项 */
{
if (pf->next != NULL)
show_rec(pf->next);
printf("Movie: %s Rating: %d\n", pf->title, pf->rating);
}
------解决方案--------------------
递归嘛 没什么好讲的 谷歌递归函数
------解决方案--------------------
用递归来实现从最后一个节点开始输出啊
#include <stdio.h>
#include <stdlib.h> /* has the malloc prototype */
#include <string.h> /* has the strcpy prototype */
#define TSIZE 45 /* size of array to hold title */
struct film {
char title[TSIZE];
int rating;
struct film * next; /* points to next struct in list */
};
void show_rec(const struct film * pf); /* recursive function */
int main(void)
{
struct film * head = NULL;
struct film * prev, * current;
char input[TSIZE];
puts("Enter first movie title:");
while (gets(input) != NULL && input[0] != '\0')
{
current = (struct film *) malloc(sizeof(struct film));
if (head == NULL) /* first structure */
head = current;
else /* subsequent structures */
prev->next = current;
current->next = NULL;
strcpy(current->title, input);
puts("Enter your rating <0-10>:");
scanf("%d", ¤t->rating);
while(getchar() != '\n')
continue;
puts("Enter next movie title (empty line to stop):");
prev = current;
}
if (head == NULL)
printf("No data entered. ");
else
printf ("Here is the movie list:\n");
current = head;
while (current != NULL)
{
printf("Movie: %s Rating: %d\n", current->title, current->rating);
current = current->next;
}
if (head != NULL)
{
printf("\nHere is the list in reverse order:\n");
show_rec(head);
}
printf("Bye!\n");
return 0;
}
void show_rec(const struct film * pf)/* 工作原理是什么?为什么能这样?我认为只会连续输出最后一项 */
{
if (pf->next != NULL)
show_rec(pf->next);
printf("Movie: %s Rating: %d\n", pf->title, pf->rating);
}
------解决方案--------------------
递归嘛 没什么好讲的 谷歌递归函数
------解决方案--------------------
用递归来实现从最后一个节点开始输出啊