首页 > 试题广场 >

修改程序清单17.2,使其既能以正序又能以逆序显示电影列表。

[问答题]
修改下面的程序,使其既能以正序又能以逆序显示电影列表。一种方法是修改链表定义以使链表能被双向遍历;另一种方法是使用递归。
/* films2.c -- 使用结构链表 */
#include <stdio.h>
#include <stdlib.h>         /* 提供malloc()原型 */
#include <string.h>         /* 提供strcpy()原型 */
#define TSIZE 45            /* 存放片名的数组大小 */
struct film {
    char title[TSIZE];
    int rating;
    struct film * next;     /* 指向链表的下一个结构 */
};
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) /* 第一个结构 */
            head = currend;
        else
            prev->next = current;
        current->next = NULL;
        strcpy (current->title, input);
        puts ("Enter your rating <0-10>: ");
        scanf ("%d", &current->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;
    }
    /* 任务已经完成,因此释放所分配的内存 */
    current = head;
    while (current != NULL)
    {
        free (current);
        current = current->next;
    }
    printf("Bye!\n");
    return 0;
}
推荐
#include <stdio.h>
#include <stdlib.h> 
#include <string.h> 
#define TSIZE 45
struct movie{
 char name[TSIZE];
 int rating;
 struct movie *next;
 struct movie *former;
};
void CreateMovies(struct movie **phead, struct movie **pend);
void DisplayOriginal(struct movie *head, struct movie *end);
void DisplayReverse(struct movie *head, struct movie *end);
void FreeMoives(struct movie *head, struct movie *end);
int main(void)
{
 struct movie *head = NULL, *end = NULL;
 CreateMovies(&head ,&end);
 DisplayOriginal(head,end);
 DisplayReverse(head,end);
 FreeMoives(head,end);
 return 0;
}
void CreateMovies(struct movie **phead, struct movie **pend)
{
 char input[TSIZE];
 puts("Enter first movie title:");
 while(gets(input) != NULL && input[0] != '\0')
 {
 if(*phead == NULL)
 {
 *phead = malloc( sizeof(struct movie) );
 (*pend) = *phead;
 }
 else
 {
 (*pend)->next = malloc( sizeof(struct movie) );
 (*pend)->next->former = (*pend);
 (*pend) = (*pend)->next;
 }
 strcpy((*pend)->name, input);
 puts("Enter your rating :");
 scanf("%d",&(*pend)->rating);
 getchar();
 puts("Enter next movie title (empty line to stop):");
 }
}
void DisplayOriginal(struct movie *head, struct movie *end)
{
 if( head == NULL )
 {
 printf("No movies in the list\n");
 return;
 }
 printf("display in the original order:\n");
 while(head != end)
 {
 printf("%s\t\t%d\n",head->name,head->rating);
 head = head->next;
 }
 printf("%s\t\t%d\n",head->name,head->rating);
}
void DisplayReverse(struct movie *head, struct movie *end)
{
 if( end == NULL )
 {
 printf("No movies in the list\n");
 return;
 }
 printf("display in the reverse order:\n");
 while(end != head)
 {
 printf("%s\t\t%d\n",end->name,end->rating);
 end = end->former;
 }
 printf("%s\t\t%d\n",end->name,end->rating);
}
void FreeMoives(struct movie *head, struct movie *end)
{
 struct movie *previous;
 if( head == NULL ) return;
 while(head != end)
 {
 previous = head;  //注意:先得到下一个指针,再释放前一个指针
 head = head->next;
 free(previous);
 }
 free(head);
}

发表于 2018-03-18 21:52:43 回复(0)