| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 582 人关注过本帖
标题:一个电影列表正序和逆序显示电影列表。
取消只看楼主 加入收藏
saihuo092617
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2012-9-1
结帖率:25%
收藏
已结贴  问题点数:10 回复次数: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 novies 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",head->name, head->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);
}
void CreateMovies(struct movie **phead, struct movie **pend)这个函数为什么要用指向指针的指针,用void CreateMovies(struct movie * phead, struct movie * pend)不行么,有什么不一样?
在这几个函数中 end 都指向哪里?求大神指教!!!!!
搜索更多相关主题的帖子: void head include rating former 
2013-04-09 13:18
快速回复:一个电影列表正序和逆序显示电影列表。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025490 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved